2015-01-21 4 views
1

removeWords()를 사용하여 패키지 tm을 사용한 텍스트 마이닝. 나는 수천 개의 합계 중 약 500 개의 관련 단어 목록을 가지고있다. removeWords()를 사용하여 논리를 뒤집어 목록에없는 단어를 코퍼스에서 제거 할 수 있습니까? 펄텍스트 마이닝 (tm 포함), 목록에없는 단어 제거

, 나는 이런 식으로 뭔가를 할 수 :

text <- tm_map(text, removeWords, wordList) 

은 무엇과 같은 일을위한 올바른 구문 될 것이다 :

R에서
$diminishedText = (fullText =! s/$wordlist//g); #not tested 

,이 단어 목록에서 단어를 제거 이?

text <- tm_map(text, removeWords, not in wordList) 
+3

최소의 작동 예제가 예상됩니다. 데이터 집합 (이 경우 코퍼스)과 R 벡터 형식으로 유지할 단어 목록입니다. ** tm ** 패키지에는 사용할 수있는 코퍼스 데이터가 내장되어 있습니다. 이 작업을 수행하지 않으면 닫힌 질문의 근거가됩니다. –

+0

OP가 최소한의 작업 예제를 제공하지 않았으므로이 질문을 끝내기로했습니다. –

답변

1

이것은 꽤 느껴지지만 작동 할 수도 있습니다. 다른 가능성은 결국 있습니다. 여기

keepWords <- c("oil", "crude", "tanker") # choose the words to keep from the starting documents 
keeppattern <- paste0(keepWords, collapse = "|") # create a regex pattern of the keepWords 
Text <- unlist(str_extract_all(string = docs, pattern = keeppattern)) # remove only the keepWords, as a vector 
Text.tdm <- TermDocumentMatrix(Text) # create the tdm based on keepWords only 

다른 가능성 [%의 keepWords에 rownames (Text.tdm) %] Text.tdm 있지만 - tdm.keep < :

library(tm) 
library(qdap); library(gtools) 
library(stringr) 

docs <- c("cat", "dog", "mouse", "oil", "crude", "tanker") # starting documents 

EDIT 는 I이 방법 가로 질러 나는 그것을 통해 작동하지 않았다. R remove stopwords from a character vector using %in%

편집 : 또 다른 방법 :

tdm.keep <- Text.tdm[rownames(Text.tdm)%in%keepWords, ] 

'%nin%' <- Negate('%in%') # assign to an operator the opposite of %in% 
Text <- tm_map(crude, removeWords(crude %nin% keepWords)) 
# Error because removeWords can't take a logical argument 
+0

이 대답이 도움이 되었습니까? – lawyeR

0

어쩌면 당신이 그것을 힘을 짐승 수 있습니다.

사전을 다운로드하고 wordList에있는 단어를 제거하십시오.

tm_map()에 해당 사전을 전달해보세요.

1

텍스트 분석 패키지 quanteda에는 양수 (유지)와 제외 (제거) 기능 선택을위한 기능이 있습니다. 여기에 우리가 경제 단어 만 세트를 유지하려는 예는 미국 대통령 취임 코퍼스에서입니다 : 여기

require(quanteda) 
dfm(inaugTexts[50:57], keptFeatures = c("tax*", "econom*", "mone*"), verbose = FALSE) 
# Document-feature matrix of: 8 documents, 5 features. 
# 8 x 5 sparse Matrix of class "dfmSparse" 
#    features 
# docs   economic taxes tax economy money 
# 1985-Reagan   4  2 4  5  1 
# 1989-Bush   0  0 0  0  1 
# 1993-Clinton  0  0 0  3  0 
# 1997-Clinton  0  0 0  2  0 
# 2001-Bush   0  1 0  2  0 
# 2005-Bush   1  0 0  0  0 
# 2009-Obama   0  0 0  3  0 
# 2013-Obama   2  0 1  1  0 

경기는 기본 "글로브"형식을 사용했지만, 고정 및 정규 표현식 기능에 대한 일치 선택도 가능합니다. ?dfm?selectFeatures을 참조하십시오.

관련 문제