2014-10-17 1 views
0

안녕하세요 트위터 검색을 통해 얻은 결과에서 단어를 제거하는 데 도움이 필요합니다. 여기에 제가 사용하는 코드가 있습니다.내 코퍼스 결과에서 스톱 워드를 제거 할 수 없습니다. R 프로그래밍

library("twitteR") 
library("ROAuth") 
cred$handshake() 
save(cred, file="twitter.Rdata") 
load("twitter.Rdata") 
registerTwitterOAuth(cred) 
tweets = searchTwitter('#apple', n = 100, lang = "en") 
tweets.df = twListToDF(tweets) 
names(tweets.df) 
tweets.df$text 
tweet.words = strsplit(tweets.df$text, "[^A-Za-z]+") 
word.table = table(unlist(tweet.words)) 
library("tm") 
myStopwords <- c(stopwords('english'), "#apple","http://") 
tweet.corpus = Corpus(VectorSource(tweets.df$text)) 
tweet.corpus = tm_map(tweet.corpus,function(x) iconv(x, to='UTF8', sub='byte')) 
tweet.corpus = tm_map(tweet.corpus, PlainTextDocument) 
tweet.corpus = tm_map(tweet.corpus,removeWords, myStopwords) 
tweet.dtm = DocumentTermMatrix(tweet.corpus) 
tweet.matrix = inspect(tweet.dtm) 

그러나 문제는 그것입니다 #apple를 포함하는 결과를 제거되지 않으며, 웹 사이트 주소는 http를을 포함 : // 코퍼스에서, 나는 이러한 결과를 제거하는 방법? 도와 줘서 고마워, 매트.

+0

(HTTP : //stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). 문제는'tm' 명령에 실제로 있기 때문에 우리가 실행할 수없는 모든 twitterR 코드를 포함하여 적절한 자격 증명이 없기 때문에 실제로 도움이되지 않습니다. 문제를 재현하기 위해 다른 사람들이 복사/붙여 넣기 할 수있는 샘플 데이터를 포함해야합니다. – MrFlick

답변

2

removeWords은 실제로 기호가 아닌 "단어"를 제거하려고합니다. 실제로이

function (x, words) 
gsub(sprintf("(*UCP)\\b(%s)\\b", paste(words, collapse = "|")), 
    "", x, perl = TRUE) 

같은 정규 표현식을 통해 작동 그래서 단어의 벡터 정규 표현식 | (또는) 연산자를 통해 그들을 붕괴하고 그 조건을 제거합니다. "단어 문자"와 "단어가 아닌 문자"사이의 길이가 0 인 "단어 경계"와 일치하는 일치하는 표현식을 \b에 래핑합니다. 귀하의 조건에 대한 문제는 #/이 단어가 아닌 문자로 적합하므로 경계가 일치하지 않으며 해당 단어가 대체되지 않는 것입니다.

미친 기호를 제거해야하는 경우 일치하는 조건에 대해 더 명확하게 설명 할 수있는 자신 만의 콘텐츠 trasnformer를 작성하는 것이 좋습니다. 예를

를 들어
myremove <- content_transformer(function(x, ...) { 
    gsub("(#apple\\b|\\bhttp://)","",x, perl=TRUE) 
}) 

은 그럼 당신은 따라서 우리는 우리의 추가 변환 단계를 추가

tweets<-c("test one two", "two apples","hashtag #apple", "#apple #tree", "http://microsoft.com") 

library("tm") 
tweet.corpus = Corpus(VectorSource(tweets)) 
tweet.corpus = tm_map(tweet.corpus,content_transformer(function(x) iconv(x, to='UTF8', sub='byte'))) 
tweet.corpus = tm_map(tweet.corpus,removeWords, stopwords('english')) 
tweet.corpus = tm_map(tweet.corpus,myremove) 
tweet.dtm = DocumentTermMatrix(tweet.corpus) 
inspect(tweet.dtm) 

# <<DocumentTermMatrix (documents: 5, terms: 7)>> 
# Non-/sparse entries: 8/27 
# Sparsity   : 77% 
# Maximal term length: 13 
# Weighting   : term frequency (tf) 
# 
#  Terms 
# Docs #tree apples hashtag microsoft.com one test two 
# 1  0  0  0    0 1 1 1 
# 2  0  1  0    0 0 0 1 
# 3  0  0  1    0 0 0 0 
# 4  1  0  0    0 0 0 0 
# 5  0  0  0    1 0 0 0 

을 할 수있는 우리는 그 용어가 문서 용어 행렬에서 제거됩니다 볼 수 있습니다.

0

상기 (# 사과/URL) 제거가 발생하고 코퍼스가 구축되는 방법을 때 변경 qdap를 사용하여 약간 다른 접근 방식 : 당신은 정말 [재현 예]를 만들 수있는 시간을 소요

library(qdap); library(tm) 

dat <- data.frame(
    person = paste0("person_", 1:5), 
    tweets = c("test one two", "two apples","hashtag #apple", 
     "#apple #tree", "http://microsoft.com") 
) 

## remove specialty items 
dat[["tweets"]] <- rm_default(dat[["tweets"]], pattern=pastex("@rm_url", "#apple\\b")) 


myCorp <- tm_map(myCorp, removeWords, stopwords) 
myCorp %>% as.dtm() %>% tm::inspect() 

## <<DocumentTermMatrix (documents: 5, terms: 7)>> 
## Non-/sparse entries: 8/27 
## Sparsity   : 77% 
## Maximal term length: 13 
## Weighting   : term frequency (tf) 
## 
##   Terms 
## Docs  #tree apples hashtag microsoft.com one test two 
## person_1  0  0  0    0 1 1 1 
## person_2  0  1  0    0 0 0 1 
## person_3  0  0  1    0 0 0 0 
## person_4  1  0  0    0 0 0 0 
## person_5  0  0  0    1 0 0 0 
관련 문제