2017-04-19 5 views
0

Je suis familier avec l'utilisation de la bibliothèque tm pour créer un tdm et compter les fréquences des termes.Text Mining - Count Fréquences de phrases (plus d'un mot)

Mais ces termes sont tous à un mot.

Comment peut-on compter le nombre de fois qu'une phrase composée de plusieurs mots apparaît dans un document et/ou un corpus?

EDIT:

J'ajoute le code que je dois maintenant améliorer/clarifier mon poste.

C'est assez code standard pour construire une matrice terme-document:

library(tm) 


cname <- ("C:/Users/George/Google Drive/R Templates/Gospels corpus") 

corpus <- Corpus(DirSource(cname)) 

#Cleaning 
corpus <- tm_map(corpus, tolower) 
corpus <- tm_map(corpus, removeNumbers) 
corpus <- tm_map(corpus, removePunctuation) 
corpus <- tm_map(corpus, stripWhitespace) 
corpus <- tm_map(corpus, removeWords, c("a","the","an","that","and")) 

#convert to a plain text file 
corpus <- tm_map(corpus, PlainTextDocument) 

#Create a term document matrix 
tdm1 <- TermDocumentMatrix(corpus) 

m1 <- as.matrix(tdm1) 
word.freq <- sort(rowSums(m1), decreasing=T) 
word.freq<-word.freq[1:100] 

Le problème est que cela renvoie une matrice de seul mot termes, par exemple:

all  into  have  from  were  one  came  say  out 
    397  390  385  383  350  348  345  332  321 

Je veux être capable de rechercher des termes multi-mots dans le corpus à la place. Ainsi, par exemple, "vient de" au lieu de simplement "venu" et "de" séparément.

Merci.

+0

S'il vous plaît lire les informations sur [comment poser une bonne question] (http://stackoverflow.com/help/how-to-ask) et comment donner un [exemple reproductible] (http: // stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). Cela rendra beaucoup plus facile pour les autres de vous aider. – Jaap

Répondre

0

j'ai créé la fonction suivante pour obtenir mot n grammes et leurs fréquences correspondantes

library(tau) 
library(data.table) 
# given a string vector and size of ngrams this function returns  word ngrams with corresponding frequencies 
createNgram <-function(stringVector, ngramSize){ 

    ngram <- data.table() 

    ng <- textcnt(stringVector, method = "string", n=ngramSize, tolower = FALSE) 

    if(ngramSize==1){ 
    ngram <- data.table(w1 = names(ng), freq = unclass(ng), length=nchar(names(ng))) 
    } 
    else { 
    ngram <- data.table(w1w2 = names(ng), freq = unclass(ng), length=nchar(names(ng))) 
    } 
    return(ngram) 
} 

d'une chaîne comme

text <- "This is my little R text example and I want to count the frequency of some pattern (and - is - my - of). This is my little R text example and I want to count the frequency of some patter." 

Voici comment appeler la fonction pour une paire de mots, des phrases de longueur 3 passe 3 comme argument

res <- createNgram(text, 2) 

impression res sorties

  w1w2  freq length 
1:  I want 2  6 
2:  R text 2  6 
3:  This is 2  7 
4:   and I 2  5 
5:  and is 1  6 
6:  count the 2  9 
7: example and 2  11 
8: frequency of 2  12 
9:   is my 3  5 
10:  little R 2  8 
11:  my little 2  9 
12:   my of 1  5 
13:  of This 1  7 
14:  of some 2  7 
15: pattern and 1  11 
16: some patter 1  11 
17: some pattern 1  12 
18: text example 2  12 
19: the frequency 2  13 
20:  to count 2  8 
21:  want to 2  7 
+0

vous pouvez également trouver le [package tokenizers] (https://cran.r-project.org/web/packages/tokenizers/index.html) utile. Voir 'tokenize_ngram()' dans la documentation du paquet. –

0

Compte tenu du texte:

text <- "This is my little R text example and I want to count the frequency of some pattern (and - is - my - of). This is my little R text example and I want to count the frequency of some patter." 

Pour fréquence trouver des mots:

table(strsplit(text, ' ')) 


    -  (and  and  count example frequency   I  is little  my 
    3   1   2   2   2   2   2   3   2   3 
    of  of). patter. pattern   R  some  text  the  This  to 
    2   1   1   1   2   2   2   2   2   2 
want 
    2 

Pour connaître la fréquence d'un motif:

attr(regexpr('is', text), "match.length") 

[1] 3 
0

Voici un bel exemple avec du code utilisant Tidytext: https://www.kaggle.com/therohk/news-headline-bigrams-frequency-vs-tf-idf

La même technique peut être étendue à des valeurs n plus grandes.

bigram_tf_idf <- bigrams %>% 
    count(year, bigram) %>% 
    filter(n > 2) %>% 
    bind_tf_idf(bigram, year, n) %>% 
    arrange(desc(tf_idf)) 

bigram_tf_idf.plot <- bigram_tf_idf %>% 
    arrange(desc(tf_idf)) %>% 
    filter(tf_idf > 0) %>% 
    mutate(bigram = factor(bigram, levels = rev(unique(bigram)))) 

bigram_tf_idf.plot %>% 
    group_by(year) %>% 
    top_n(10) %>% 
    ungroup %>% 
    ggplot(aes(bigram, tf_idf, fill = year)) + 
    geom_col(show.legend = FALSE) + 
    labs(x = NULL, y = "tf-idf") + 
    facet_wrap(~year, ncol = 3, scales = "free") + 
    theme(text = element_text(size = 10)) + 
    coord_flip()