检查字符串中的单词是否相似?(例如,书和小溪与书和运气)



我编写了一段代码,可以比较两个字符串来找到匹配的单词。现在我希望能够找到相对接近的单词。例如,书和小溪是相似的,而书和运气则不是。我该怎么做?

我想把每个单词分成几个字符,然后计算所说字符的频率?现在,匹配的单词给出值0。否则,会给出2,但我想扩展该部分,以执行上面描述的操作。

for i in range(0, out.shape[0]):  # from 0 to total number of rows out.shape[0] is rows - out.shape[1] is columns
for word in refArray:  # for each word in the samplearray
#out.ix[i, str(word)] = out.index[i].count(str(word))
if out.index[i].count(str(word)) == 1:
out.ix[i, str(word)] = 0 
else:
out.ix[i, str(word)] = 2

您需要计算编辑距离。https://en.wikipedia.org/wiki/Edit_distance

$ pip3 search edit | grep distance
edith (0.1.0a1)            - Edit-distanc implementation with edit-path retrieval
string-distance (1.0.0)    - Minimum Edit Distance
subdist (0.2.1)            - Substring edit distance
editdist (0.1)             - Calculate Levenshtein's edit distance
leven (1.0.4)              - Levenshtein edit distance library

我在浏览谷歌后使用了nltk。在这个阶段,我只需要比较简单的单词,就可以了解我的程序的基本功能。稍后会考虑更复杂的解决方案。感谢您的帮助。

import nltk
nltk.edit_distance("word1", "word2")

来源:https://datascience.stackexchange.com/a/12583/56244

相关内容

最新更新