我有一个TSV(制表符分隔值)文件,我需要拼写检查拼写错误和组合词(即'我爱你'和'Iloveyou')。
我在我的机器上安装了Aspell,可以使用aspell()函数在R中运行它。
files <- "train2.tsv"
res <- aspell(files)
str(res)
summary(res)
但是,在R中运行它的输出只是拼写错误的单词列表和可能的建议。
> summary(res)
Possibly mis-spelled words:
[1] "amant" "contaneir" "creat" "ddition" "EssaySet" "EssayText" "experiament" "expireiment" "expirement"
[10] "Fipst" "infomation" "Inorder" "measureing" "mintued" "neccisary" "officialy" "renuminering" "rinsen"
[19] "sticlenx" "sucessfully" "tipe" "vineager" "vinigar" "yar"
> str(res)
Classes ‘aspell’ and 'data.frame': 27 obs. of 5 variables:
$ Original : chr "EssaySet" "EssayText" "expirement" "expireiment" ...
$ File : chr "train2.tsv" "train2.tsv" "train2.tsv" "train2.tsv" ...
$ Line : int 1 1 3 3 3 3 3 3 6 6 ...
$ Column : int 4 27 27 108 132 222 226 280 120 156 ...
$ Suggestions:List of 27
..$ : chr "Essay Set" "Essay-Set" "Essayist" "Essays" ...
..$ : chr "Essay Text" "Essay-Text" "Essayist" "Sedatest" ...
..$ : chr "experiment" "excrement" "excitement" "experiments" ...
..$ : chr "experiment" "experiments" "experimenter" "excrement" ...
..$ : chr "Amandy" "am ant" "am-ant" "Amanda" ...
..$ : chr "year" "ya" "Yard" "yard" ...
是否有办法让aspell(或任何其他拼写检查程序)自动纠正拼写错误的单词?
答案 0 :(得分:8)
看起来您可以执行以下操作:
s = load_up_users_dictionary()
for word in text_to_check:
if word not in s:
new_words = s.suggest( word )
replace_incorrect_word( word, new_words[0] )#Pick the first word from the returned list.
快速浏览文档,看起来就像自动使用建议的正确拼写一样。
http://0x80.pl/proj/aspell-python/index-c.html
编辑: 意识到你可能不会在寻找python代码,但这是用python做最简单的方法,因为问题是用python标记的。可能有一种更有效的方法,但它已经很晚了,首先想到了这一点。