1

我正在尝试构建一个正则表达式来在 bigquery 中的表中查找一个可以包含重音符号的单词。输入可以有或没有重音。我在 scala 中对其进行编码,因此可以在 scala 中转换输入或使用 bigquery 的函数。我想一个好的解决方案可能是这样的:

input: serie
word to find: Séries

我可以将输入转换为

r'(?i:s[éèe]r[íìi][éèe]s)'

但我认为最好将表格的列转换为:

r'(?i:s[ée]ries)'

我不知道如何解决第二种情况,或者是否有更好的方法来解决它。提前致谢

4

2 回答 2

1

使用 Java Collator(例如,参见这个答案),您可以采取一种更简单的方法:

scala> val c = java.text.Collator.getInstance()
c: java.text.Collator = java.text.RuleBasedCollator@289747d6
scala> c.setStrength(Collator.PRIMARY)
scala> c.equals("é","E")
res24: Boolean = true
scala> c.setStrength(Collator.SECONDARY)
scala> c.equals("é","E")
res26: Boolean = false

这会考虑到您的语言环境,因为构成“基本”(等效)字符的规则因语言而异。

于 2019-02-21T07:57:06.213 回答
1

我正在尝试构建一个正则表达式来在 bigquery 中的表中查找一个可以包含重音符号的单词。

玩下面的例子

所以输入可以在scala中转换或使用bigquery的函数

它在 BigQuery 标准 SQL 中完成所有工作

#standardSQL
CREATE TEMP FUNCTION latin2accents(word STRING) AS
((
  WITH lookups AS (
    SELECT 
    'y,a,e,i,o,u,c,ç,n,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,ø,Ø,Å,Á,À,Â,Ä,È,É,Ê,Ë,Í,Î,Ï,Ì,Ò,Ó,Ô,Ö,Ú,Ù,Û,Ü,Ÿ,Ç,Æ,Œ,ñ' AS accents,
    'y,a,e,i,o,u,c,c,n,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,o,O,A,A,A,A,A,E,E,E,E,I,I,I,I,O,O,O,O,U,U,U,U,Y,C,AE,OE,n' AS latins
  ), pairs AS (
    SELECT accent, latin FROM lookups, 
      UNNEST(SPLIT(accents)) AS accent WITH OFFSET AS p1, 
      UNNEST(SPLIT(latins)) AS latin WITH OFFSET AS p2
    WHERE p1 = p2
  ), map AS (
    SELECT latin, CONCAT('[', STRING_AGG(accent, ''), ']') AS accents
    FROM pairs  
    GROUP BY latin
  )
  SELECT CONCAT('(?i:', STRING_AGG(IFNULL(accents, char), ''), ')')
  FROM UNNEST(SPLIT(word, '')) char
  LEFT JOIN map
  ON char = latin
));

WITH yourTable AS (
  SELECT 'Séries' AS word UNION ALL SELECT 'Series' UNION ALL
  SELECT 'brasília' UNION ALL SELECT 'Niño' UNION ALL SELECT 'aperçu' 
), inputs AS (
  SELECT 'series' AS input UNION ALL SELECT 'Brasilia' UNION ALL
  SELECT 'nino' UNION ALL SELECT 'apercu'
)
SELECT input, word AS found_word 
FROM yourTable CROSS JOIN inputs
WHERE REGEXP_CONTAINS(word,  latin2accents(input)) = TRUE
ORDER BY input, word

输出是(我认为这是你想要的)

input       found_word   
-----       ----------
Brasilia    brasília     
apercu      aperçu   
nino        Niño     
series      Series   
series      Séries   
于 2017-04-05T02:18:27.337 回答