(文本表示及挖掘)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

這裏寫圖片描述

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