【问题标题】:reading text file against specific words针对特定单词读取文本文件
【发布时间】:2014-03-24 14:58:59
【问题描述】:

我正在 Java Eclipse 中创建一个工具,它可以区分句子是否包含特定单词。

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

我使用了 stanford NLP 标记器来标记来自 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 条不同标记的推文,对照正面和负面。 txt 文件来找出是否出现了一个词,这样我就可以区分推文是正面的还是负面的

我的最终结果应该有

推文 1:正面评价 推文2:负面 推文3:否定

目前,我正在努力创建一种可以实现此功能的算法

任何帮助将不胜感激

谢谢

【问题讨论】:

    标签: java twitter text-files stanford-nlp pos-tagger


    【解决方案1】:

    这是我的五分钟算法。将您的肯定词和否定词存储为分隔字符串。然后遍历推文中的单词以查看它们是否存在于分隔字符串中。您必须扩展拆分正则表达式以包含所有特殊字符:

    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);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2011-04-12
    • 2016-10-19
    相关资源
    最近更新 更多