NLTK路径_相似性给出值错误

2024-09-29 23:22:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在用NLTK做一个coursera作业,以查找两个文档之间的路径相似性,但被卡住了

import numpy as np
import nltk
from nltk.corpus import wordnet as wn
import pandas as pd

nltk.download('averaged_perceptron_tagger')
nltk.download('wordnet')
nltk.download('punkt')

def convert_tag(tag):
    """Convert the tag given by nltk.pos_tag to the tag used by wordnet.synsets"""
    
    tag_dict = {'N': 'n', 'J': 'a', 'R': 'r', 'V': 'v'}
    try:
        return tag_dict[tag[0]]
    except KeyError:
        return None


def doc_to_synsets(doc):
    """
    Returns a list of synsets in document.

    Tokenizes and tags the words in the document doc.
    Then finds the first synset for each word/tag combination.
    If a synset is not found for that combination it is skipped.

    Args:
        doc: string to be converted

    Returns:
        list of synsets

    Example:
        doc_to_synsets('Fish are nvqjp friends.')
        Out: [Synset('fish.n.01'), Synset('be.v.01'), Synset('friend.n.01')]
    """
    tokens=nltk.word_tokenize(doc)
    tokens=nltk.pos_tag(syn)
    
    updated_pos=[convert_tag(x[1]) for x in tokens]
    
    tokens=[(tokens[x][0],updated_pos[x]) for x in range(len(tokens))]
    
    
    
    ds=[wn.synsets(tokens[x][0],pos=tokens[x][1]) for x in range(len(tokens))]
    ds1=[]
    for x in range(len(tokens)):
        try:
            ds1.append(ds[x][0])
        except:
            continue
    return ds1
def similarity_score(s1, s2):
    """
    Calculate the normalized similarity score of s1 onto s2

    For each synset in s1, finds the synset in s2 with the largest similarity value.
    Sum of all of the largest similarity values and normalize this value by dividing it by the
    number of largest similarity values found.

    Args:
        s1, s2: list of synsets from doc_to_synsets

    Returns:
        normalized similarity score of s1 onto s2

    Example:
        synsets1 = doc_to_synsets('I like cats')
        synsets2 = doc_to_synsets('I like dogs')
        similarity_score(synsets1, synsets2)
        Out: 0.73333333333333339
    """
    maxscore=[]
    for x in s1:
        dis=[]
        for y in s2:
            dis.append(x.path_similarity(y))
            dis=[z for z in dis if z!=None]
        maxscore.append(max(dis))
    return sum(maxscore)/len(maxscore)
    
    
def document_path_similarity(doc1,doc2):
    """Finds the symmetrical similarity between doc1 and doc2"""

    synsets1 = doc_to_synsets(doc1)
    synsets2 = doc_to_synsets(doc2)

    return (similarity_score(synsets1, synsets2)+similarity_score(synsets2, synsets1))/ 2

 #This is a test Function to check wether the above funtion is correct or not
def test_document_path_similarity():
    doc1 = 'This is a function to test document_path_similarity.'
    doc2 = 'Use this function to see if your code in doc_to_synsets \
    and similarity_score is correct!'
    return document_path_similarity(doc1, doc2)

我的问题是这个函数不是返回浮点值&;它给出了以下值错误:

ValueError                                Traceback (most recent call last)
<ipython-input-61-6c20d7dcffc6> in <module>()
----> 1 test_document_path_similarity()

<ipython-input-60-9639d05f12da> in test_document_path_similarity()
      2     doc1 = 'This is a function to test document_path_similarity.'
      3     doc2 = 'Use this function to see if your code in doc_to_synsets     and similarity_score is correct!'
----> 4     return document_path_similarity(doc1, doc2)

<ipython-input-59-849dd19f38dc> in document_path_similarity(doc1, doc2)
     89     synsets2 = doc_to_synsets(doc2)
     90 
---> 91     return (similarity_score(synsets1, synsets2)+similarity_score(synsets2, synsets1))/ 2

<ipython-input-59-849dd19f38dc> in similarity_score(s1, s2)
     79             dis.append(x.path_similarity(y))
     80             dis=[z for z in dis if z!=None]
---> 81         maxscore.append(max(dis))
     82     return sum(maxscore)/len(maxscore)
     83 

ValueError: max() arg is an empty sequence

这基本上是因为synsets2[-1]和synsets1的每个synset之间的路径相似性为None值。 但根据说明不应该是这样的,我试了这么多时间,但不知道如何避免这个,得到一个浮点值

由于导师可能需要几天的时间才能做出回应,我来这里寻求帮助,如果可以的话,请调查一下。 编辑:这些是synsets2和synsets1

synsets2=[Synset('use.v.01'),
  Synset('function.n.01'),
  Synset('see.v.01'),
  Synset('code.n.01'),
  Synset('inch.n.01'),
  Synset('be.v.01'),
  Synset('correct.a.01')],


synsets1=[Synset('be.v.01'),
  Synset('angstrom.n.01'),
  Synset('function.n.01'),
  Synset('test.v.01')]

Tags: thetopathinfordoctagdocument
1条回答
网友
1楼 · 发布于 2024-09-29 23:22:28

您的代码看起来不错,只是您需要添加以下语句:if(dis): 因为您需要maxscore.append(max(dis))将仅在“dis”有值时计算,否则将不考虑它

因此,需要修改的代码部分如下所示:

&13; 第13部分,;
maxscore=[]
for x in s1:
    dis=[]
    for y in s2:
        dis.append(x.path_similarity(y))
        dis=[z for z in dis if z!=None]
    if(dis):
        maxscore.append(max(dis))
return sum(maxscore)/len(maxscore)
和#13;
和#13;

相关问题 更多 >

    热门问题