Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文本相似性方法不反映;“真正的”;文本间的相似性_Python_Nlp_Text Mining_Similarity_Sentence Similarity - Fatal编程技术网

Python 文本相似性方法不反映;“真正的”;文本间的相似性

Python 文本相似性方法不反映;“真正的”;文本间的相似性,python,nlp,text-mining,similarity,sentence-similarity,Python,Nlp,Text Mining,Similarity,Sentence Similarity,我将CVs(.txt文件中已删除停止词)的内容与非常紧凑的工作描述(JD)进行比较,如下所示: 项目管理, 领导层, 出售, 液 市场营销 CVs大约有600个单词,JDs只有上面突出显示的单词 我目前遇到的问题是,当我对它应用相似性度量时,我会得到混淆的结果,我确信这是由于我缺乏知识造成的。例如,我的简历编号1包含JD中的所有单词,有时会重复多次。我还有CV 2,与JD相比,它只包含单词project。尽管如此,当我应用余弦相似性、差异、jaccard距离和编辑距离时,所有这些度量都会使CV2

我将CVs(.txt文件中已删除停止词)的内容与非常紧凑的工作描述(JD)进行比较,如下所示:

项目管理, 领导层, 出售, 液 市场营销

CVs大约有600个单词,JDs只有上面突出显示的单词

我目前遇到的问题是,当我对它应用相似性度量时,我会得到混淆的结果,我确信这是由于我缺乏知识造成的。例如,我的简历编号1包含JD中的所有单词,有时会重复多次。我还有CV 2,与JD相比,它只包含单词project。尽管如此,当我应用余弦相似性、差异、jaccard距离和编辑距离时,所有这些度量都会使CV2和JD之间的相似度更高,这对我来说很奇怪,因为它们之间只有一个单词相等,而CV1拥有JD中的所有单词

我使用了错误的方法来评估相似性?我很抱歉,如果这是一个天真的问题,我是一个编程初学者

代码遵循

Diff

    from difflib import SequenceMatcher
    def similar(a, b):
        return SequenceMatcher(None, a, b).ratio()
    similar('job.txt','LucasQuadros.txt')
    0.43478260869565216
    similar('job.txt','BrunaA.Fernandes.txt')
    0.2962962962962963
余弦

    from sklearn.feature_extraction.text import TfidfVectorizer
    document= ('job.txt','LucasQuadros.txt','BrunaA.Fernandes')
    tfidf = TfidfVectorizer().fit_transform(document)
    matrix= tfidf * tfidf.T
    matrix.todense()
    matrix([[1.        , 0.36644682, 0.        ],
    [0.36644682, 1.        , 0.        ],
    [0.        , 0.        , 1.        ]])
    import nltk
    w1= ('job.txt')
    w2= ('LucasQuadros.txt')
    w3= ('BrunaA.Fernandes.txt')
    nltk.edit_distance(w1,w2)
    11
    nltk.edit_distance(w1,w3)
    16
    import nltk
    a1= set('job.txt')
    a2= set('LucasQuadros.txt')
    a3= set('BrunaA.Fernandes.txt')
    nltk.jaccard_distance(a1,a2)
    0.7142857142857143
    nltk.jaccard_distance(a1,a3)
    0.8125
编辑距离

    from sklearn.feature_extraction.text import TfidfVectorizer
    document= ('job.txt','LucasQuadros.txt','BrunaA.Fernandes')
    tfidf = TfidfVectorizer().fit_transform(document)
    matrix= tfidf * tfidf.T
    matrix.todense()
    matrix([[1.        , 0.36644682, 0.        ],
    [0.36644682, 1.        , 0.        ],
    [0.        , 0.        , 1.        ]])
    import nltk
    w1= ('job.txt')
    w2= ('LucasQuadros.txt')
    w3= ('BrunaA.Fernandes.txt')
    nltk.edit_distance(w1,w2)
    11
    nltk.edit_distance(w1,w3)
    16
    import nltk
    a1= set('job.txt')
    a2= set('LucasQuadros.txt')
    a3= set('BrunaA.Fernandes.txt')
    nltk.jaccard_distance(a1,a2)
    0.7142857142857143
    nltk.jaccard_distance(a1,a3)
    0.8125
Jaccard距离

    from sklearn.feature_extraction.text import TfidfVectorizer
    document= ('job.txt','LucasQuadros.txt','BrunaA.Fernandes')
    tfidf = TfidfVectorizer().fit_transform(document)
    matrix= tfidf * tfidf.T
    matrix.todense()
    matrix([[1.        , 0.36644682, 0.        ],
    [0.36644682, 1.        , 0.        ],
    [0.        , 0.        , 1.        ]])
    import nltk
    w1= ('job.txt')
    w2= ('LucasQuadros.txt')
    w3= ('BrunaA.Fernandes.txt')
    nltk.edit_distance(w1,w2)
    11
    nltk.edit_distance(w1,w3)
    16
    import nltk
    a1= set('job.txt')
    a2= set('LucasQuadros.txt')
    a3= set('BrunaA.Fernandes.txt')
    nltk.jaccard_distance(a1,a2)
    0.7142857142857143
    nltk.jaccard_distance(a1,a3)
    0.8125

正如你们所看到的,“LucasQuadros.txt”(CV1)与“job.txt”(工作描述)有更高的相似性,尽管它只包含工作描述中的一个词。

我已经意识到我做错了什么。当我编写下面的代码行时,我比较的是“job.txt”和“LucasQuadros.txt”,而不是文档本身

similar('job.txt','LucasQuadros.txt')
要改变这一点,我只需在代码中包含.read函数,如下所示:

jd = open('job.txt')
jd = jd.read()
cv1= ('LucasQuadros.txt')
cv1= cv1.read()

from difflib import SequenceMatcher
def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

similar(jd, cv1)
0.0
similar(jd,cv2)
0.007104795737122558
现在相似性是正确的。
正如我上面所说的,这是一个相当初级的错误。

发布代码片段