如何使用 NLP 通过语义相似性对多个哨人进行分组



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

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

我已经找到了以下描述如何预处理文本以进行分析的文章: 如何在 Python 中使用 NLP(自然语言处理(模型开发释义工具

我还发现了关于SO的其他问题,但它们都涉及单词相似性,两个句子的比较或使用参考含义的比较。

  • 这个使用参考含义
  • 这个指的是两句话的比较

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

这个答案说它不可能完成,但它很古老,也许有人知道一些新的东西。

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

感谢Anurag Wagh的建议,我想通了。 我使用了这个关于 gensim 以及如何以多种方式使用它的教程。

第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)

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

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

doc2vec 在这里会有所帮助