String 如何查找删除非ASCII字符的字符串副本
我有一个书名表,其中大多数都是不同版本的重复多次。许多标题被错误地导入了缺少的非ASCII字符,即“La m?tamorphose”变为“La m?tamorphose”,有时é变为空格或从字符串中删除 桌子String 如何查找删除非ASCII字符的字符串副本,string,postgresql,non-ascii-characters,String,Postgresql,Non Ascii Characters,我有一个书名表,其中大多数都是不同版本的重复多次。许多标题被错误地导入了缺少的非ASCII字符,即“La m?tamorphose”变为“La m?tamorphose”,有时é变为空格或从字符串中删除 桌子 editionid | bookid | title -------------------------------------------- 1 | 1 | Elementarne čestice 2 | 1 | Elementarne
editionid | bookid | title
--------------------------------------------
1 | 1 | Elementarne čestice
2 | 1 | Elementarne ?estice
3 | 1 | Elementarne estice
4 | 1 | Las partículas elementales
5 | 2 | Schöne neue Welt
6 | 2 | Sch ne neue Welt
我想通过剥离标题的非ASCIIs并与同一本书的其他标题进行比较来识别不正确的标题。如果有一个匹配,我发现一个有缺陷的标题
结果:
o.title (flawed) | e.title (good)
-----------------------------------
Elementarne ?estice | Elementarne čestice
Elementarne estice | Elementarne čestice
Sch ne neue Welt | Schöne neue Welt
这个表相当大,但因为我只需要在性能不是关键的情况下执行此操作
我的做法:
select distinct on (o.editionid) o.title, e.title
from editions o
inner join editions e on (o.bookid = e.bookid)
where o.bookid between 1 and 1000
and e.title !~ '^[ -~]*$' -- only for performance
and ((
e.title like '%Þ%' and (o.title = regexp_replace(e.title, '[Þ]', '?') or o.title = regexp_replace(e.title, '[Þ]', ' ') or o.title = regexp_replace(e.title, '[Þ]', ''))
) or (
e.title like '%ß%' and (o.title = regexp_replace(e.title, '[ß]', '?') or o.title = regexp_replace(e.title, '[ß]', ' ') or o.title = regexp_replace(e.title, '[ß]', ''))
) or (
e.title like '%à%' and (o.title = regexp_replace(e.title, '[à]', '?') or o.title = regexp_replace(e.title, '[à]', ' ') or o.title = regexp_replace(e.title, '[à]', ''))
.
.
.
))
到目前为止,这是可行的,但似乎不可能单独添加所有非ASCII字符。有没有人想到一种更通用的方法,一次覆盖所有非ASCII字符
第二,如果两个不同的角色被剥光,我不知道如何解决这个问题,这是行不通的
第三,但可能是不可能的-通常只有一些非ASCIIs被转换,但不是全部,即“Weiße Nächte”转换为“Wei e Nächte”-如果这些也能被覆盖,那就太好了。经过一些修改,最终并没有那么难:
select distinct on (o.editionid) o.title as flawed, e.title as good
from editions o
inner join editions e on (o.bookid = e.bookid)
where o.bookid between 0 and 10000
and e.title ~ '[^\x00-\x7F]'
and (
o.title = regexp_replace(e.title, '[^\x00-\x7F]', '?', 'g')
or o.title = regexp_replace(e.title, '[^\x00-\x7F]', ' ', 'g')
)
regexp\u replace(例如标题“[^\x00-\x7F]”、“?”、“g”)
是键,其中\x00-\x7F
都是非ASCII格式的Unicode字符,并且'g'
不断在同一字符串中搜索更多的命中率。经过一些修改后,最终没有那么难:
select distinct on (o.editionid) o.title as flawed, e.title as good
from editions o
inner join editions e on (o.bookid = e.bookid)
where o.bookid between 0 and 10000
and e.title ~ '[^\x00-\x7F]'
and (
o.title = regexp_replace(e.title, '[^\x00-\x7F]', '?', 'g')
or o.title = regexp_replace(e.title, '[^\x00-\x7F]', ' ', 'g')
)
regexp\u replace(例如标题“[^\x00-\x7F]”、“?”、“g”)
是键,其中\x00-\x7F
都是非ASCII格式的Unicode字符,并且'g'
不断在同一字符串中搜索更多的点击。您可能想要“编辑距离”的概念如上所述。这将是非常容易出错的,因为它不能区分ASCII与否。即例如,“示例”与“示例”之间的距离与“示例”与“示例”之间的距离相同,后者在不同语言中可能完全合法。您可能需要如上所述的“编辑距离”概念。这很容易出错,因为它无法区分ASCII与否。即例如,“示例”与“示例”之间的距离与“示例”与“示例”之间的距离相同,后者在不同语言中可能完全合法。