【发布时间】:2019-09-20 11:40:54
【问题描述】:
我有下面这样的句子
mainsentence="My words aren't available give didn't give apple and did happening me"
stopwords=['are','did','word', 'able','give','happen']
如果任何单词与中间的单词匹配,则要删除(例如:“word”应匹配“words”并将其删除,“did”应匹配“did't”并将其删除,“able”应删除“available” " 因为 'able' 词在 'available' 中
finalsentence="My apple and me"
用下面的代码试过了,但是
querywords = mainsentence.split()
resultwords = [word for word in querywords if word.lower() not in stopwords]
result = ' '.join(resultwords)
print(result)
但它只适用于完全匹配。
请帮帮我。
【问题讨论】:
-
这里的问题是你想要部分匹配,但
a将是你的大部分单词的部分匹配。 -
另外,
happen应该在finalsentence中 -
@tituszban:更正了问题。
-
听起来您需要检查的不是您的单词列表,而是您的单词列表的同义词列表。有几种方法可以做到这一点,一种是使用 PyDictionary:pypi.org/project/PyDictionary
标签: python python-3.7