Python 获取错误类型错误:无序类型:NoneType()>;float()位于下面代码的粗体行
正如@Chris\u Rands所述,您的问题是函数Python 获取错误类型错误:无序类型:NoneType()>;float()位于下面代码的粗体行,python,sentence-similarity,Python,Sentence Similarity,正如@Chris\u Rands所述,您的问题是函数path\u similarity()可能返回None,然后max()调用失败。这是一个核实案件何时发生的问题。 一种可能的解决方案是创建一个列表,simlist从path\u similarity()中排除None值。 如果simlist为空,则跳过当前迭代;如果不是,则调用max()并继续迭代的其余部分 from nltk import word_tokenize, pos_tag from nltk.corpus import wordn
path\u similarity()
可能返回None
,然后max()
调用失败。这是一个核实案件何时发生的问题。
一种可能的解决方案是创建一个列表,simlist
从path\u similarity()
中排除None
值。
如果simlist
为空,则跳过当前迭代;如果不是,则调用max()并继续迭代的其余部分
from nltk import word_tokenize, pos_tag
from nltk.corpus import wordnet as wn
def penn_to_wn(tag):
""" Convert between a Penn Treebank tag to a simplified Wordnet tag """
if tag.startswith('N'):
return 'n'
if tag.startswith('V'):
return 'v'
if tag.startswith('J'):
return 'a'
if tag.startswith('R'):
return 'r'
return None
def tagged_to_synset(word, tag):
wn_tag = penn_to_wn(tag)
if wn_tag is None:
return None
try:
return wn.synsets(word, wn_tag)[0]
except:
return None
def sentence_similarity(sentence1, sentence2):
""" compute the sentence similarity using Wordnet """
# Tokenize and tag
sentence1 = pos_tag(word_tokenize(sentence1))
sentence2 = pos_tag(word_tokenize(sentence2))
# Get the synsets for the tagged words
synsets1 = [tagged_to_synset(*tagged_word) for tagged_word in sentence1]
synsets2 = [tagged_to_synset(*tagged_word) for tagged_word in sentence2]
# Filter out the Nones
synsets1 = [ss for ss in synsets1 if ss]
synsets2 = [ss for ss in synsets2 if ss]
score, count = 0.0, 0
# For each word in the first sentence
for synset in synsets1:
# Get the similarity value of the most similar word in the other sentence
**best_score = max([(synset.path_similarity(ss)) for ss in synsets2])**
# Check that the similarity could have been computed
if best_score is not None:
score += best_score
count += 1
# Average the values
score /= count
return score
if __name__ == '__main__':
sentences = [
'Password should not be less than 8 characters.',
'The user should enter valid user name and password.',
'User name should not have special characters.',
'Datta passed out from IIT',
]
focus_sentence = 'The user should enter valid user name and password and password should have greater than or equal to 8 characters.'
for sentence in sentences:
print(sentence_similarity(focus_sentence, sentence))
第一句话中的每个单词
# For each word in the first sentence
for synset in synsets1:
# Get the similarity value of the most similar word in the other sentence
simlist = [synset.path_similarity(ss) for ss in synsets2 if synset.path_similarity(ss) is not None]
if not simlist:
continue;
best_score = max(simlist)
# Check that the similarity could have been computed
score += best_score
count += 1
if count == 0:
return 0
# Average the values
score /= count
return score
检查是否可以计算相似性
分数+=最佳分数
计数+=1
for synset in synsets1:
Get the similarity value of the most similar word in the other sentence
simlist = [synset.path_similarity(ss) for ss in synsets2 if synset.path_similarity(ss) is not None]
if not simlist:
continue;
best_score = max(simlist)
它正在工作
synset。路径相似性(ss)
有时是None
,因此max()
调用失败,错误消息非常清楚。那么在这种情况下,我应该采取什么方法?
if count == 0:
return 0
Average the values
score /= count
return score