2014-03-24 43 views
0

我正在Java Eclipse中创建一个工具来区分句子是否包含特定单词。针对特定单词读取文本文件

我正在使用twitter4j工具来搜索twitter中的推文。

我已经使用了stanford NLP tagger来标记twitter上的推文。然后将其存储在文本文件中。

下面是代码

public class TextTag { 

public static void main(String[] args) throws IOException, 
ClassNotFoundException { 

String tagged; 

// Initialize the tagger 
MaxentTagger tagger = new MaxentTagger("taggers/english-left3words-distsim.tagger"); 

// The sample string 
String sample = "Output Tagged"; 

//The tagged string 
tagged = tagger.tagString(sample); 

//output the tagged sample string onto your console 
//System.out.println(tagged); 

/*pick up some sentences from the file ouput.txt and store the output of 
tagged sentences in another file EntityTagged.txt. */ 

FileInputStream fstream = new FileInputStream("Output.txt"); 
DataInputStream in = new DataInputStream(fstream); 
BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

//we will now pick up sentences line by line from the file ouput.txt and store it in the string sample 
while((sample = br.readLine())!=null) 
{ 
//tag the string 
tagged = tagger.tagString(sample); 
FileWriter q = new FileWriter("EntityTagged.txt",true); 
BufferedWriter out =new BufferedWriter(q); 
//write it to the file EntityTagged.txt 
out.write(tagged); 
out.newLine(); 
out.close(); 

} 

我的下一个步骤是从EntityTagged.txt使用标记的微博,并用积极的词和否定词的字符串比较这些。

我已经创建了2个文本文件,一个正面单词列表和一个负面单词列表,我的目标是通过'EntityTagged.txt“文件中的10个不同标记的推文,针对positive.txt和负面.txt文件,以找出一个词来,这样即使在鸣叫是积极还是消极

我的最终结果应该有我可以区分

分享Tweet 1:积极 Tweet 2个:负 分享Tweet 3:负

etc

目前,我正在努力创造一种能够实现这个

任何帮助将非常感激

谢谢

回答

0

这是我五分钟算法的算法。将正面和负面词语存储为分隔字符串。然后循环播放推文中的文字,看看它们是否存在于分隔字符串中。您必须展开正则表达式以包含所有特殊字符:

String positiveWords = "|nice|happy|great|"; 
positiveWords = positiveWords.toLowerCase(); 

String negativeWords = "|bad|awful|mean|yuck|sad|"; 
negativeWords = negativeWords.toLowerCase(); 

String tweetOne = "nice day happy not sad at all"; 
tweetOne = tweetOne.toLowerCase(); 

String[] arrWords = tweetOne.split("\\s"); 
int value = 0; 
for (int i=0; i < arrWords.length; i++) { 

    if (positiveWords.indexOf("|"+arrWords[i]+"|") != -1) { 
     System.out.println("POS word(+1): " + arrWords[i]); 
     value++; 
    } 
    if (negativeWords.indexOf("|"+arrWords[i]+"|") != -1) { 
     System.out.println("NEG word(-1): " + arrWords[i]); 
     value--; 
    }    
} 

System.out.println("positive/negative value: " + value); 
+0

非常感谢您的帮助。我如何阅读文本文件,我已经创建了积极的话,消极的话和鸣叫 – user3406318

+0

我在GitHub上有一个例子:https://github.com/CoachEd/JavaExamples/tree/master/ReadTextFileExample。这将逐行读取文本文件。然后你可以相应地解析每一行。 –

相关问题