评论文本挖掘(Review Text Mining)是一种自然语言处理(NLP)技术,用于从在线评论、社交媒体帖子和其他文本数据中提取有用信息。这种技术可以帮助企业和研究人员了解消费者对产品、服务和品牌的看法,从而为市场营销策略、产品开发和客户服务提供有价值的见解。
评论文本挖掘的主要步骤:
评论文本挖掘在各种应用场景中具有重要价值
如何用数值来表示文本
机器不懂人类的自然语言,我们要将自然语言转换为机器易于理解的“语言”,NLP(Natural language processing)
NLP 里面,最细粒度的是词语,词语组成句子,句子再组成段落、篇章、文档。所以处理 NLP 的问题,首先要处理词语 。
词语处理的最终目标是用向量来表示词语,通过词向量,可以计算不同词语之间的相似度,这对于词语层面的任务非常重要。利用词向量可以找到语义上相近的词语,这有助于改善信息检索和文本分析的效果。词向量可以作为搜索引擎和信息检索系统中的一个重要工具,帮助提高检索的准确性。
获取原始文本 : Yes, Everjoys ukulele is the right Soprano Ukulele I have been looking for. It arrived earlier and very well packed, just the way I expected.
分词:[‘Yes’,’,', 'Everjoys', 'ukulele', 'is', 'the', 'right', 'Soprano', 'Ukulele', 'I', 'have', 'been', 'looking', 'for.', 'It', 'arrived', 'earlier', 'and', 'very', 'well', 'packed,', 'just', 'the', 'way', 'I', 'expected.']
向量化编码:[1,0,1,0,1,0,1,0……]
词干提取 – Stemming
词干提取是自然语言处理中的一个步骤,主要是针对英文语料进行的处理。它的目的是通过去除单词的前后缀来得到词根,这有助于标准化词汇,便于后续的处理和分析。
这些例子展示了通过去除单词的后缀来得到它们的词干。词干提取的目标是将单词还原到它们的基本形式,以便进行进一步的文本处理和分析。
词形还原 – Lemmatisation
将单词的各种形态转换回它们的基本形态或词典形式。与词干提取不同,词形还原考虑了单词的语法和语义信息,以确保还原后的单词在语境中是正确的。
nltk是一个用于自然语言处理的Python库,它提供了各种文本处理功能。在给定的代码中,from nltk.stem.wordnet import WordNetLemmatizer这行代码导入了WordNetLemmatizer类,该类是nltk库中的一个工具,用于进行词形还原(lemmatization)。
from nltk.stem.wordnet import WordNetLemmatizer
# 创建词形还原器对象
lemmatizer = WordNetLemmatizer()
# 要进行词形还原的单词列表
words = ['running', 'dogs', 'better']
# 对每个单词进行词形还原
lemmatized_words = [lemmatizer.lemmatize(word) for word in words]
# 打印词形还原后的结果
for word, lemma in zip(words, lemmatized_words):
print(f"{word} -> {lemma}")running -> run dogs -> dog better -> good
EG
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
import pandas as pd
# nltk:文本处理的包
from nltk.stem.wordnet import WordNetLemmatizer
from collections import Counter
import matplotlib.pyplot as plt
from nltk.corpus import wordnet as wn
import pyecharts.options as opts
from pyecharts.charts import WordClouddef stars_cat(n):
if n<=2:
return '差评'
elif n ==3:
return '中评'
else:
return '好评'
# 获取评论中的日期信息,转换成日期时间格式
def get_date(x):
x = x.split('on ')[1] # 把数据拆分成两部分 ['Reviewed in the United States on ','June 24, 2020']
x = x.split(', ')
y= x[1]
x = x[0].split(' ')
m,d = x[0],x[1]
if m=='January' or m=='Jan':
on_date='01-'+d+'-'+y
elif m=='February' or m=='Feb':
on_date='02-'+d+'-'+y
elif m=='March' or m=='Mar':
on_date='03-'+d+'-'+y
elif m=='April' or m=='Apr':
on_date='04-'+d+'-'+y
elif m=='May':
on_date='05-'+d+'-'+y
elif m=='June' or m=='Jun':
on_date='06-'+d+'-'+y
elif m=='July' or m=='Jul':
on_date='07-'+d+'-'+y
elif m=='August' or m=='Aug':
on_date='08-'+d+'-'+y
elif m=='September' or m=='Sep':
on_date='09-'+d+'-'+y
elif m=='October' or m=='Oct':
on_date='10-'+d+'-'+y
elif m=='November' or m=='Nov':
on_date='11-'+d+'-'+y
elif m=='December' or m=='Dec':
on_date='12-'+d+'-'+y
on_date=datetime.datetime.strptime(on_date, '%m-%d-%Y').strftime('%Y-%m-%d')
return on_datedef replace_abbreviations(text):
new_text = text
#则表达式过滤特殊符号用空格符占位,双引号、单引号、句点、逗号
new_text = re.sub(r'[^a-zA-Z.,?! \']+',' ', text).strip().lower()
# 还原常见缩写单词还原,i'm i'd he's
new_text = re.sub(r"(it|he|she|that|this|there|here)(\'s)",r"\1 is", new_text,re.I)
# (?<=pattern)xxx,就是捕获以pattern开头的内容xxx
new_text = re.sub(r"(?<=[a-zA-Z])n\'t"," not", new_text) # not的缩写 aren't-- are not
new_text = re.sub(r"(?<=[a-zA-Z])\'d"," would", new_text) # would的缩写i'd -->i would--> 'i' 'would'
new_text = re.sub(r"(?<=[a-zA-Z])\'ll"," will", new_text) # will的缩写
new_text = re.sub(r"(?<=[I|i])\'m"," am", new_text) # am的缩写
new_text = re.sub(r"(?<=[a-zA-Z])\'re"," are", new_text) # are的缩写
new_text = re.sub(r"(?<=[a-zA-Z])\'ve"," have", new_text) # have的缩写
new_text = new_text.replace('\'', ' ').replace('.', '. ')
return new_textdef get_words(clean_text):
words_all = []
for words in clean_text:
for word in words:
words_all.append(word)
total_words = list(set(words_all))
all_words = Counter(words_all)
content_mean = len(words_all)/len(clean_text)
words_cap = len(total_words)/len(words_all)
return all_words,content_mean,total_words,words_cap
(WordCloud()
.add(series_name="好评词云",
data_pair=positive_words_wordcloud, #传入绘制词云图的数据
word_size_range=[16, 80]) #word_size_range 字号大小取值范围
.set_global_opts(
title_opts=opts.TitleOpts(
title="好评词云",
title_textstyle_opts=opts.TextStyleOpts(font_size=23) # 设置标题字号
),
tooltip_opts=opts.TooltipOpts(is_show=True), # 设置为True 鼠标滑过文字会弹出提示框
)
.render_notebook()
)render(path): 将词云图保存为图片文件。参数path是保存图片的路径和文件名。
render_embed(): 将词云图嵌入到HTML页面中。返回一个包含词云图的HTML代码字符串。
render_json(): 将词云图转换为JSON格式的数据。返回一个包含词云图数据的字典。
repr_html_(): 返回一个包含词云图的HTML代码字符串。这个方法主要用于Jupyter Notebook中的交互式显示。
to_image(): 将词云图转换为PIL.Image对象。可以使用此方法将词云图保存为其他格式的图片文件。