Example explanation of keyword extraction in Python

不言
Release: 2018-04-28 15:01:34
Original
2057 people have browsed it

The following is an example of how to implement keyword extraction in python. It has a good reference value and I hope it will be helpful to everyone. Come and take a look together

The newbie is blogging again! ! ! No one said they were unhappy~~(>_<)~~

Today I will get a simple keyword extraction code

Article content keywords Extraction is divided into three major steps:

(1) Word segmentation

(2) Remove stop words

(3) Keyword extraction

There are many word segmentation methods. Here I choose the commonly used stuttering jieba word segmentation; to remove stop words, I used a stop word list .

The specific code is as follows:

import jieba
import jieba.analyse
#第一步:分词,这里使用结巴分词全模式
text = &#39;&#39;&#39;新闻,也叫消息,是指报纸、电台、电视台、互联网经常使用的记录社会、传播信息、反映时代的一种文体,具有真实性、时效性、简洁性、可读性、准确性的特点。新闻概念有广义与狭义之分。就其广义而言,除了发表于报刊、广播、电视上的评论与专文外的常用文本都属于新闻之列,包括消息、通讯、特写、速写(有的将速写纳入特写之列)等等。狭义的新闻则专指消息,消息是用概括的叙述方式,比较简明扼要的文字,迅速及时地报道国内外新近发生的、有价值的的事实。新闻也分公众新闻和小道新闻等。每则新闻在结构上,一般包括标题、导语、主体、背景和结语五部分。前三者是主要部分,后二者是辅助部分。写法上主要是叙述,有时兼有议论、描写、评论等。
&#39;&#39;&#39;
fenci_text = jieba.cut(text)
#print("/ ".join(fenci_text))
#第二步:去停用词
#这里是有一个文件存放要改的文章,一个文件存放停用表,然后和停用表里的词比较,一样的就删掉,最后把结果存放在一个文件中
stopwords = {}.fromkeys([ line.rstrip() for line in open(&#39;stopwords.txt&#39;) ])
final = ""
for word in fenci_text:
  if word not in stopwords:
    if (word != "。" and word != ",") :
      final = final + " " + word
print(final)
#第三步:提取关键词
a=jieba.analyse.extract_tags(text, topK = 5, withWeight = True, allowPOS = ())
print(a)
#text 为待提取的文本
# topK:返回几个 TF/IDF 权重最大的关键词,默认值为20。
# withWeight:是否一并返回关键词权重值,默认值为False。
# allowPOS:仅包括指定词性的词,默认值为空,即不进行筛选。
Copy after login

Running results:

runfile(&#39;D:/Data/文本挖掘/xiaojieba.py&#39;, wdir=&#39;D:/Data/文本挖掘&#39;)
 新闻 消息 指 报纸 、 电台 、 电视台 、 互联网 记录 社会 、 传播 信息 、 时代 一种 文体 真实性 、 时效性 、 简洁性 、 可读性 、 准确性 新闻 概念 广义 狭义 之分 广义 发表 报刊 、 广播 、 电视 评论 专文 外 常用 文本 新闻 列 包括 消息 、 通讯 、 特写 、 速写 ( 速写 纳入 特写 列 ) 狭义 新闻 专指 消息 消息 概括 叙述 方式 简明扼要 文字 报道 国内外 新近 发生 、 价值 事实 新闻 分 公众 新闻 小道 新闻 每则 新闻 在结构上 包括 标题 、 导语 、 主体 、 背景 结语 五 前 三者 二者 辅助 写法 叙述 兼有 议论 、 描写 、 评论 
[(&#39;新闻&#39;, 0.4804811569680808), (&#39;速写&#39;, 0.2121107125313131), (&#39;消息&#39;, 0.20363211136040404), (&#39;特写&#39;, 0.20023623445272729), (&#39;狭义&#39;, 0.16168734917858588)]
Copy after login

Okay, is it very simple?

Related recommendations:

Sample code for python implementation of log log

Python implementation of a function that accepts any number of parameters

The above is the detailed content of Example explanation of keyword extraction in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!