【发布时间】:2014-02-19 01:05:16
【问题描述】:
我有一个系列类型对象,我必须应用一个函数,该函数使用二元组来纠正单词,以防它与另一个单词一起出现。我创建了一个 bigrams list ,根据频率对其进行排序(最高优先)并将其命名为 fdist 。
bigrams = [b for l in text2 for b in zip(l.split(" ")[:-1], l.split(" ")[1:])]
freq = nltk.FreqDist(bigrams) #computes freq of occurrence
fdist = freq.keys() # sorted according to freq
接下来,我创建了一个函数,它接受每一行(“或句子”、“列表的对象”)并使用二元组来决定是否进一步纠正它。
def bigram_corr(line): #function with input line(sentence)
words = line.split() #split line into words
for word1, word2 in zip(words[:-1], words[1:]): #generate 2 words at a time words 1,2 followed by 2,3 3,4 and so on
for i,j in fdist: #iterate over bigrams
if (word2==j) and (jf.levenshtein_distance(word1,i) < 3): #if 2nd words of both match, and 1st word is at an edit distance of 2 or 1, replace word with highest occurring bigram
word1=i #replace
return word1 #return word
问题是整个句子只返回一个单词,例如:
“Lts go twards the east is” 替换为 Lets 。看起来进一步的迭代不起作用。
word1, word2 的 for 循环是这样工作的:
第一次迭代中的“lts go”,最终将被“lets”取代,因为“go”出现的频率更高
在第二次迭代中“走向”。
在第 3 次迭代中“朝向”……以此类推。
有一个小错误,我无法弄清楚,请帮忙。
【问题讨论】:
-
请澄清。
bigram_corr("Lets go towards the east is")的预期结果是什么? -
已编辑,请检查。我需要返回整个句子。
-
你过早地回来了。
-
读者可能对导致创建此帖子的聊天讨论感兴趣。那里可能还有其他线索。 chat.stackoverflow.com/transcript/message/14784810#14784810
标签: python return-value levenshtein-distance function n-gram