Python受欢迎的原因之一就是其计算生态丰富,据不完全统计,Python 目前为止有约13万+的第三方库。
本系列将会陆续整理分享一些有趣、有用的第三方库。
-
通过百度网盘获取:
链接:https://pan.baidu.com/s/1FSGLd7aI_UQlCQuovVHc_Q?pwd=mnsj
提取码:mnsj
-
前往GitHub获取:
https://github.com/returu/Python_Ecosystem
-
名词短语提取; -
词性标注; -
情感分析; -
分类(朴素贝叶斯、决策树); -
分词(将文本拆分为单词和句子); -
单词和短语频率统计; -
语法分析; -
n-gram提取; -
单词变形(复数化和单数化)和词形还原; -
拼写纠正。
pip install textblob
https://github.com/sloria/TextBlob
TextBlob旨在通过接口提供对常见文本处理操作的访问。
-
2.1 创建TextBlob:
# 导入TextBlob模块
from textblob import TextBlob
# 创建一个TextBlob对象
blob = TextBlob("TextBlob simplifies common natural language processing tasks.")
# 切片操作
blob[0:19]
# 输出:TextBlob("TextBlob simplifies")
# 常见的字符串方法
blob.upper()
# 输出:TextBlob("TEXTBLOB SIMPLIFIES COMMON NATURAL LANGUAGE PROCESSING TASKS.")
blob.find("common")
# 输出:20
# 比较TextBlobs和字符串
apple_blob = TextBlob("apples")
banana_blob = TextBlob("bananas")
apple_blob < banana_blob , apple_blob == "apples"
# 输出:(True, True)
# 连接和插入TextBlobs和字符串。
apple_blob + " and " + banana_blob
# 输出:TextBlob("apples and bananas")
"{0} and {1}".format(apple_blob, banana_blob)
# 输出:'apples and bananas'
-
2.2 名词短语提取:
# 通过noun_phrases属性来实现
blob.noun_phrases
# 输出:WordList(['textblob', 'common natural language processing tasks'])
-
2.3 词性标注:
通过tags属性访问词性标注。
# 通过tags属性访问词性标注
blob.tags
# 输出:[('TextBlob', 'NNP'),('simplifies', 'NNS'), ('common', 'JJ'),
# ('natural', 'JJ'), ('language', 'NN'), ('processing', 'NN'), ('tasks', 'NNS')]
-
2.4 情感分析:
sentiment属性返回一个名为Sentiment(polarity, subjectivity)的命名元组。极性分数是一个介于−1.0,1.0之间的浮点数,主观性是一个介于0.0,1.0之间的浮点数,其中0.0表示非常客观,1.0表示非常主观。
testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!")
testimonial.sentiment
# 输出:Sentiment(polarity=0.39166666666666666, subjectivity=0.4357142857142857)
-
2.5 分词:
可以将TextBlobs拆分为单词或句子。
zen = TextBlob("Beautiful is better than ugly. "
"Explicit is better than implicit. "
"Simple is better than complex.")
# 将TextBlobs拆分为单词
zen.words
# 输出:WordList(['Beautiful', 'is', 'better', 'than', 'ugly', 'Explicit', 'is', 'better', 'than', 'implicit', 'Simple', 'is', 'better', 'than', 'complex'])
# 将TextBlobs拆分为句子
zen.sentences
# 输出:[Sentence("Beautiful is better than ugly."),
# Sentence("Explicit is better than implicit."),
# Sentence("Simple is better than complex.")]
Sentence对象具有与TextBlobs相同的属性和方法。
# Sentence对象具有与TextBlobs相同的属性和方法
for sentence in zen.sentences:
print(sentence.sentiment)
# 输出:Sentiment(polarity=0.2166666666666667, subjectivity=0.8333333333333334)
# Sentiment(polarity=0.5, subjectivity=0.5)
# Sentiment(polarity=0.06666666666666667, subjectivity=0.41904761904761906)
使用sentence.start和sentence.end获取句子在TextBlob中的起始和结束索引。
# 使用sentence.start和sentence.end获取句子在TextBlob中的起始和结束索引
for s in zen.sentences:
print(s)
print("---- Starts at index {}, Ends at index {}".format(s.start, s.end))
# 输出:Beautiful is better than ugly.
# ---- Starts at index 0, Ends at index 30
# Explicit is better than implicit.
# ---- Starts at index 31, Ends at index 64
# Simple is better than complex.
# ---- Starts at index 65, Ends at index 95
-
2.6 单词变形和词形还原:
TextBlob.words或Sentence.words中的每个单词都是一个Word对象(unicode的子类),它有一些有用的方法,例如用于单词变形的方法。
使用TextBlob库对单词进行变形操作,包括单数化和复数化。
sentence = TextBlob("Use 4 spaces per indentation level.")
sentence.words
# 对WordList中的第三个单词(索引为2)进行单数化操作
sentence.words[2].singularize()
# 输出:'space'
# 对WordList中的最后一个单词(索引为-1)进行复数化操作
sentence.words[-1].pluralize()
# 输出:'levels'
可以通过调用lemmatize方法对单词进行词形还原。词形还原是将单词的不同形态还原为基本形式(词根形式)的过程。
lemmatize()方法支持传入词性参数(如“n”表示名词,“v”表示动词,“a”表示形容词等),以便更准确地进行词形还原。
from textblob import Word
w = Word("octopi")
# lemmatize()方法会将单词还原为名词的词根形式
# "octopi"是复数形式,其词形还原后的基本形式是"octopus"
w.lemmatize()
# 输出:'octopus'
w = Word("went")
# 调用lemmatize()方法,并传入了参数"v",表示将单词"went"还原为动词的词根形式
w.lemmatize("v")
# 输出:'go'
-
2.7 拼写纠正:
使用correct()方法尝试拼写纠正。
b = TextBlob("I havv goood speling!")
print(b.correct())
# 输出:I have good spelling!
Word对象有一个spellcheck()方法,返回一个包含拼写建议的(word, confidence)元组列表。
from textblob import Word
w = Word("havv")
w.spellcheck()
# 输出:[('have', 1.0)]
-
2.8 获取单词和名词短语频率:
-
第一种是通过word_counts字典
# 通过word_counts字典
monty = TextBlob("We are no longer the Knights who say Ni. "
"We are now the Knights who say Ekki ekki ekki PTANG.")
monty.word_counts['ekki']
# 输出:3
-
第二种方法是使用count()方法
count()方法可以通过case_sensitive参数指定搜索是否区分大小写(默认为False)。
# 使用count()方法
monty.words.count('ekki')
# 输出:3
# 搜索区分大小写
monty.words.count('ekki', case_sensitive=True)
# 输出:2
-
2.9 语法解析:
b = TextBlob("And now for something completely different.")
print(b.parse())
# 输出:And/CC/O/O now/RB/B-ADVP/O for/IN/B-PP/B-PNP something/NN/B-NP/I-PNP completely/RB/B-ADJP/O different/JJ/I-ADJP/O ././O/O
输出解释:
-
单词:每个单词后面跟着一个斜杠
/
。
-
词性标注(POS Tagging):标注单词的词性。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
语法角色:标注单词在句子中的语法角色。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
短语结构:标注单词所属的短语结构。
|
|
|
|
|
|
|
|
-
2.10 n-grams:
n-grams是一种常见的文本处理技术,用于将文本分解为连续的n个单词或字符的序列。
TextBlob对象的ngrams()方法用于提取文本中的n-grams,即将文本分解为连续的n个单词的序列,该方法返回一个包含n个连续单词的元组列表。
blob = TextBlob("Now is better than never.")
blob.ngrams(n=3)
# 输出:[WordList(['Now', 'is', 'better']),
# WordList(['is', 'better', 'than']),
# WordList(['better', 'than', 'never'])]
更多内容可以前往官方文档查看:
https://textblob.readthedocs.io/en/dev/


本篇文章来源于微信公众号: 码农设计师