2016-08-13 160 views
1

我想分析一個大的(n = 500,000)文檔語料庫。我使用quanteda期望will be fastertm_map()tm。我想要一步一步地執行,而不是使用dfm()的自動方式。我有這樣的理由:在一種情況下,我不想在移除停用詞之前進行標記化,因爲這會導致許多無用的bigrams,在另一種情況下,我必須使用特定於語言的過程預處理文本。創建dfm一步一步與quanteda

謹以此順序實施:
1)刪除標點和數字
2),即標記化之前除去停用詞(以避免無用的令牌)
3)標記化使用unigram進行和雙字母組
4 )創建DFM

我嘗試:

> library(quanteda) 
> packageVersion("quanteda") 
[1] ‘0.9.8’ 
> text <- ie2010Corpus$documents$texts 
> text.corpus <- quanteda:::corpus(text, docnames=rownames(ie2010Corpus$documents)) 

> class(text.corpus) 
[1] "corpus" "list" 

> stopw <- c("a","the", "all", "some") 
> TextNoStop <- removeFeatures(text.corpus, features = stopw) 
# Error in UseMethod("selectFeatures") : 
# no applicable method for 'selectFeatures' applied to an object of class "c('corpus', 'list')" 

# This is how I would theoretically continue: 
> token <- tokenize(TextNoStop, removePunct=TRUE, removeNumbers=TRUE) 
> token2 <- ngrams(token,c(1,2)) 

獎金問題 如何刪除quanteda中的稀疏令牌? (即在tmremoveSparseTerms()相當於


UPDATE 在@肯的回答的光,這裏是按部就班與quanteda代碼:

library(quanteda) 
packageVersion("quanteda") 
[1] ‘0.9.8’ 

1)刪除自定義標點符號和數字。例如。注意到,「\ n」,在ie2010語料庫

text.corpus <- ie2010Corpus 
texts(text.corpus)[1]  # Use texts() to extrapolate text 
# 2010_BUDGET_01_Brian_Lenihan_FF 
# "When I presented the supplementary budget to this House last April, I said we 
# could work our way through this period of severe economic distress. Today, I 
# can report that notwithstanding the difficulties of the past eight months, we 
# are now on the road to economic recovery.\nIt is 

texts(text.corpus)[1] <- gsub("\\s"," ",text.corpus[1]) # remove all spaces (incl \n, \t, \r...) 
texts(text.corpus)[1] 
2010_BUDGET_01_Brian_Lenihan_FF 
# "When I presented the supplementary budget to this House last April, I said we 
# could work our way through this period of severe economic distress. Today, I 
# can report that notwithstanding the difficulties of the past eight months, we 
# are now on the road to economic recovery. It is of e 

上的原因的進一步注爲什麼一個人可能更願意預處理。我目前的語料庫是意大利語,這是一種用撇號連接單詞的文章。因此,直線dfm()可能導致不精確的標記。 例如爲:

broken.tokens <- dfm(corpus(c("L'abile presidente Renzi. Un'abile mossa di Berlusconi"), removePunct=TRUE)) 

將產生相同的字兩個分離的標記(「un'abile」和「L'abile」),因此需要與gsub()這裏的附加步驟的。

2)在quanteda中,不可能在標記之前直接在文本中刪除停用詞。在我之前的例子中,「l」和「un」必須去掉,不要產生誤導性的bigrams。這可以在tmtm_map(..., removeWords)處理。

3)符號化

token <- tokenize(text.corpus[1], removePunct=TRUE, removeNumbers=TRUE, ngrams = 1:2) 

4)創建DFM:

dfm <- dfm(token) 

5)刪除稀疏特徵

dfm <- trim(dfm, minCount = 5) 
+0

爲了總結答案,可以使用'texts()'函數在'quanteda'中逐步進行: – 000andy8484

回答

4

我們設計dfm()不是作爲一個 「黑匣子」,但更像是瑞士軍刀,它結合了許多典型用戶在轉換時需要應用的選項將他們的文本添加到文檔和功能矩陣中。但是,如果希望施加更好的控制,所有這些選項都可以通過較低級別的處理命令獲得。

然而,quanteda的設計原則之一是文本只通過標記過程變成「特徵」。如果您有一組您希望排除的標記化功能,則必須先標記文本,否則不能排除它們。與R的其他文本包(例如tm)不同,這些步驟從語料庫「下游」應用,以便語料庫保持未處理的文本集,操作將被應用於該文本集(但其本身不是變換的文本)。這樣做的目的是爲了保持一般性,同時爲了提高文本分析的可重複性和透明度。

在回答你的問題:

  1. 可以使用texts(myCorpus) <-功能,哪裏還有什麼被分配到文本將覆蓋現有的文本但是覆蓋我們鼓勵的行爲。因此,您可以使用正則表達式去除標點符號和數字 - 例如stringi命令,並使用Unicode類標點符號和數字來標識模式。

  2. 我會在刪除停用詞之前建議您使用tokenise。停止「單詞」是令牌,因此在標記文本之前無法從文本中刪除這些單詞。即使使用正則表達式替代"",也需要在正則表達式中指定某種形式的詞邊界 - 這也是標記化。

  3. 要tokenise成unigram進行和雙字母組:

    令牌(myCorpus,n元語法= 1:2)

  4. 要創建DFM,只需調用dfm(myTokens)。 (你也可以應用了第3步,爲的n-gram,在這個階段

獎金1:N = 2間的搭配產生相同的列表,二元語法,只是以不同的格式是否希望別的東西(?分開,以便也許質疑)

獎金2:見dfm_trim(x, sparsity =)removeSparseTerms()選擇相當迷惑大多數人,但是這包括從TM移民見this post更全面的解釋

BTW:。用texts()而不是ie2010Corpus$documents$texts - 我們將很快重寫一個語料庫的對象結構,所以當存在提取函數時,不應該以這種方式訪問​​它的內部結構。 (另外,這一步是不必要的 - 在這裏你只是簡單地重新語料庫。)

更新2018-01

爲語料對象的新名字是data_corpus_irishbudget2010,並搭配打分函數是textstat_collocations()

+1

非常感謝@Ken。我錯過了這個'texts()'函數。 – 000andy8484

+0

不客氣!對於一些版本,「文本< - 」被禁用,但我意識到很多人會發現替換功能很有用。 (希望他們會負責任地使用這個!) –