赞
踩
写在博客前的话:
本文主要阐述如何对一段简短的文本做特征提取的处理以及如何对文本进行分析。
本文主要脉络以一个故事 s t o r y story story 为主线,以该主线逐步延申,涉及到:文本特征提取、词汇频率统计 (TF),反文档频率 (IDF) 以及 余弦相似度 计算的概念,读者可以按照自己需求通过目录直接抵达,同时更建议从上到下按照本文介绍依次对概念逐个理解。
文本特征提取的步骤一览图:
《故事 s t o r y story story 在此》:
在情人节的那天,南方小镇的大街上,下着毛毛细雨,女朋友坐在V90CC的副驾驶上,看着车载导航,说到:前面一共有三个酒店,三个酒店的点评如下:
酒店一: "It is a hotel; quiet hotel, cheap hotel; ¥200 at hotel, Nice hotel;”
酒店二: “It is a quiet hotel, Nice!”
酒店三: “It is OK with the noise, since the hotel is cheapest among hotels.”
你说,哪个酒店都行,但是女朋友说到她想去的酒店的类型:
想去的类型: {cheap, quiet, nice ,hotel},让你按照这个决定从三个酒店中选择最符合她心意的,否则晚上睡地板。
按照空格分割
将三个酒店的点评按照空格拆分开:
酒店一: "It is a hotel; quiet hotel, cheap hotel; ¥200 at hotel, Nice hotel;”
酒店二: “It is a quiet hotel, Nice!”
酒店三: “It is OK with the noise, since the hotel is cheapest among hotels.”
拆分结果:
酒店 | 拆分结果 |
---|---|
酒店1 | { I t , i s , a , h o t e l , ; , q u i e t , , , h o t e l , c h e a p , h o t e l , ; , ¥ 200 , a t , h o t e l , , , N i c e , h o t e l , ; It, is, a, hotel, ; , quiet, , , hotel, cheap, hotel, ; , ¥200, at, hotel, , , Nice, hotel,; It,is,a,hotel,;,quiet,,,hotel,cheap,hotel,;,¥200,at,hotel,,,Nice,hotel,;} |
酒店2 | { I t , i s , a , q u i e t , h o t e l , , , N i c e , ! It, is, a, quiet, hotel, , , Nice,! It,is,a,quiet,hotel,,,Nice,!} |
酒店3 | { I t , i s , O K , w i t h , t h e , n o i s e , , , s i n c e , t h e , h o t e l , i s , c h e a p e s t , a m o n g , h o t e l s , . It, is, OK, with, the, noise, , , since, the, hotel, is, cheapest, among, hotels,. It,is,OK,with,the,noise,,,since,the,hotel,is,cheapest,among,hotels,.} |
去除数字以及标点符号
将上一步的结果进行去除数字以及标点符号的操作,去除结果为:
酒店 | 去除结果 |
---|---|
酒店1 | { I t , i s , a , h o t e l , q u i e t , h o t e l , c h e a p , h o t e l , a t , h o t e l , N i c e , h o t e l It, is, a, hotel, quiet, hotel, cheap, hotel, at, hotel, Nice, hotel It,is,a,hotel,quiet,hotel,cheap,hotel,at,hotel,Nice,hotel} |
酒店2 | { I t , i s , a , q u i e t , h o t e l , N i c e It, is, a, quiet, hotel, Nice It,is,a,quiet,hotel,Nice} |
酒店3 | { I t , i s , O K , w i t h , t h e , n o i s e , s i n c e , t h e , h o t e l , i s , c h e a p e s t , a m o n g , h o t e l s It, is, OK, with, the, noise, since, the, hotel, is, cheapest, among, hotels It,is,OK,with,the,noise,since,the,hotel,is,cheapest,among,hotels} |
所有字符全部小写
将上一步的结果进行改进,将所有的字符全部小写,结果为:
酒店 | 小写结果 |
---|---|
酒店1 | { i t , i s , a , h o t e l , q u i e t , h o t e l , c h e a p , h o t e l , a t , h o t e l , n i c e , h o t e l it, is, a, hotel, quiet, hotel, cheap, hotel, at, hotel, nice, hotel it,is,a,hotel,quiet,hotel,cheap,hotel,at,hotel,nice,hotel} |
酒店2 | { i t , i s , a , q u i e t , h o t e l , n i c e it, is, a, quiet, hotel, nice it,is,a,quiet,hotel,nice} |
酒店3 | { i t , i s , o k , w i t h , t h e , n o i s e , s i n c e , t h e , h o t e l , i s , c h e a p e s t , a m o n g , h o t e l s it, is, ok, with, the, noise, since, the, hotel, is, cheapest, among, hotels it,is,ok,with,the,noise,since,the,hotel,is,cheapest,among,hotels} |
第一步骤分割句子到此完成!
下面进行第二步去除词汇!!
在完成分割句子的步骤之后,此时需要对句子中一些常用词,不如代词,冠词以及功能词进行清除,原因在于这些词汇其实对我们的分析并没有用处,知识为了保证句子的完整性与语法才用的词汇,去除之。
需要去除的代词与冠词功能词包含: " a " , " t h e " , " i n " , " t o " , " I " , " h e " , " s h e " "a","the","in","to","I","he","she" "a","the","in","to","I","he","she" 等。
继续进行改进,将所有代词冠词等清除:
酒店 | 清除后结果 |
---|---|
酒店1 | { h o t e l , q u i e t , h o t e l , c h e a p , h o t e l , h o t e l , n i c e , h o t e l hotel, quiet, hotel, cheap, hotel, hotel, nice, hotel hotel,quiet,hotel,cheap,hotel,hotel,nice,hotel} |
酒店2 | { q u i e t , h o t e l , n i c e quiet, hotel, nice quiet,hotel,nice} |
酒店3 | { n o i s e , h o t e l , c h e a p e s t , h o t e l s noise, hotel, cheapest, hotels noise,hotel,cheapest,hotels} |
在完成上述的两个步骤后,接近处理的尾声,最后一步进一步对剩下的词汇做处理,去除复数的形式,去除比较级与最高级形式,修改同义词汇不同形式的词语。若上述理解起来有些不明白,可以看如下的举例:
e
.
g
.
e.g.
e.g.
(1)去除复数形式
“hotels” to “hotel”
(2)去除最高级形式
“cheapest” to “cheap”
(3)去除同义不同词
“computational” to “compute”
“computation” to “compute”
将上一步处理好的三个酒店的结果再做进一步处理,作为最终处理结果:
酒店 | 处理后结果 |
---|---|
酒店1 | { h o t e l , q u i e t , h o t e l , c h e a p , h o t e l , h o t e l , n i c e , h o t e l hotel, quiet, hotel, cheap, hotel, hotel, nice, hotel hotel,quiet,hotel,cheap,hotel,hotel,nice,hotel} |
酒店2 | { q u i e t , h o t e l , n i c e quiet, hotel, nice quiet,hotel,nice} |
酒店3 | { n o i s e , h o t e l , c h e a p , h o t e l noise, hotel, cheap, hotel noise,hotel,cheap,hotel} |
“你怎么才弄完??”女朋友埋怨道,我早就整理好了!!!
至此,我们对三个酒店的信息处理完成,即 文本特征提取 完成。
我们此步骤为将处理好的三个酒店的信息进行词汇频率统计:
Hotel1: {
h
o
t
e
l
:
5
,
q
u
i
e
t
:
1
,
c
h
e
a
p
:
1
,
n
i
c
e
:
1
hotel:5, quiet:1, cheap:1, nice:1
hotel:5,quiet:1,cheap:1,nice:1}
Hotel2: {
q
u
i
e
t
:
1
,
h
o
t
e
l
:
1
,
n
i
c
e
:
1
quiet:1, hotel:1, nice:1
quiet:1,hotel:1,nice:1}
Hotel3: {
n
o
i
s
e
:
1
,
h
o
t
e
l
:
2
,
c
h
e
a
p
:
1
noise:1, hotel:2, cheap:1
noise:1,hotel:2,cheap:1}
我们根据上面的结果构造 词频统计表格:
Vocabulary | quiet | cheap | hotel | noise | nice |
---|---|---|---|---|---|
Hotel1 | 1 | 1 | 5 | 0 | 1 |
Hotel2 | 1 | 0 | 1 | 0 | 1 |
Hotel3 | 0 | 1 | 2 | 1 | 0 |
至此,我们可以尝试去判断女朋友所求的酒店是哪个酒店了。
还记得女朋友的要求吗?
她的想法我怎么会不记得声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/666079
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。