Bow詞袋模型原理與實例(bag of words)

The bag-of-words model is a simplifying assumption used in natural language processing and information retrieval. In this model, a text (such as a sentence or a document) is represented as an unordered collection of words, disregarding grammar and even word order.
詞袋模型是在自然語言處理和信息檢索中的一種簡單假設。在這種模型中,文本(段落或者文檔)被看作是無序的詞彙集合,忽略語法甚至是單詞的順序。

The bag-of-words model is used in some methods of document classification. When a Naive Bayes classifier is applied to text, for example, the conditional independence assumption leads to the bag-of-words model. [1] Other methods of document classification that use this model are latent Dirichlet allocation and latent semantic analysis.[2]
詞袋模型被用在文本分類的一些方法當中。當傳統的貝葉斯分類被應用到文本當中時,貝葉斯中的條件獨立性假設導致詞袋模型。另外一些文本分類方法如LDA和LSA也使用了這個模型。

Example: Spam filtering
In Bayesian spam filtering, an e-mail message is modeled as an unordered collection of words selected from one of two probability distributions: one representing spam and one representing legitimate e-mail (“ham”). Imagine that there are two literal bags full of words. One bag is filled with words found in spam messages, and the other bag is filled with words found in legitimate e-mail. While any given word is likely to be found somewhere in both bags, the “spam” bag will contain spam-related words such as “stock”, “Viagra”, and “buy” much more frequently, while the “ham” bag will contain more words related to the user’s friends or workplace.
在貝葉斯垃圾郵件過濾中,一封郵件被看作無序的詞彙集合,這些詞彙從兩種概率分佈中被選出。一個代表垃圾郵件,一個代表合法的電子郵件。這裏假設有兩個裝滿詞彙的袋子。一個袋子裏面裝的是在垃圾郵件中發現的詞彙。另一個袋子裝的是合法郵件中的詞彙。儘管給定的一個詞可能出現在兩個袋子中,裝垃圾郵件的袋子更有可能包含垃圾郵件相關的詞彙,如股票,偉哥,“買”,而合法的郵件更可能包含郵件用戶的朋友和工作地點的詞彙。

To classify an e-mail message, the Bayesian spam filter assumes that the message is a pile of words that has been poured out randomly from one of the two bags, and uses Bayesian probability to determine which bag it is more likely to be.
爲了將郵件分類,貝葉斯郵件分類器假設郵件來自於兩個詞袋中中的一個,並使用貝葉斯概率條件概率來決定那個袋子更可能產生這樣的一封郵件。


Bow example :

Reference website : http://blog.csdn.net/android_ruben/article/details/78238483
1.已知詞彙
2.對已知詞彙進行表示

步驟1.蒐集數據

It was the best of times,
it was the worst of times,
it was the age of wisdom,
it was the age of foolishness,

步驟2.設計詞彙表

“it”
“was”
“the”
“best”
“of”
“times”
“worst”
“age”
“wisdom”
“foolishness”

步驟3.創建文檔向量
eg : “It was the best of times“

“it” = 1
“was” = 1
“the” = 1
“best” = 1
“of” = 1
“times” = 1
“worst” = 0
“age” = 0
“wisdom” = 0
“foolishness” = 0

vector:
 It was the best of times  [1, 1, 1, 1, 1, 1, 0, 0, 0, 0]

it was the worst of times [1, 1, 1, 0, 1, 1, 1, 0, 0, 0]

it was the age of wisdom [1, 1, 1, 0, 1, 1, 0, 1, 1, 0]

it was the age of foolishness [1, 1, 1, 0, 1, 1, 0, 0, 0, 1]

管理詞彙表

隨着文檔的不斷增長,詞彙表的增長將會導致文檔向量不斷的增長,表現爲文檔向量的維度不斷增加
設想要爲大量的書籍建立詞袋模型,那麼詞彙表將會變得非常的大,文檔向量將會變得相當的長。當往往一本書中其實通常使用到的詞彙表是非常小的,這就會導致一本書的表示向量中存在大量的0.這樣的向量稱爲稀疏向量或者叫稀疏表示

稀疏的向量將會在計算的時候耗費大量的計算資源和內存,所以減小詞彙表大小就成爲了急切需要解決的問題了。

下面介紹了一些簡單的減小詞彙表的方法:

忽略大小寫
忽略標點符號
去除無意義的詞,比如a the of
修正拼寫錯誤
取出時態


一種複雜的方法就是對詞進行聚合。這個方法能夠得到文檔一些語義信息,但也面臨這詞袋模型面臨的同樣問題。
這個方法叫做:N-元模型。N表示聚合的詞個數,比如2就表示2個2個聚合在一起,叫做2元模型。
比如說“It was the best of times”,經過2元模型處理之後表示如下:

“it was”
“was the”
“the best”
“best of”
“of times”
N元模型比詞袋模型在某些任務表現得更好,比如文檔分類,但也在某些情況下帶來麻煩。

詞的權重評定

當文檔向量確定之後,就需要給每一個詞的權重進行評估了。
首先介紹2個概念:

出現次數:詞在一個文檔中出現的次數
出現頻率:詞在一個文檔中出現的次數除以文檔中的詞總數


1.詞的哈希

在計算機科學中,通常使用哈希方程將大的數值空間轉換爲固定範圍的數值。比方說將名字轉換爲數字以方便查找。
我們可以把詞彙表中的詞進行哈希表示,這樣就解決了大量文檔和詞彙表太長的問題。因爲我們可以把詞彙表種的每一個詞都表示成固定長度的哈希表示。這個方法存在的問題就是要儘可能的減小碰撞。

2.TF-IDF

TF-IDF(term frequency–inverse document frequency)是一種用於資訊檢索與文本挖掘的常用加權技術。TF-IDF是一種統計方法,用以評估一字詞對於一個文件集或一個語料庫中的其中一份文件的重要程度字詞的重要性隨着它在文件中出現的次數成正比增加,但同時會隨着它在語料庫中出現的頻率成反比下降。

詞袋模型的侷限性

雖然詞袋模型簡單易用並且在實際應用中取得了很大的成功,但是詞袋模型本身也具有侷限性:

詞彙表的構建:詞彙表的建立和維護都值得考量,不合理的詞彙表將導致文檔表示向量的稀疏問題顯著。
稀疏問題:詞袋模型有一個原生問題就是向量的稀疏,這將對計算資源和推理帶來巨大的挑戰
語義:因爲詞袋模型沒有考慮到語序,但是往往語序又蘊含着不同的語義信息。比如“this is interesting” vs “is this interesting”,不同的語序代表的語義是不同的。

 

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