python提取word参考文献_Python wordsegment包_程序模块 - PyPI - Python中文网

教程

在您自己的python程序中,您通常希望使用segment来划分

词组列表:>>> from wordsegment import load, segment

>>> load()

>>> segment('thisisatest')

['this', 'is', 'a', 'test']

函数从

磁盘。只需加载一次数据。

WordSegment还为批处理提供了一个命令行接口

处理。此接口接受两个参数:in file和out file。线

文件中的from被迭代分段,用空格连接,并写入

输出文件。输入和输出分别默认为stdin和stdout。$ echo thisisatest | python -m wordsegment

this is a test

如果您想将WordSegment作为一种服务器进程运行,那么使用python的

-u用于无缓冲输出的选项。您还可以在中设置PYTHONUNBUFFERED=1。

环境。>>> import subprocess as sp

>>> wordsegment = sp.Popen(

['python', '-um', 'wordsegment'],

stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)

>>> wordsegment.stdin.write('thisisatest\n')

>>> wordsegment.stdout.readline()

'this is a test\n'

>>> wordsegment.stdin.write('workswithotherlanguages\n')

>>> wordsegment.stdout.readline()

'works with other languages\n'

>>> wordsegment.stdin.close()

>>> wordsegment.wait() # Process exit code.

0<>最大分段字长为24个字符。既不是unigram也不是

bigram数据包含超过该长度的单词。语料库也排除了

标点符号和所有字母都已小写。在分割文本之前,

clean被调用以将输入转换为规范形式:>>> from wordsegment import clean

>>> clean('She said, "Python rocks!"')

'shesaidpythonrocks'

>>> segment('She said, "Python rocks!"')

['she', 'said', 'python', 'rocks']

有时,研究unigram和bigram计数很有趣

他们自己。它们存储在python字典中,将单词映射到count。>>> import wordsegment as ws

>>> ws.load()

>>> ws.UNIGRAMS['the']

23135851162.0

>>> ws.UNIGRAMS['gray']

21424658.0

>>> ws.UNIGRAMS['grey']

18276942.0

上面我们看到拼写gray比拼写gray更常见。

大图由空格连接:>>> import heapq

>>> from pprint import pprint

>>> from operator import itemgetter

>>> pprint(heapq.nlargest(10, ws.BIGRAMS.items(), itemgetter(1)))

[('of the', 2766332391.0),

('in the', 1628795324.0),

('to the', 1139248999.0),

('on the', 800328815.0),

('for the', 692874802.0),

('and the', 629726893.0),

('to be', 505148997.0),

('is a', 476718990.0),

('with the', 461331348.0),

('from the', 428303219.0)]

有些大论以&lt;s&gt;开头。这表示bigram的开始:>>> ws.BIGRAMS[' where']

15419048.0

>>> ws.BIGRAMS[' what']

11779290.0

unigrams和bigrams数据存储在

分别是unigrams.txt和bigrams.txt文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现中文分词和词性标注可以使用jieba和pynlpir这两个库,而TF-IDF的实现可以使用sklearn库。以下是Python代码实现: ```python import jieba import jieba.posseg as pseg import pynlpir from sklearn.feature_extraction.text import TfidfVectorizer # 分词和词性标注 def segment(text): # 使用jieba分词 words = jieba.cut(text) # 使用pseg标注词性 words_with_pos = pseg.cut(text) return [(word, flag) for word, flag in words_with_pos] # 初始化pynlpir pynlpir.open() # 分词和词性标注 def segment(text): # 使用pynlpir分词 words = pynlpir.segment(text) # 使用pynlpir标注词性 words_with_pos = pynlpir.postag(words) return [(word, flag) for word, flag in words_with_pos] # 计算TF-IDF def tfidf(texts): # 初始化TfidfVectorizer vectorizer = TfidfVectorizer() # 计算TF-IDF矩阵 tfidf_matrix = vectorizer.fit_transform(texts) # 获取关键词列表 feature_names = vectorizer.get_feature_names() # 遍历每个文本 for i in range(len(texts)): # 获取该文本的TF-IDF矩阵 tfidf_vector = tfidf_matrix[i] # 将该文本的关键词按照TF-IDF值从大到小排序 sorted_indices = tfidf_vector.toarray()[0].argsort()[::-1] # 获取该文本的前10个关键词 top_10 = [feature_names[j] for j in sorted_indices[:10]] print("Top 10 keywords in text ", i+1, ": ", top_10) # 测试代码 text1 = "这是一段测试文本,用于测试分词和词性标注。" text2 = "这是另外一段测试文本,用于测试TF-IDF关键词提取。" texts = [text1, text2] for text in texts: print(segment(text)) tfidf(texts) ``` 注意,使用pynlpir需要先安装pynlpir库,并且需要下载pynlpir所需的数据。可以使用以下代码进行安装和下载: ```python import pynlpir # 安装pynlpir pynlpir.open() # 下载所需的数据 pynlpir.nlpir.update() ``` 另外,TF-IDF的计算需要将文本转换为向量表示,因此需要使用TfidfVectorizer类。在调用fit_transform方法计算TF-IDF矩阵之前,需要先使用get_feature_names方法获取所有的关键词。最后,可以使用argsort方法对TF-IDF值进行排序,并获取前10个关键词。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值