繁体   English   中英

针对特定单词读取文本文件

[英]reading text file against specific words

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

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

我使用了斯坦福大学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个文本文件,一个肯定词列表和一个否定词列表,我的目标是针对“ positive.txt”和“ negative.txt”文件循环遍历“ EntityTagged.txt”文件中的10个不同的已标记推文。找出一个词是否出现,以便我可以区分这些推文是正面的还是负面的

我的最终结果应该是

Tweet 1:正面Tweet 2:负面Tweet 3:负面

等等

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

任何帮助都感激不尽

谢谢

这是我的五分钟算法。 将您的肯定和否定词存储为定界字符串。 然后在推文中循环浏览单词,以查看它们是否存在于分隔字符串中。 您必须将split regex扩展为包括所有特殊字符:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM