簡體   English   中英

Stanford NLP用於推文階段的情感分析

[英]Stanford NLP for Sentiment analysis of tweets stages

原始推文已按以下結構保存到文件中:

推文語言|| 鳴叫

以下是我的預處理階段,以刪除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) {
    }
}

然后將其保存到新文件中。 下一步將如何分析這些推文?

以下是一些使用情感注釋器的示例代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM