Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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 - Fatal编程技术网

Python 根据意义将字符串等同起来

Python 根据意义将字符串等同起来,python,nlp,Python,Nlp,在python中,有没有一种方法可以根据字符串的含义将它们等同起来,尽管它们并不相似。 比如说, 临时工。马克斯 最高环境温度 我尝试过使用fuzzyfuzzy和difflib,虽然它们通常使用令牌匹配来实现这一点,但当我对大量字符串设置阈值时,它们也会提供误报。 是否有其他使用NLP或Tokenizationn的方法,我在这里遗漏了这些方法 编辑: CO提供的答案确实解决了上述问题,但是否有任何方法可以使用关键字中的word2vec匹配特定子字符串? e、 g.键=最高温度 Sent=加利福尼

在python中,有没有一种方法可以根据字符串的含义将它们等同起来,尽管它们并不相似。 比如说,

  • 临时工。马克斯
  • 最高环境温度
  • 我尝试过使用fuzzyfuzzydifflib,虽然它们通常使用令牌匹配来实现这一点,但当我对大量字符串设置阈值时,它们也会提供误报。 是否有其他使用NLPTokenizationn的方法,我在这里遗漏了这些方法

    编辑: CO提供的答案确实解决了上述问题,但是否有任何方法可以使用关键字中的word2vec匹配特定子字符串? e、 g.键=最高温度 Sent=加利福尼亚州预计明天的最高环境温度为34度


    这里我想得到子串“最高环境温度”。有什么提示吗?

    正如您所说,像fuzzyfuzzy或difflib这样的包将受到限制,因为它们根据字符串的拼写而不是其含义来计算相似度

    您可以使用单词嵌入。单词嵌入是单词的向量表示,计算方式允许在一定程度上表示其含义

    有不同的方法生成单词嵌入,但最常见的方法是在一个或一组单词级NLP任务上训练神经网络,并使用倒数第二层作为单词的表示。这样,单词的最终表示应该已经积累了足够的信息来完成任务,并且这些信息可以被解释为单词含义的近似值。我建议您阅读一下Word2vec,它是一种使单词嵌入变得流行的方法,因为它简单易懂,但代表了单词嵌入的含义。两个词之间的相似性可以通过向量表示之间的余弦距离来计算

    当然,您不需要自己训练单词嵌入,因为存在大量的预训练向量(手套、word2vec、fasttext、spacy…)。选择使用哪种嵌入取决于观察到的性能,以及您对它们是否适合您想要执行的任务的理解。下面是一个示例,其中通过平均单词向量计算句子向量:

    # Importing spacy and fuzzy wuzzy
    import spacy
    from fuzzywuzzy import fuzz
    
    # Loading spacy's large english model
    nlp_model = spacy.load('en_core_web_lg')
    
    s1 = "temp. Max"
    s2 = "maximum ambient temperature"
    s3 = "the blue cat"
    
    doc1 = nlp_model (s1)
    doc2 = nlp_model (s2)
    doc3 = nlp_model (s3)
    
    
    # Word vectors (The document or sentence vector is the average of the word vectors it contains)
    print("Document vectors similarity between '{}' and '{}' is: {:.4f} ".format(s1, s2, doc1.similarity(doc2)))
    print("Document vectors similarity between '{}' and '{}' is: {:.4f}".format(s1, s3, doc1.similarity(doc3)))
    print("Document vectors similarity between '{}' and '{}' is: {:.4f}".format(s2, s3, doc2.similarity(doc3)))
    
    # Fuzzy logic
    print("Character ratio similarity between '{}' and '{}' is: {:.4f} ".format(s1, s2, fuzz.ratio(doc1, doc2)))
    print("Character ratio similarity between '{}' and '{}' is: {:.4f}".format(s1, s3, fuzz.ratio(doc1, doc3)))
    print("Character ratio similarity between '{}' and '{}' is: {:.4f}".format(s2, s3, fuzz.ratio(doc2, doc3)))
    
    
    这将打印:

    >>> Document vectors similarity between 'temp. Max' and 'maximum ambient temperature' is: 0.6432 
    >>> Document vectors similarity between 'temp. Max' and 'the blue cat' is: 0.3810
    >>> Document vectors similarity between 'maximum ambient temperature' and 'the blue cat' is: 0.3117
    
    >>> Character ratio similarity between 'temp. Max' and 'maximum ambient temperature' is: 28.0000 
    >>> Character ratio similarity between 'temp. Max' and 'the blue cat' is: 38.0000
    >>> Character ratio similarity between 'maximum ambient temperature' and 'the blue cat' is: 21.0000
    
    如您所见,与词向量的相似性更好地反映了文档含义的相似性

    然而,这实际上只是一个起点,因为可能会有很多警告。这里列出了一些您应该注意的事项:

    • 单词(和文档)向量本身并不代表单词(或文档)的含义,它们是一种近似的方法。这意味着它们会在某一点上受到限制,你不能想当然地认为它们会让你区分语言的所有细微差别
    • 我们所期望的两个单词/句子之间的“意义相似性”根据我们的任务而有所不同。例如,“最高温度”和“最低温度”之间的“理想”相似性是什么?高是指同一概念的极端状态,低是指同一概念的相反状态?通过单词嵌入,这些句子通常会有很高的相似性,因为“最大值”和“最小值”经常出现在相同的上下文中,这两个单词会有相似的向量
    • 在给出的示例中,0.6432仍然不是很高的相似性。这可能是因为示例中使用了缩写词。根据单词嵌入的生成方式,它们可能无法很好地处理缩写。一般来说,最好在语法和语法上正确地输入NLP算法。根据数据集的外观和您对它的了解,事先进行一些清理可能非常有用。下面是一个语法正确的句子示例,可以更好地突出意思上的相似性:

    无论如何,单词嵌入可能是您正在寻找的,但是您需要花时间来了解它们。我建议您阅读有关word(以及句子和文档)嵌入的内容,并尝试使用不同的预训练向量,以便更好地理解如何将它们用于您的任务。

    因此,我尝试实现word2vec和doc2vec,在这种情况下,它似乎工作得很好,谢谢。
    
    s1 = "The president has given a good speech"
    s2 = "Our representative has made a nice presentation"
    s3 = "The president ate macaronis with cheese"
    
    doc1 = nlp_model (s1)
    doc2 = nlp_model (s2)
    doc3 = nlp_model (s3)
    
    # Word vectors
    print(doc1.similarity(doc2))
    >>> 0.8779 
    print(doc1.similarity(doc3))
    >>> 0.6131
    print(doc2.similarity(doc3))
    >>> 0.5771