2016-06-16 7 views
3

나는 이것이 상당히 쉬워야한다고 생각합니다. 나는 현재 정규 표현식으로 변환 한 globs 형식의 용어 사전을 가지고 있습니다. 정규 표현식으로 변환 한 이유는 tm 패키지가 그 패키지에서만 작동하기 때문입니다. 괜찮아. 하지만 여러 사전 용어를 전달하여 termDocumentMatrix를 부분 집합하는 방법을 알아낼 수는 없습니다. 이것에 대한 또 다른 단서는 사전 용어가 여러 길이를 갖고, 일부는 1이고, 일부는 2이고, 일부는 3 단어입니다.정규식 사전으로 TermDocumentMatrix를 필터링하십시오.

다음은 현재 코드입니다.

#load libraries 
library(tm) 
library(stringi) 
#Load corpus crude part of tm package 
data(crude) 
#make tokenizer to account for multi-word dictionaries 
myTokenizer <- 
function(x) 
unlist(lapply(ngrams(words(x), 1:3), paste, collapse = " "), 
use.names = FALSE) 
#make TermDocumentMatrix 
tdm<-TermDocumentMatrix(crude, control=list(tokenizer=myTokenizer)) 
#Make dictionary of regular expressions 
dict<-c('^also$', '^told reuters$', '^an emergency$', '^in world oil$') 
#This is what I am working with 
inspect(
tdm[sapply(dict, function(x) stri_detect_regex(tdm$dimnames$Terms,  
pattern=x)),] 
) 
+0

은 앞의 단어 문자 값의 맨 끝에있는 경우에만 일치가있을 것이라는 점을 의미 추가 캐럿 의미 만 정확히 일치하는 것이다. 그 마커를 모두 제거하고 싶을 수도 있습니다. –

+0

그래, 그걸로 놀 수 있지만, 공급의 핵심 문제는 어떨까? 여러 정규 표현식을 사용하여 TermDocumentMatrix 필터링 – spindoctor

답변

1

나는 지금 crude 데이터 세트가 테스트를 허용하지 그 패키지 중 하나의 일부는 사실을 알게 될 것입니다. 이 패턴에서 캐럿과 달러 기호를 제거하는 항목의 훨씬 더 큰 숫자가 목표를 일치 발견 할 수 있는지 보여줍니다 당신이 GREP를 사용하는 경우이 사람이 일치되는 볼 수 있습니다

> sum(sapply(dict, grepl, x=tdm$dimnames$Terms)) 
[1] 4 
> dict2<-c('also', 'told reuters', 'an emergency', 'in world oil') 
> sum(sapply(dict2, grepl, x=tdm$dimnames$Terms)) 
[1] 51 

. TDM을 $ $ 약관 dimnames로 (grepl의 결과는 한 4 timesd 것 :

> sapply(dict2, grep, x=tdm$dimnames$Terms) 
$also 
[1] 707 708 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 
[19] 754 1485 1486 2434 2881 2882 2988 2989 3399 3400 3782 3983 5265 5995 6088 6382 6383 6893 
[37] 7427 7428 7524 7525 7605 

$`told reuters` 
[1] 3013 7209 7210 

$`an emergency` 
[1] 779 780 781 2437 2642 4205 

$`in world oil` 
[1] 3276 

TDM의의 인쇄 방법은 특히 정보되지 않습니다,하지만 당신은 내부에 뭐가 있는지 dput으로 값을 "폭발"할 수 있습니다 : 정규식 패턴의 끝에 달러 기호를 넣는

> dput(tdm[ sapply(dict2, grepl, x=tdm$dimnames$Terms), ]) 
structure(list(i = c(1L, 2L, 3L, 8L, 9L, 33L, 3L, 16L, 17L, 20L, 
21L, 32L, 3L, 6L, 7L, 22L, 39L, 40L, 3L, 14L, 15L, 36L, 37L, 
38L, 3L, 12L, 13L, 27L, 28L, 41L, 3L, 10L, 11L, 25L, 26L, 30L, 
3L, 4L, 5L, 23L, 24L, 31L, 3L, 4L, 5L, 23L, 24L, 31L, 3L, 18L, 
19L, 29L, 34L, 35L), j = c(6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
10L, 10L, 10L, 10L, 10L, 14L, 14L, 14L, 14L, 14L, 14L, 16L, 16L, 
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 
18L, 18L, 18L), v = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
    nrow = 41L, ncol = 20L, dimnames = structure(list(Terms = c("ali also", 
    "ali also delivered", "also", "also called", "also called for", 
    "also contributed", "also contributed to", "also delivered", 
    "also delivered \"a", "also denied", "also denied that", 
    "also nigerian", "also nigerian oil", "also no", "also no projection", 
    "also reviews", "also reviews the", "also was", "also was lowered", 
    "but also", "but also reviews", "european weekend also", 
    "group, also", "group, also called", "he also", "he also denied", 
    "is also", "is also nigerian", "louisiana sweet also", "meeting.\" he also", 
    "private group, also", "sector, but also", "sheikh ali also", 
    "sweet also", "sweet also was", "there was also", "was also", 
    "was also no", "weekend also", "weekend also contributed", 
    "who is also"), Docs = c("127", "144", "191", "194", "211", 
    "236", "237", "242", "246", "248", "273", "349", "352", "353", 
    "368", "489", "502", "543", "704", "708")), .Names = c("Terms", 
    "Docs"))), .Names = c("i", "j", "v", "nrow", "ncol", "dimnames" 
), class = c("TermDocumentMatrix", "simple_triplet_matrix"), weighting = c("term frequency", 
"tf"))