【问题标题】:Getting error TypeError: unorderable types: NoneType() > float() at line which is in bold of the code below收到错误 TypeError: unorderable types: NoneType() > float() at line which is in粗体下面的代码
【发布时间】:2017-09-19 12:05:57
【问题描述】:
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))

【问题讨论】:

  • synset.path_similarity(ss) 有时是None,所以max() 调用失败,错误信息很清楚。
  • 那么在这种情况下我应该怎么做?

标签: python sentence-similarity


【解决方案1】:

正如@Chris_Rands 所说,您的问题是函数path_similarity() 可以返回None,然后max() 调用失败。这是一个验证这种情况何时发生的问题。 一个可能的解决方案是创建一个列表,simlist,不包括 path_similarity() 中的 None 值。 如果simlist 为空,则跳过当前迭代,如果不是,则调用 max() 并继续剩余的迭代。

# 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

【讨论】:

    【解决方案2】:

    对于第一句话中的每个单词

        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)
    

    检查是否可以计算相似度 分数 += best_score 计数 += 1

     if count == 0:
        return 0
    
     Average the values
        score /= count
        return score
    

    它正在工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多