2017-04-17 2 views
0

Je travaille sur l'exploration de texte en utilisant R, je voudrais identifier si certains mots précèdent mon mot-clé focal de trois mots ou moins. Par exemple, mon mot-clé focal est compatibilité et je voulais savoir si le mot limité précède mon mot-clé de trois mots ou moins. Ainsi, je voulais obtenir le nombre de fréquences dans un texte en ce qui concerne le nombre de fois la combinaison suivante apparaît (X = tout autre mot):R text mining - comment identifier le mot qui précède le mot-clé

  • compatibilité limitée
  • compatibilité X limitée
  • limitée XX Compatibilité

Toutes les suggestions sont les bienvenues. Merci.

Répondre

0

est ici une approche à l'aide tidytext pour trouver sauter ngrams:

library(tidyverse) 
library(tidytext) 

x <- 'I am working on text mining using R, I would like to identify if some words precede my focal keyword by three or fewer words. For instance, my focal keyword is compatibility and I wanted to know if the word limited precedes my keyword by three or fewer words. Thus, I wanted to get frequency count in a text regarding how many times the following combination appears (X=any other word): 

limited compatibility 
limited X compatibility 
limited X X compatibility 

Any suggestions are welcome. Thanks.' 

data_frame(x) %>% 
    unnest_tokens(line, x, 'lines') %>% 
    mutate(line_number = row_number()) %>% 
    unnest_tokens(ngram, line, 'skip_ngrams', n = 2, k = 2) %>% 
    filter(grepl('limited', ngram), grepl('compatibility', ngram)) 
#> # A tibble: 3 × 2 
#> line_number     ngram 
#>   <int>     <chr> 
#> 1   2 limited compatibility 
#> 2   3 limited compatibility 
#> 3   4 limited compatibility 
0

Voici une approche avec la base R et des expressions régulières.
grepRaw fournit la position de chaque motif regex correspondant (avec l'argument all = TRUE). La longueur de ce résultat fournit le nombre de correspondances.

d <- c(" 
Limited compatibility Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla maximus lobortis 
tellus quis egestas. Donec non dignissim urna. Praesent at commodo ligula. 
Cras laoreet limited compatibility interdum mi nec euismod. Ut interdum odio non sem luctus iaculis. Mauris id sapien limited X XXXX compatibility accumsan, imperdiet justo non,limited compatibility egestas felis. Morbi commodo lectus limited X compatibility scelerisque limited XXX compatibility est bibendum, vel varius tellus vulputate. Aenean dictum accumsan limited X compatibility neque limited X X compatibility sed dictum. Vivamus finibus lacus sit amet iaculis molestie. Fusce enim limited X compatibility sapien, iaculis quis leo non, pellentesque lobortis arcu. Proin commodo limited X XXX XXXXX compatibility velit placerat venenatis mattis. Limited compatibility Curabitur et laoreet ipsum. Limited compatibility 
") 

> length(grepRaw("Limited compatibility", d, ignore.case = TRUE, all = TRUE)) 
[1] 5 
> length(grepRaw("limited \\w+ compatibility", d, ignore.case = TRUE, all = TRUE)) 
[1] 4 
> length(grepRaw("limited (\\w+){2}compatibility", d, ignore.case = TRUE, all = TRUE)) 
[1] 2 
> length(grepRaw("limited (\\w+){3}compatibility", d, ignore.case = TRUE, all = TRUE)) 
[1] 1 

Le regex suivant correspond à motif « compatibilité neque compatibilité XX limitée limitée X » et ce n'est pas le comportement entended

> length(grepRaw("limited (\\w+){6}compatibility", d, ignore.case = TRUE, all = TRUE)) 
[1] 1 

Peut-être plus sûr alors de placer tous les « compatibilité limitée xx » sur une seule ligne :

d <- gsub("Limited", "\nLimited", d, ignore.case = TRUE) 
d <- gsub("compatibility", "compatibility\n", d, ignore.case = TRUE) 
# writeLines(d) 

Ceci est maintenant correct

> length(grepRaw("limited (\\w+){6}compatibility", d, ignore.case = TRUE, all = TRUE)) 
[1] 0