二、基于中文的主题分析

2021-07-27  本文已影响0人  许志辉Albert

和拉丁语系不同,亚洲语言是不用空格分开每个有意义的词,而当我们进行自然语言处理的时候,大部分的情况下,词汇是我们对句子和文章理解的基础,因此需要一个额外的分词工具把完整的文本分解成粒度更细的词

1.1 关键词抽取

1.1.1 基于TF-IDF算法的关键词抽取

import jieba.analyse
jieba.analyse.extract_tags(sentence , topK=20,withWeight = False , allowPOS = ())
topK为返回几个TF-IDF权重最大的词,默认为20
withWeight 为是否一并返回关键权重值,默认为False
allowPOS 仅包括制定词性的词,默认值为空,即不筛选

import jieba.analyse as analyse
import pandas as pd
df = pd.read_csv('./origin_data/technology_news.csv',encoding = 'utf-8')
df = df.dropna()
lines = df.content.values.tolist()
content = ' '.join(lines)
keywords = analyse.extract_tags(content , topK = 30 , withWeight = False , allowPOS=())
import jieba.analyse as analyse
import pandas as pd
df = pd.read_csv('./origin_data/military.csv',encoding = 'utf-8')
df = df.dropna()
lines = df.content.values.tolist()
content = " ".join(lines)
keywords = analyse.extract_tags(content , topK = 30 ,withWeigth = False,allowPOS = ())

1.1.2 基于TextRank算法进行关键词抽取

jieba.analyse.textrank(sentence,topK = 20 , withWeight = False , allowPOS = ('ns','n','vn',,'v'))直接使用,接口相同,注意默认过滤词性
jieba.analyse.TextRank()新建自定义TextRank实例
基本思想:
将抽取关键词的文本进行分词
以固定窗口大小(默认为5,通过span属性调整),词之间的共线关系构建图
计算图中节点的PageRank,注意是无向带权图

真正好的论文被引用的次数多,好的网页被指向的次数多,每一个词都是一个节点,词榆次的连接通过窗口的形式连接。

import jieba.analyse as analyse
import pandas as pd
df = pd.read_csv('./origin_data/military_news.csv',encoding = 'utf-8')
df = df.dropna()
lines = df.content.values.tolist()
content = ' '.join(lines)
print(" " .join(analyse.textrank(content , topK =20 , withWeight = False , allowPOS = ('ns','n','vn','v'))))

print('------------------------------分割线----------------------------------------')
print(' '.join(analyse.textrank(content,topK = 20 , withWeight=False , allowPOS = ('ns','n') )))

1.2 LDA主题模型

用LDA主题模型建模,看看这些新闻主要说的是什么主题
首先我们要把文本内容处理成固定的格式,一个包含句子的list,list中每个元素是分词后的词list。类似下面这样子
[[第,一,条,新闻,在,这里],[第,二,条,新闻,在,这里],[这,是,在,做,什,么]]

#gensim输入有格式要求,一个嵌套列表,所有的新闻在大列表里,列表中的元素是子列表,字列表是每个新闻分词后的结果
from gensim import corpora,models,similarities
import gensim

1.2.1 载入停用词

stopwords = pd.read_csv('./origin_data/stopwords.txt)',index_col = False , quoting = 3 , sep = '\t' , names = ['stopword'],encoding = 'utf-8')
stopwords = stopwords['stopword'].values

1.2.2转换成合适的格式

import jieba
import pandas as pd
df = pd.read_csv()
df = df.dropna()
lines = df.content.values.tolist()

sentence = []
for line in lines:
  try:
    segs = jieba.lcut(line)
    sges = list(filter(lambda x: len(x)>1 ,segs))
    segs = list(filter(lambda x: x not in stopwords , segs))
    sentence.append(list(segs))
  except Exception as e:
    print(line)
    continue

#处理完的格式为[[第.一,条,新闻,在,这里] , [第,二,条,新闻,在,这里],[这,是,在,做,什么],.....]
print(sentencep[:2])

1.2.3 看一眼

for word in sentence[:5]:
  print(word)

1.2.4 词袋模型

#把嵌套的列表丢进gensim的corpora的Dictionary扫描一遍词 统计词频 为不同的词坐编号,根据编号把每句话转成不一样的id
dictionary = corpora.Dictionary(sentence)
corpus = [dictionary.doc2vec(sentence) for sentence in sentences]
corpus[5]

1.2.5 LDA建模

lda = gensim.models.LdaModel(corpus = corpus , id2word = dictionary , num_topics = 20)
print(lda.print_topic(3 , topn=5))

#把所有的主题打印出来看看
for topic in lda.print_topic(num_topics = 20 ,num_words = 8):
  print(topic[1)
上一篇下一篇

猜你喜欢

热点阅读