Nlp 情感分析(SentiWordNet)-判断句子的上下文

Nlp 情感分析(SentiWordNet)-判断句子的上下文,nlp,stanford-nlp,wordnet,sentiment-analysis,Nlp,Stanford Nlp,Wordnet,Sentiment Analysis,我试图在以下步骤中找出一个句子是肯定的还是否定的: 1.)使用斯坦福NLP解析器从句子中检索词性(动词、名词、形容词等) 2.)使用SentiWordNet查找与每个词性相关的正值和负值 3.)将获得的正值和负值相加,以计算与句子相关的净正值和净负值 但问题是,SentiWordNet返回一个基于不同感官/上下文的正负值列表。是否可以将特定句子连同词性一起传递给SentiWordNet解析器,以便它能够自动判断词义/上下文并只返回一对正负值 或者这个问题还有其他的解决方案吗 谢谢。我们可以将po

我试图在以下步骤中找出一个句子是肯定的还是否定的:

1.)使用斯坦福NLP解析器从句子中检索词性(动词、名词、形容词等)

2.)使用SentiWordNet查找与每个词性相关的正值和负值

3.)将获得的正值和负值相加,以计算与句子相关的净正值和净负值

但问题是,SentiWordNet返回一个基于不同感官/上下文的正负值列表。是否可以将特定句子连同词性一起传递给SentiWordNet解析器,以便它能够自动判断词义/上下文并只返回一对正负值

或者这个问题还有其他的解决方案吗


谢谢。

我们可以将pos传递给sentiwordnet解析器。 下载模式python模块

from pattern.en import wordnet

print wordnet.synsets("kill",pos="VB")[0].weight
wordnet.synsets返回语法集列表 从中我们选择第一项 输出将是(极性、主观性)的元组 希望这有助于…

这可能对你有帮助

//    Copyright 2013 Petter Törnberg
//
//    This demo code has been kindly provided by Petter Törnberg <pettert@chalmers.se>
//    for the SentiWordNet website.
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SentiWordNetDemoCode {

    private Map<String, Double> dictionary;

    public SentiWordNetDemoCode(String pathToSWN) throws IOException {
        // This is our main dictionary representation
        dictionary = new HashMap<String, Double>();

        // From String to list of doubles.
        HashMap<String, HashMap<Integer, Double>> tempDictionary = new HashMap<String, HashMap<Integer, Double>>();

        BufferedReader csv = null;
        try {
            csv = new BufferedReader(new FileReader(pathToSWN));
            int lineNumber = 0;

            String line;
            while ((line = csv.readLine()) != null) {
                lineNumber++;

                // If it's a comment, skip this line.
                if (!line.trim().startsWith("#")) {
                    // We use tab separation
                    String[] data = line.split("\t");
                    String wordTypeMarker = data[0];

                    // Example line:
                    // POS ID PosS NegS SynsetTerm#sensenumber Desc
                    // a 00009618 0.5 0.25 spartan#4 austere#3 ascetical#2
                    // ascetic#2 practicing great self-denial;...etc

                    // Is it a valid line? Otherwise, through exception.
                    if (data.length != 6) {
                        throw new IllegalArgumentException(
                                "Incorrect tabulation format in file, line: "
                                        + lineNumber);
                    }

                    // Calculate synset score as score = PosS - NegS
                    Double synsetScore = Double.parseDouble(data[2])
                            - Double.parseDouble(data[3]);

                    // Get all Synset terms
                    String[] synTermsSplit = data[4].split(" ");

                    // Go through all terms of current synset.
                    for (String synTermSplit : synTermsSplit) {
                        // Get synterm and synterm rank
                        String[] synTermAndRank = synTermSplit.split("#");
                        String synTerm = synTermAndRank[0] + "#"
                                + wordTypeMarker;

                        int synTermRank = Integer.parseInt(synTermAndRank[1]);
                        // What we get here is a map of the type:
                        // term -> {score of synset#1, score of synset#2...}

                        // Add map to term if it doesn't have one
                        if (!tempDictionary.containsKey(synTerm)) {
                            tempDictionary.put(synTerm,
                                    new HashMap<Integer, Double>());
                        }

                        // Add synset link to synterm
                        tempDictionary.get(synTerm).put(synTermRank,
                                synsetScore);
                    }
                }
            }

            // Go through all the terms.
            for (Map.Entry<String, HashMap<Integer, Double>> entry : tempDictionary
                    .entrySet()) {
                String word = entry.getKey();
                Map<Integer, Double> synSetScoreMap = entry.getValue();

                // Calculate weighted average. Weigh the synsets according to
                // their rank.
                // Score= 1/2*first + 1/3*second + 1/4*third ..... etc.
                // Sum = 1/1 + 1/2 + 1/3 ...
                double score = 0.0;
                double sum = 0.0;
                for (Map.Entry<Integer, Double> setScore : synSetScoreMap
                        .entrySet()) {
                    score += setScore.getValue() / (double) setScore.getKey();
                    sum += 1.0 / (double) setScore.getKey();
                }
                score /= sum;

                dictionary.put(word, score);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (csv != null) {
                csv.close();
            }
        }
    }

    public double extract(String word, String pos) {
        return dictionary.get(word + "#" + pos);
    }

    public static void main(String [] args) throws IOException {
        if(args.length<1) {
            System.err.println("Usage: java SentiWordNetDemoCode <pathToSentiWordNetFile>");
            return;
        }

        String pathToSWN = args[0];
        SentiWordNetDemoCode sentiwordnet = new SentiWordNetDemoCode(pathToSWN);

        System.out.println("good#a "+sentiwordnet.extract("good", "a"));
        System.out.println("bad#a "+sentiwordnet.extract("bad", "a"));
        System.out.println("blue#a "+sentiwordnet.extract("blue", "a"));
        System.out.println("blue#n "+sentiwordnet.extract("blue", "n"));
    }
}
//版权所有2013 Petter Törnberg
//
//此演示代码由Petter Törnberg提供
//对于SentiWordNet网站。
//
//此程序是免费软件:您可以重新发布和/或修改它
//它是根据GNU通用公共许可证的条款发布的
//自由软件基金会,或者许可证的第3版,或者
//(由您选择)任何更高版本。
//
//这个节目的发布是希望它会有用,
//但无任何保证;甚至没有任何关于
//适销性或适合某一特定目的。见
//有关更多详细信息,请参阅GNU通用公共许可证。
//
//您应该已经收到GNU通用公共许可证的副本
//和这个节目一起。如果没有,请参阅。
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Map;
公共类SentiWordNetDemoCode{
专用地图词典;
公共SentiWordNetDemoCode(字符串pathToSWN)引发IOException{
//这是我们主要的字典表示法
dictionary=newhashmap();
//从字符串到双精度列表。
HashMap tempDictionary=新HashMap();
BufferedReader csv=null;
试一试{
csv=新的BufferedReader(新的文件读取器(pathToSWN));
int lineNumber=0;
弦线;
而((line=csv.readLine())!=null){
lineNumber++;
//如果是评论,请跳过这一行。
如果(!line.trim(){
//我们使用制表符分隔
String[]data=line.split(“\t”);
字符串wordTypeMarker=数据[0];
//示例行:
//POS ID PosS NegS SynsetTerm#sensenumber Desc
//一个00009618 0.50.25斯巴达人#4严峻#3苦行#2
//禁欲主义者,修炼伟大的自我克制;等等
//它是有效行吗?否则,通过异常。
如果(data.length!=6){
抛出新的IllegalArgumentException(
文件中的表格格式不正确,第行:
+行号);
}
//将synset分数计算为分数=PosS-NegS
Double synstetscore=Double.parseDouble(数据[2])
-Double.parseDouble(数据[3]);
//获取所有语法集术语
字符串[]synTermsSplit=data[4]。拆分(“”);
//检查当前语法集的所有术语。
for(字符串synTermSplit:synTermsSplit){
//获取synterm和synterm等级
字符串[]synTermAndRank=synTermSplit.split(“#”);
字符串synTerm=synTermAndRank[0]+“#”
+字型标记;
int synTermRank=Integer.parseInt(synTermAndRank[1]);
//我们在这里得到的是以下类型的地图:
//术语->{synset的分数#1,synset的分数#2…}
//如果术语没有映射,则将映射添加到术语
if(!tempDictionary.containsKey(synTerm)){
tempDictionary.put(语法术语,
新HashMap());
}
//将synset链接添加到synterm
tempDictionary.get(synTerm).put(synTermRank,
合成核);
}
}
}
//通读所有条款。
for(Map.Entry:tempDictionary)
.entrySet()){
String word=entry.getKey();
Map synstetcoremap=entry.getValue();
//计算加权平均值。根据
//他们的级别。
//分数=1/2*第一名+1/3*第二名+1/4*第三名….等等。
//总和=1/1+1/2+1/3。。。
双倍得分=0.0;
双和=0.0;
对于(Map.Entry setScore:syntsetscoremap
.entrySet()){
分数+=setScore.getValue()/(双精度)setScore.getKey();
sum+=1.0/(双精度)setScore.getKey();
}
分数/=总和;
字典。放(字、分数);
}
}捕获(例外e){
e、 printStackTrace();
}最后{
如果(csv!=null){
csv.close();