1. 程式人生 > >Bow詞袋模型原理與例項(bag of words)

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 :

步驟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”,不同的語序代表的語義是不同的。