【问题标题】:Stanford NLP for Sentiment analysis of tweets stages斯坦福 NLP 用于推文阶段的情绪分析
【发布时间】:2017-03-14 08:41:04
【问题描述】:

原始推文已按以下结构保存到文件中:

推文语言 ||推特

以下是我删除 URL、RT、用户名和任何非字母数字字符的预处理阶段。

def cleanTweets() {

    File dirtyTweets = new File("result.txt")
    File cleanTweets = new File("cleanTweets.txt")

    try {
        Scanner console = new Scanner(dirtyTweets)

        PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter(cleanTweets)))
        LinkedHashSet<String> ln = new LinkedHashSet<String>();

        while (console.hasNextLine()) {

            String line = console.nextLine();

            String[] splitter = line.split("\\|\\|\\|")
            //Only looks at the english tweets
            if (splitter[0] == "en") {

                line = line.replaceFirst("en", "")

                String urlIdentifier = "((http|ftp|https):\\/\\/)?[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?"

                //Removes URL's, RT, Twitter usernames and any non alpha numeric character
                String[] removeNoise = ["RT", urlIdentifier, "(?:\\s|\\A)[@]+([A-Za-z0-9-_]+)", "[^a-zA-Z0-9 ]"]

                removeNoise.each { noise ->
                    line = line.replaceAll(noise, "").toLowerCase()
                }
                ln.add(line)

            }
        }

        ln.each { line ->
            printWriter.write(line)
            printWriter.println()
        }
        //write to file here
    } catch (IOException e) {
    }
}

然后将其保存到新文件中。对这些推文进行情绪分析的下一阶段是什么?

【问题讨论】:

    标签: java twitter stanford-nlp


    【解决方案1】:

    这里是一些使用情感注释器的示例代码:

    package edu.stanford.nlp.examples;
    
    import edu.stanford.nlp.ling.*;
    import edu.stanford.nlp.pipeline.*;
    import edu.stanford.nlp.sentiment.*;
    import edu.stanford.nlp.util.*;
    import java.util.Properties;
    
    public class SentimentExample {
    
      public static void main(String[] args) {
        Annotation document = new Annotation("...insert tweet text here...");
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,sentiment");
        // you might want to enforce treating the entire tweet as one sentence
        //...if so uncomment the line below setting ssplit.eolonly to true
        // also make sure you remove newlines, this will prevent the
        // sentence splitter from dividing the tweet into different sentences
        //props.setProperty("ssplit.eolonly","true");
        props.setProperty("parse.binaryTrees","true");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        pipeline.annotate(document);
        for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
          System.out.println("---");
          System.out.println(sentence.get(CoreAnnotations.TextAnnotation.class));
          System.out.println(sentence.get(SentimentCoreAnnotations.SentimentClass.class));
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多