利用Stanford Parser进行观点词否定词抽取
2012-08-14 15:25
204 查看
利用Stanford Parser进行观点词否定词抽取
问题:
接上一篇内容,当我们在文本中得到特征词的观点词之后,如果我们要做情感分析、极性判定(用户是在赞美还是批评),除了分析观点词本身的情感色彩之外,我们还需得到句子中是否有对该观点词的否定。如“我喜欢这个产品”->肯定;“我不喜欢这个产品”->否定。常见的否定词一般有“不”“无”“没有”等,否定词常常出现在观点词前面(不一定紧邻,有可能当中会隔着助词或副词等),因此如果光用统计的方法去判定准确率不高。
方法:
1.选择文本数据(数据源,如产品评论文本等)
2.对文本进行断句和分词
3.语法分析(Stanford Parser)
4.观点词抽取(参考前一篇内容)
5.否定词抽取。(利用stanford Parser返回的TypedDependency关系对象集合,查找当前观点词是否和其他词存在否定关系)
代码:
这里给的代码直接略过了前面几步,输入为:分词后的句子和观点词,输出:该观点词的否定词。
package textAnalysis;
import java.io.StringReader;
import java.util.Collection;
import java.util.List;
import edu.stanford.nlp.process.Tokenizer;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.Label;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.process.Tokenizer;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.trees.GrammaticalStructureFactory;
import edu.stanford.nlp.trees.PennTreebankLanguagePack;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreebankLanguagePack;
import edu.stanford.nlp.trees.TypedDependency;
import edu.stanford.nlp.trees.international.pennchinese.ChineseTreebankLanguagePack;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.parser.lexparser.Test;
public class NegWordExtra
{
public NegWordExtra(){
}
public static void main(String[]
args) {
LexicalizedParser lp =
new LexicalizedParser("grammar/chinesePCFG.ser.gz");
Test.MAX_ITEMS =
2000000000;
TreebankLanguagePack tlp = new ChineseTreebankLanguagePack();
GrammaticalStructureFactory
gsf = tlp.grammaticalStructureFactory();
String sentence = "这个 产品 很 保湿 但 不 油腻";
String keyword = "油腻";
Tokenizer<? extends HasWord>
toke = tlp.getTokenizerFactory()
.getTokenizer(new StringReader(sentence));
List<? extends HasWord>
sentList = toke.tokenize();
Tree parser = lp.apply(sentList);
GrammaticalStructure gs =
gsf.newGrammaticalStructure(parser);
Collection tdl = gs.typedDependenciesCollapsedTree();
//System.out.println(tdl);
for(int i
= 0;i < tdl.size();i ++){
TypedDependency td
= (TypedDependency)tdl.toArray()[i];
String
nodDep = td.dep().toString();
String
nodgov = td.gov().toString();
String
relation = td.reln().toString();
//int act
= -1;
if(nodgov.contains(keyword)
&& relation.equals("neg")){
//act
= td.gov().index();
System.out.println("Dep:" +
nodDep + ", Gov:" +
nodgov + " > relation:"+
relation);
}
}
}
}
注:本程序用的是Stanford 2011年9月14号发布的jar包和词库。因为发现在12年2月3号发的那个包把否定关系“neg”归为了副词修饰形容词的“advmod”关系了,所以没法得到否定词,具体情况还在学习研究中,有知道的同学也麻烦告诉我下。谢谢。
问题:
接上一篇内容,当我们在文本中得到特征词的观点词之后,如果我们要做情感分析、极性判定(用户是在赞美还是批评),除了分析观点词本身的情感色彩之外,我们还需得到句子中是否有对该观点词的否定。如“我喜欢这个产品”->肯定;“我不喜欢这个产品”->否定。常见的否定词一般有“不”“无”“没有”等,否定词常常出现在观点词前面(不一定紧邻,有可能当中会隔着助词或副词等),因此如果光用统计的方法去判定准确率不高。
方法:
1.选择文本数据(数据源,如产品评论文本等)
2.对文本进行断句和分词
3.语法分析(Stanford Parser)
4.观点词抽取(参考前一篇内容)
5.否定词抽取。(利用stanford Parser返回的TypedDependency关系对象集合,查找当前观点词是否和其他词存在否定关系)
代码:
这里给的代码直接略过了前面几步,输入为:分词后的句子和观点词,输出:该观点词的否定词。
package textAnalysis;
import java.io.StringReader;
import java.util.Collection;
import java.util.List;
import edu.stanford.nlp.process.Tokenizer;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.ling.Label;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.process.Tokenizer;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.trees.GrammaticalStructureFactory;
import edu.stanford.nlp.trees.PennTreebankLanguagePack;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreebankLanguagePack;
import edu.stanford.nlp.trees.TypedDependency;
import edu.stanford.nlp.trees.international.pennchinese.ChineseTreebankLanguagePack;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.parser.lexparser.Test;
public class NegWordExtra
{
public NegWordExtra(){
}
public static void main(String[]
args) {
LexicalizedParser lp =
new LexicalizedParser("grammar/chinesePCFG.ser.gz");
Test.MAX_ITEMS =
2000000000;
TreebankLanguagePack tlp = new ChineseTreebankLanguagePack();
GrammaticalStructureFactory
gsf = tlp.grammaticalStructureFactory();
String sentence = "这个 产品 很 保湿 但 不 油腻";
String keyword = "油腻";
Tokenizer<? extends HasWord>
toke = tlp.getTokenizerFactory()
.getTokenizer(new StringReader(sentence));
List<? extends HasWord>
sentList = toke.tokenize();
Tree parser = lp.apply(sentList);
GrammaticalStructure gs =
gsf.newGrammaticalStructure(parser);
Collection tdl = gs.typedDependenciesCollapsedTree();
//System.out.println(tdl);
for(int i
= 0;i < tdl.size();i ++){
TypedDependency td
= (TypedDependency)tdl.toArray()[i];
String
nodDep = td.dep().toString();
String
nodgov = td.gov().toString();
String
relation = td.reln().toString();
//int act
= -1;
if(nodgov.contains(keyword)
&& relation.equals("neg")){
//act
= td.gov().index();
System.out.println("Dep:" +
nodDep + ", Gov:" +
nodgov + " > relation:"+
relation);
}
}
}
}
注:本程序用的是Stanford 2011年9月14号发布的jar包和词库。因为发现在12年2月3号发的那个包把否定关系“neg”归为了副词修饰形容词的“advmod”关系了,所以没法得到否定词,具体情况还在学习研究中,有知道的同学也麻烦告诉我下。谢谢。
相关文章推荐
- 利用Stanford Parser进行中文观点抽取(附代码)
- 利用Stanford Parser进行中文行为抽取
- 利用Stanford Parser进行中文行为抽取
- 利用Gate进行中文信息抽取时 不能识别Token.string=="中文"的问题
- 基于python借助百度云API对评论进行观点抽取
- 利用正则进行sql解析
- 定义数组为有序数组模型,并利用二分法进行查找,删除特定值(缺点:只能删除一个值,若有相同的值,则删除第一个)
- HDU 5242 利用树链剖分思想进行贪心
- 利用正则表达式进行修改自定义ini配置文件
- 利用python进行数据分析读书笔记
- 利用Hog特征和SVM分类器进行行人检测
- 在Azure HDInsight中创建R服务器,并利用Spark集群进行分布式R分析
- 利用nodejs进行大文件(比如在线视频)的读取
- 利用BitMap进行大数据排序去重
- [置顶] Windows 下利用openwrt网关进行wireshark抓包
- ExcelVBA:利用SUMIF和OFFSET+MATCH进行多条件求和
- 利用jquery 的jsonp 进行跨域访问
- AdaBoost中利用Haar特征进行人脸识别算法分析与总结1——Haar特征与积分图
- AndroidStudio使用JDBC并利用Mysql6.0进行增删改查
- RedHat Linux下利用sersync进行实时同步数据