我正在Python 3.4.3上创建代码。我有一个语言程序。我的代码的这部分必须删除下一个单词,如果它是前一个单词的同义词。首先,我们必须为每个单词创建一个同义词列表。然后我们将所有列表转换为集合。但最终,我们必须比较我们的列表以检查它们是否具有相同的同义词。我不知道如何比较它们。如果下一个有同义词,我们必须保留一个单词。
from nltk.corpus import wordnet
text = ['','','']
text4 = []
def f4(text):
global text4
synonyms = []
for sentence in text:
d = ' '
sentence = sentence.split(d)
for word in sentence:
syn = []
for syn in wordnet.synsets(word):
for lemma in syn.lemmas():
syn.append(lemma.name())
synonyms.append(syn)
synonyms2 = []
for x in synonyms:
x = set(x)
synonyms2.append(x)
答案 0 :(得分:1)
我的代码必须删除下一个单词,如果它是前一个单词的同义词。
我建议使用不同的算法。这是一个例子:
text = 'run race stroll rush nice lovely mean kind' # example text
synonyms = [] # contains a list of synonym lists
synonyms.append( ['run', 'race', 'rush'] ) # run synonyms
synonyms.append( ['nice', 'lovely', 'kind'] ) # nice synonyms
def in_synonyms(list_of_synonym_lists, word):
""" Returns index of synonym list the word is in; -1 if isn't found. """
for index, synonym_list in enumerate(list_of_synonym_lists):
if word in synonym_list:
return index
return -1
# The algorithm
split_text = text.split()
index = 1
while index < len(split_text):
if in_synonyms(synonyms, split_text[index]) != -1: # if word is in any synonyms list
if in_synonyms(synonyms, split_text[index]) == in_synonyms(synonyms, split_text[index-1]):
# if word before is in the same synonyms list as current we delete the current
# one and start over again
del(split_text[index])
index = 1 # restart the algorithm
else:
index += 1 # continue on forward
text = ' '.join(split_text)
此代码:
我还没有测试过,但我希望你能得到这个想法。
答案 1 :(得分:1)
如果你想过滤出重复的词,重言式,前面词的同义词:
CompletableFuture
您可以在列表理解中执行此操作:
<T, U> CompletableFuture<U> fmap(Function<T, U> f, CompletableFuture<T> m) {
return m.thenCompose(x -> CompletableFuture.completedFuture(f.apply(x)));
}
<T> CompletableFuture<T> join(CompletableFuture<CompletableFuture<T>> n) {
return n.thenCompose(x -> x);
}