2014-03-24 41 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。這將逐行讀取文本文件。然後你可以相應地解析每一行。 –

相關問題