【问题标题】:Topic modeling with Spacy - not making very good predictions使用 Spacy 进行主题建模 - 没有做出很好的预测
【发布时间】:2021-02-21 10:05:44
【问题描述】:

我正在处理一个主题建模任务,我正在收集人们的反馈(文本)并尝试从中提取重要的主题。

反馈很短,我不知道这是否是我们的问题所在。下面是我的代码,有什么明显的我遗漏了吗?

我正在删除停用词、词形还原、仅保留名词并删除停用词。但是我将这些传递到模型中,它并没有像我希望的那样工作

其中一个大问题是语义,客户可以用不同的方式指代同一个概念:商店、精品店、商店、超市等……它们都指的是商店,但 LDA 认为这些是不同的概念并将它们转储到不同的主题中,即使“我爱商店”和“我爱商店”是同一个陈述。

import spacy
import pandas as pd
from textblob import TextBlob

#set display options
pd.set_option('display.max_colwidth', 0)
pd.set_option('display.max_rows', 0)

#ingest data
df = pd.read_csv('surv.csv')

#import spacy language library and stopword dictionary
nlp = spacy.load('en_core_web_sm')
all_stopwords = nlp.Defaults.stop_words

#Limit DF to columns of interest and drop nulls
responses = df[['Comment', 'score']]
responses = responses.dropna()

#lemmatize the strings
def cleanup(row):
    comment = row['Comment']
    comment = nlp(comment)
    sent = []
    for word in comment:
        sent.append(word.lemma_)    
    return " ".join(sent)

#keep only nouns
def only_nouns(row):
    comment = row['nostops']
    blob = TextBlob(comment)
    x = blob.noun_phrases
    return " ".join(x)

def pos(row):
    comment = row['nostops']
    comment = nlp(comment)
    nouns = []
    i=0
    while i < len(comment)-1:
        if comment[i].pos_ == 'NOUN':
            nouns.append(comment[i])
        i=i+1
    return nouns
        
#remove the stop words
def remove_stops(row):
    comment = row['Comment']
    comment = comment.split(' ')  
    rem = []
    for word in comment:
        if word not in all_stopwords:
            rem.append(word)
    return " ".join(rem)

#What entities are defined in the document
def split_entities(row):
    comment = row['Comment']
    comment = nlp(comment)
    entities = []
    for ent in comment.ents:
        entities.append(ent)
    return entities          

#Call functions
responses['lemmas'] = responses.apply(cleanup,axis=1)            
responses['nostops'] = responses.apply(remove_stops,axis=1)
responses['nouns'] = responses.apply(pos, axis=1)
responses['nouns2'] = responses.apply(only_nouns, axis=1)
responses['entities'] = responses.apply(split_entities,axis=1)


from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
cv = CountVectorizer(max_df=0.9, min_df=2, stop_words='english') 
document_term_matrix = cv.fit_transform(responses['nouns'])
lda = LatentDirichletAllocation(n_components=4, random_state=42)
lda.fit(document_term_matrix)
topic_results = lda.transform(document_term_matrix) 

【问题讨论】:

    标签: python-3.x nlp spacy


    【解决方案1】:

    一般建议:您是否尝试过添加TF-IDF in sklearn?,这是一种基于单词在文档中和跨文档出现的频率来衡量单词的通用方法,它提高了 LDA 输出的质量。您可以将它与“CountVectorizer”一起添加。这是来自sklearn docs 的一个很好的完整示例。

    具体建议对于您希望将其视为同义词(“商店、精品店、商店、超市”)的问题:我想我会添加一个预处理步骤来替换所有这些单独的词中具有完全相同的标记(例如,将所有出现的“商店、精品店、商店、超市”转换为“商店”)。它需要手动创建同义词列表,但这是解决问题的简单方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 2020-07-07
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多