1

我修改了一个 Python 代码来创建一个简单的字符串相似度。

但是,我想做的是用户输入,我希望第二个用户输入(单词)包含单词列表,以便我可以在单词之间进行比较。

    '''
Input the English words in w1, and
the translated Malay words in the list
'''
w1 = raw_input("Enter the English word: ")
words = raw_input("Enter all the Malay words: ")
## The bits im not sure what to code
wordslist = list(words)

for w2 in wordslist:
    print(w1 + ' --- ' + w2)
    print(string_similarity(w1, w2))
    print

当我输入时,它似乎与整个 'w1' 输入的字符串相似,所有单个字符都在 'words' 输入中。我想要的只是例如

w1 = 英国单词 = United Kingdom、United Kingdoms、United States、Kingdmo。

然后它在哪里测量

United Kingdom --- United Kingdom
United Kingdom --- United Kingdoms
United Kingdom --- United Sates
United Kingdom --- Kingdmo

等等。

谢谢你的帮助!

4

1 回答 1

1

您可以str.split用来获取单词列表:

>>> strs = "United Kingdom, United Kingdoms, United States, Kingdmo"
>>> strs.split(",")
['United Kingdom', ' United Kingdoms', ' United States', ' Kingdmo']

帮助str.split:_

>>> str.split?
Namespace:  Python builtin
Docstring:
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.
于 2013-05-08T01:49:33.263 回答