2015-02-09 32 views
-1

我正在尝试使用R进行情感分析。我的代码如下。 我试图提取积极和消极的话来创建词云。因此我试图获取输入文件中的正面和负面词汇列表。使用循环将值加载到列表中

library(tm) 
library(wordcloud) 
library(plyr) 
library(sentiment) 
require(stringr) 
pos.words=scan('positive-words.txt',what='character',comment.char=';') 
neg.words=scan('negative-words.txt',what='character',comment.char=';') 

data <- readLines("input.txt") 

''' 
clean the input 

''' 


# split into words. str_split is in the stringr package 

word.list = str_split(cleaned, '\\s+') 

# sometimes a list() is one level of hierarchy too much 

words = unlist(word.list) 


# compare our words to the dictionaries of positive & negative terms 

pos.matches = match(words, pos.words) 

neg.matches = match(words, neg.words) 

# match() returns the position of the matched term or NA 

# we just want a TRUE/FALSE: 

pos.matches = !is.na(pos.matches) 

neg.matches = !is.na(neg.matches) 

这是for循环我试图实现以提取负面词语。但我只是从名单中得到一个字。

for (w in neg.matches) 
    if(!is.na(w)) 
    negit <- neg.words[w] 

有没有更好的方法来做到这一点?

+1

用户错误:您没有对分配的LHS进行任何索引。我不知道这应该如何索引,因为这是一个常见的错误。但索引它可能没有帮助,因为没有错误抛出。 – 2015-02-09 03:57:46

+0

请参阅下面的答案 – dagan 2015-02-09 06:15:09

+0

如果这应该是一个答案,它仍然不清楚问题是什么。 – 2015-02-09 07:43:59

回答

0
count = 0 
for (w in neg.matches) 
{ 
    if(!is.na(w)) 
    {  
    count = count + 1 
    } 

} 

negit = array(dim=c(count-1)) 
i = 0 
for (w in neg.matches) 
{ 
    if(!is.na(w)) 
    { 
    negit[i] <- neg.words[w] 
    i = i + 1 
    } 

} 

count = 0 
for (w in pos.matches) 
{ 
    if(!is.na(w)) 
    {  
    count = count + 1 
    } 

} 

posit = array(dim=c(count-1)) 
i = 0 
for (w in pos.matches) 
{ 
    if(!is.na(w)) 
    { 
    posit[i] <- pos.words[w] 
    i = i + 1 
    } 

}