【问题标题】:How can I use NLP to group multiple senteces by semantic similarity如何使用 NLP 通过语义相似性对多个句子进行分组
【发布时间】:2020-06-06 07:02:53
【问题描述】:

我正在尝试提高不合格品管理计划的效率。基本上,我有一个包含大约几百行的数据库,每行使用文本字段描述不符合项。 文本以意大利语提供,我无法控制用户写的内容。 我正在尝试使用 NTLK 编写一个 python 程序来检测这些行中有多少行报告相同的问题,编写方式不同但内容相似。 比如下面的句子需要相关,置信度高

  • 我收到的比订购的少 10 件
  • 10 件未发货

我已经找到以下描述如何预处理文本以进行分析的文章: How to Develop a Paraphrasing Tool Using NLP (Natural Language Processing) Model in Python

我还发现了其他关于 SO 的问题,但它们都是指单词相似度、两句比较或使用参考意义进行比较。

就我而言,我没有参考资料,如果涉及类似问题,我有多个句子需要分组,所以我想知道这项工作是否甚至可以用脚本来完成。

This answer 说不能做但是太老了,也许有人知道一些新东西。

感谢所有可以帮助我的人。

【问题讨论】:

    标签: python nlp nltk sentence-similarity


    【解决方案1】:

    感谢 Anurag Wagh 的建议,我想通了。 我使用了this tutorial 来了解 gensim 以及如何以多种方式使用它。

    Chapter 18 符合我的要求,但在我的测试中,我发现了一种更好的方法来实现我的目标。

    Chatper 11 展示了如何构建 LDA 模型以及如何在一组文档中提取主要主题列表。

    这是我用于构建 LDA 模型的代码

    # Step 0: Import packages and stopwords
    from gensim.models import LdaModel, LdaMulticore
    import gensim.downloader as api
    from gensim.utils import simple_preprocess, lemmatize
    from nltk.corpus import stopwords
    from gensim import corpora
    import re
    import nltk
    import string
    import pattern
    import logging
    
    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s')
    logging.root.setLevel(level=logging.INFO)
    
    docs = [doc for doc in open('file.txt', encoding='utf-8')]
    
    import nltk
    import string
    import pattern
    
    # dictionary of Italian stop-words
    it_stop_words = nltk.corpus.stopwords.words('italian')
    it_stop_words = it_stop_words + [<custom stop words>]
    # Snowball stemmer with rules for the Italian language
    ita_stemmer = nltk.stem.snowball.ItalianStemmer()
    
    # the following function is just to get the lemma
    # out of the original input word
    def lemmatize_word(input_word):
        in_word = input_word
        word_it = pattern.it.parse(
            in_word, 
            tokenize=False,  
            tag=False,  
            chunk=False,  
            lemmata=True 
        )
        the_lemmatized_word = word_it.split()[0][0][4]
        return the_lemmatized_word
    
    # Step 2: Prepare Data (Remove stopwords and lemmatize)
    data_processed = []
    
    for doc in docs:
        word_tokenized_list = nltk.tokenize.word_tokenize(doc)
        word_tokenized_no_punct = [x.lower() for x in word_tokenized_list if x not in string.punctuation]
        word_tokenized_no_punct_no_sw = [x for x in word_tokenized_no_punct if x not in it_stop_words]
    
        word_tokenized_no_punct_no_sw_no_apostrophe = [x.split("'") for x in word_tokenized_no_punct_no_sw]
        word_tokenized_no_punct_no_sw_no_apostrophe = [y for x in word_tokenized_no_punct_no_sw_no_apostrophe for y in x]
        data_processed.append(word_tokenized_no_punct_no_sw_no_apostrophe)
    
    dct = corpora.Dictionary(data_processed)
    corpus = [dct.doc2bow(line) for line in data_processed]
    
    lda_model = LdaMulticore(corpus=corpus,
                             id2word=dct,
                             random_state=100,
                             num_topics=7,
                             passes=10,
                             chunksize=1000,
                             batch=False,
                             alpha='asymmetric',
                             decay=0.5,
                             offset=64,
                             eta=None,
                             eval_every=0,
                             iterations=100,
                             gamma_threshold=0.001,
                             per_word_topics=True)
    
    # save the model
    lda_model.save('lda_model.model')
    
    # See the topics
    lda_model.print_topics(-1)
    

    通过训练的模型,我可以获得每个新的不符合项的主题列表,并检测它是否与其他不符合项已经报告的内容有关

    【讨论】:

      【解决方案2】:

      也许将文档转换为向量以及计算两个向量之间的距离会有所帮助

      doc2vec 可以在这里提供帮助

      【讨论】:

        猜你喜欢
        • 2021-07-14
        • 2012-12-16
        • 2018-05-28
        • 2020-05-26
        • 2020-07-11
        • 1970-01-01
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多