(文本表示及挖掘)Representing and Mining Text

主要内容:

1. Text data
2. Bag of words
3. N-gram sequence 
4. Text mining 案例

(一) text data

文本数据(Text data )的特点

  • Unstructured data (非结构化数据)
  • Linguistic structure(语言结构)——NLP (自然语言处理)

文本数据的缺陷(Text data’s problem —dirty data ):

  1. 不按照语法规则书写,例如很多用户留言或评论(Ungrammatical)
  2. 拼写错误(misspell)
  3. 各种缩写简写(Abbreviate)
  4. 随意使用标点符号(Punctuate randomly)
  5. 各种同义词的使用(Synonyms )
  6. 同形异词(Homograph)
  7. 同一词汇在不同领域的不同理解(Different domain)
  8. 很多词汇的理解需要基于上下文(Context)

Dirty data 需要经过预处理(preprocess)才能作为下一步模型的输入(input)

A collection of documents is called 'corpus'

(二)bag of words

词汇频率表(term frequency)–记录文档中,每个词汇的出现次数

In mathematics a bag is a multiset, where members are allowed to appear more than once. (discarding word order)

这里写图片描述

Term frequency 有两点需要我们注意:

  1. 太过稀有的词汇往往不加入考虑范畴。 对于聚类分析,低频的词汇往往没有太大作用,所以在预处理时,我们会设置一个最低频率限制。(当然,在有的应用场景下,这些低频词汇会显得十分重要,就和我们有时候需要关注outlier一样)
  2. 过于平凡的词汇也不用太过关注。在聚类分析中,这种平凡词汇并不能提供区分点,所以我们常常会设置一个最高频率限制

Term frequency 描述了词汇在单个文档中的频繁程度,我们常常还要关心这个词汇在我们挖掘的文档集中的分布情况。于是我们便引入了逆文档频率(inverse document frequency)—IDF的概念,若某些词汇集中出现在少数几个文档中,那么这些词汇对这几个文档的重要性不言而喻。

IDF(t)=1+log(t)

IDF 图像效果如下

这里写图片描述

看以看出,当某个词汇在文档集中出现较为稀缺时,IDF 值是显著的,并且随着数量增加显著性迅速下降,大多数stopword 的IDF 为 1

TF (term frequency )IDF(inverse document frequency)和并到一起,得到TFIDF 值

TFIDF(t,d)=TF(t,d)IDF(t)

依据TFIDF 我们便可以将文档转变为特征向量 , 随后我们往往需要进行特征选择(可以使用频率的上下阈值,或者之前提到过的information gain),之后我们便能够将特征向量用于各种模型之中。

IDF 与 熵(Entropy )的联系

定义 p(t)=t 简记为p

于是有 IDF(t)=1+log(1p)

1 仅仅是一个常数,故忽略后得到 IDF(t)=log(1p)=log(p)

IDF(nott)=log(11p)=log(1p)

Entropy(t)=plog(p)(1p)log(1p)

=pIDF(t)(1p)[IDF(nott)]

=pIDF(t)+(1p)IDF(nott)

We can express entropy as the expected value of IDF(t) and IDF(not_t) based on the probability of its occurrence in the corpus.

这里写图片描述

可以看出第三图图与熵的图是一致的


(三)N-gram Sequence

看到N-gram Sequence 方法的时候突然想起了之前写过的一段代码

用R语言写的提取英语文章中较为常见的短语的小程序

(当初觉得别人整理的常见词组没有针对性,于是就写了这个,将各种文章导入,一份属于自己的常用短语表就出来了,【初学R语言时现学现用的小代码,太过简单,勿喷】)

[Words 是将文档split 后得到的 word list ]

n=2
two_word<-character(0)
for (i in 1:(length(words)-n+1))
{
two_word[i]<-paste(words[i],words[i+1])
}
s2w<-sort(table(two_word),d=T)
s2w<-s2w[s2w>2]

n=3
three_word<-character(0)
for (i in 1:(length(words)-n+1))
{
three_word[i]<-paste(words[i],words[i+1],words[i+2])
}
s3w<-sort(table(three_word),d=T)
s3w<-s3w[s3w>1]

N-gram sequence 的例子:

“The quick brown fox jumps.” it would be transformed into the set of its constitutent words {quick, brown, fox, jumps}, plus the tokens quick_brown, brown_fox, and fox_jumps.

N-gram Sequence 方法 ,简单的讲就是 将邻近的几个词作为一个整体加入变量集合

Topic models

在词汇与文档直接对应的模型中加入 topic 层,可以挖掘出更多的隐含信息(atent information)

【个人觉得 topic layer 有点 neural network 中 hiden layer 的意味】

这里写图片描述

General methods for creating topic models include matrix factorization methods, such as Latent Semantic Indexing and Probabilistic Topic Models, such as Latent Dirichlet Allocation. 

(四)Text mining 案例

Mining News Stories to Predict Stock Price Movement

这里写图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章