假设我有一堆带有噪声的相似字符串,主要是错误连接/断开连接的单词。像这样:
"Once more unto the breach, dear friends. Once more!"
"Once more unto the breach , dearfriends. Once more!"
"Once more unto the breach, de ar friends. Once more!"
"Once more unto the breach, dear friends. Once more!"我如何才能将每个人规范化到同一组单词中?即
["once" "more" "unto" "the" "breach" "dear" "friends" "once" "more"]谢谢!
发布于 2012-11-22 03:02:39
这里有几点建议。我认为您最终将不得不编写一组例程/函数来修复您遇到的所有各种类型的异常。
好消息是,您可以逐步添加到您的“修复”集,并不断改进解析器。
我不得不做一些类似的事情,我发现this post by Peter Norvig非常有用。(请注意,它是用Python编写的。)
这个函数特别具有您需要的思想:拆分、删除、转置和插入不规则的单词以“更正”它们。
def edits1(word):
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [a + b[1:] for a, b in splits if b]
transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b]
inserts = [a + c + b for a, b in splits for c in alphabet]
return set(deletes + transposes + replaces + inserts)上面是Norvig的spelling corrector中的一个片段
即使您不能按原样使用代码,其核心思想也适用于您的情况:您使用一个记号(" word "),这是您的情况中的不规则单词,尝试不同的调整,看看它是否属于已知和接受的单词的大字典。
希望这能有所帮助。
https://stackoverflow.com/questions/13494836
复制相似问题