gab*_*lly 7 search text natural-language pattern-matching
我有一个名称文件,我想在其中进行搜索,而不是太在意我是否正确拼写了名称(我正在搜索的名称)。我知道它grep
有相当多的功能可以在文件或流中搜索大量类似的字符串,但据我所知,它没有纠正拼写错误的功能,即使有,因为这些是人名,在标准字典中找不到。
也许我可以把我的名字文件做成一个特殊的字典,然后使用一些标准的拼写检查工具?在这个应用程序中特别重要的是匹配发音相似的单词的能力。
例如:"jacob"
应该返回"Jakob"
。如果语言间的相似性也被考虑在内,那就更好了,这样"miguel"
应该匹配"Michael"
。
这是已经实施的东西,还是我必须自己构建?
@manatwork 说得对,soundex 可能是您正在寻找的工具。
使用 CPAN 安装 perl Soundex 模块:
$ sudo cpan Text::Soundex
CPAN: Storable loaded ok (v2.27)
....
Text::Soundex is up to date (3.04).
Run Code Online (Sandbox Code Playgroud)
制作一个充满名称的文件以进行测试调用 names.txt
jacob
Jakob
miguel
Michael
Run Code Online (Sandbox Code Playgroud)
现在使用 Soundex 模块的 perl 脚本, soundslike.pl
#!/usr/bin/perl
use Text::Soundex;
open(FH, 'names.txt');
$targetSoundex=soundex($ARGV[0]);
print "Target soundex of $ARGV[0] is $targetSoundex\n";
while(<FH>) {
chomp;
print "Soundex of $_ is ".soundex($_);
if($targetSoundex eq soundex($_)) {
print " (match).\n";
}else {
print " (no match).\n";
}
}
close(FH);
Run Code Online (Sandbox Code Playgroud)
使其可执行并运行一些示例:
$ chmod +x soundslike.pl
$ ./soundslike.pl michael
Target soundex of michael is M240
Soundex of jacob is J210 (no match).
Soundex of Jakob is J210 (no match).
Soundex of miguel is M240 (match).
Soundex of Michael is M240 (match).
$ ./soundslike.pl jagub
Target soundex of jagub is J210
Soundex of jacob is J210 (match).
Soundex of Jakob is J210 (match).
Soundex of miguel is M240 (no match).
Soundex of Michael is M240 (no match).
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1202 次 |
最近记录: |