Я работал с R.3.4.1 и перешел на R3.3.3, теперь решение VCorpus работает для меня. И TM, и RWeka создают биграммы правильно.

многих руководств по созданиюбиграмм используя пакеты 'tm' и 'RWeka', я разочаровался, что только1-Грамм были возвращены вТДМ, Через много проб и ошибок я обнаружил, что правильная функция была достигнута с помощью 'VCorpus«но не использую»тело». Кстати, я почти уверен, что это работало с «Корпусом» ~ 1 месяц назад, но это не сейчас.

R (3.3.3), RTools (3.4), RStudio (1.0.136) и все пакеты (tm 0.7-1, RWeka 0.4-31) обновлены до последней версии.

Я был бы признателен за любую информацию о том, что это не будет работать с Корпус и если у других есть такая же проблема.

#A Reproducible example
#
#Weka bi-gram test
#

library(tm)
library(RWeka)

someCleanText <- c("Congress shall make no law respecting an establishment of",
                    "religion, or prohibiting the free exercise thereof or",
                    "abridging the freedom of speech or of the press or the",
                    "right of the people peaceably to assemble and to petition",
                    "the Government for a redress of grievances")

aCorpus <- Corpus(VectorSource(someCleanText))   #With this, only 1-Grams are created
#aCorpus <- VCorpus(VectorSource(someCleanText)) #With this, biGrams are created as desired

BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min=2, max=2))

aTDM <- TermDocumentMatrix(aCorpus, control=list(tokenize=BigramTokenizer))

print(aTDM$dimnames$Terms)

Результат с «Корпус»

 [1] "congress"      "establishment" "law"           "make"         
 [5] "respecting"    "shall"         "exercise"      "free"         
 [9] "prohibiting"   "religion"      "the"           "thereof"      
[13] "abridging"     "freedom"       "press"         "speech"       
[17] "and"           "assemble"      "peaceably"     "people"       
[21] "petition"      "right"         "for"           "government"   
[25] "grievances"    "redress"

Результат с VCorpus

 [1] "a redress"        "abridging the"    "an establishment" "and to"          
 [5] "assemble and"     "congress shall"   "establishment of" "exercise thereof"
 [9] "for a"            "free exercise"    "freedom of"       "government for"  
[13] "law respecting"   "make no"          "no law"           "of grievances"   
[17] "of speech"        "of the"           "or of"            "or prohibiting"  
[21] "or the"           "peaceably to"     "people peaceably" "press or"        
[25] "prohibiting the"  "redress of"       "religion or"      "respecting an"   
[29] "right of"         "shall make"       "speech or"        "the free"        
[33] "the freedom"      "the government"   "the people"       "the press"       
[37] "thereof or"       "to assemble"      "to petition"

Ответы на вопрос(1)

Ваш ответ на вопрос