当前位置: 首页 > 科技观察

使用深度学习模型在Java中执行文本情感分析_0

时间:2023-03-15 20:37:48 科技观察

正面?消极的?中性的?只需几行代码,就可以使用StanfordCoreNLP组件分析句子。本文展示了如何使用集成到StanfordCoreNLP中的情感工具在Java中实现这样的任务,StanfordCoreNLP是一个用于自然语言处理的开源库。StanfordCoreNLP情感分类器要执行情感分析,您需要一个情感分类器,这是一种可以根据从训练数据集中学习到的预测来识别情感信息的工具。在StanfordCoreNLP中,情感分类器建立在在StanfordSentimentTreebank(SST)上训练的递归神经网络(RNN)深度学习模型之上。SST数据集是一个带有情感标签的语料库,其中每个句法上可能的短语都是从数千个使用过的句子中推导出来的,从而可以捕获文本中情感的构成效果。简而言之,这允许模型根据单词如何形成短语的含义来识别情绪,而不是仅仅通过孤立地评估单词。为了更好地了解SST数据集的结构,您可以从StanfordCoreNLP情感分析页面下载数据集文件。在Java代码中,斯坦福CoreNLP情感分类器的使用如下。首先,您通过添加执行情感分析(例如标记化、拆分、解析和情感)所需的注释器来构建文本处理管道。就StanfordCoreNLP而言,注释器是对注释对象进行操作的接口,其中后者代表文档中的一段文本。例如,需要使用ssplit注释器将标记序列拆分为句子。StanfordCoreNLP在每个句子的基础上计算情绪。因此,将文本分割成句子的过程总是伴随着应用情感注释器。一旦文本被分割成句子,解析注释器就会执行句法依赖解析,为每个句子生成依赖表示。然后,情绪注释器处理这些依赖表示,将它们与底层模型进行比较,以构建一个带有每个句子的情绪标签(注释)的二叉树。简而言之,树的节点由输入句子的标记标识,并且包含指示从句子派生的所有短语的五个情感类别中的预测类别的注释,范围从非常消极到非常积极。基于这些预测,情感标注器计算整个句子的情感。设置StanfordCoreNLP在开始使用StanfordCoreNLP之前,您需要进行以下设置:要运行StanfordCoreNLP,您需要Java1.8或更高版本。下载StanfordCoreNLP包并将该包解压缩到您计算机上的本地文件夹中。下载地址:https://nlp.stanford.edu/software/stanford-corenlp-latest.zip本文以将上述代码解压到以下目录为例:c:/softwareInstall/corenlp/stanford-corenlp-4.3。2完成以上步骤后,就可以创建一个运行StanfordCoreNLP流水线来处理文本的Java程序了。首先新建一个maven项目,手动添加stanford-corenlp-4.3.2到Libraries:在下面的例子中,你将实现一个简单的Java程序,它运行StanfordCoreNLP管道来解析包含多个句子的文本,做情感分析。首先,实现一个NlpPipeline类,它提供了一个方法来初始化管道和一个方法来使用这个管道将提交的文本拆分成句子,然后对每个句子的情感进行分类。下面是NlpPipeline类代码:packagecom.zh.ch.corenlp;importedu.stanford.nlp.ling.CoreAnnotations;importedu.stanford.nlp.neural.rnn.RNNCoreAnnotations;importedu.stanford.nlp.pipeline.Annotation;importedu.stanford.nlp.pipeline.StanfordCoreNLP;importedu.stanford.nlp.sentiment.SentimentCoreAnnotations;importedu.stanford.nlp.trees.Tree;importedu.stanford.nlp.util.CoreMap;importjava.util.Properties;publicclassNlpPipeline{StanfordCoreNLPpipeline=null;publicvoidinit(){Propertiesprops=newProperties();props.setProperty("annotators","tokenize,ssplit,parse,sentiment");pipeline=newStanfordCoreNLP(props);}publicvoidestimatingSentiment(Stringtext){intsentimentInt;StringsentimentName;Annotationannotation=pipeline.process(文本);for(CoreMapsentence:annotation.get(CoreAnnotations.SentencesAnnotation.class)){Treetree=sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);sentimentInt=RNNCoreAnnotations.getPredictedClass(tree);sentimentName=sentence.get(SentimentCoreAnnotations.SentimentClass.class);System.out.println(sentimentName+"\t"+sentimentInt+"\t"+sentence);}}}init()方法初始化StanfordCoreNLP流水线,也初始化了使用情感工具RequiredTokenizers、DependentParsers和SentenceSplitters要初始化管道,请将带有相应注释器列表的Properties对象传递给StanfordCoreNLP()构造函数。这将创建一个自定义管道,准备好对文本执行情感分析。在NlpPipeline类的estimatingSentiment()方法中,调用之前创建的pipeline对象的process()方法,传入文本进行处理。process()方法返回一个注释对象,该对象存储对提交文本的分析。接下来,迭代注释对象,在每次迭代中获得一个句子级别的CoreMap对象。对于这些对象中的每一个,获取一个包含用于确定基础句子情感的情感注释的Tree对象。将Tree对象传递给RNNCoreAnnotations类的getPredictedClass()方法,提取句子对应的预测情绪的数字编码。然后,获取预测情绪的名称并打印结果。要测试上述功能,请使用调用init()方法的main()方法实现一个类,然后调用nlpPipeline类的estimatingSentiment()方法,并向其传递示例文本。在以下实现中,为简单起见,直接指定文本文本。例句旨在涵盖StanfordCoreNLP可用的整个情感分数范围:非常积极、积极、中性、消极和非常消极。packagecom.zh.ch.corenlp;importjava.io.FileReader;importjava.io.IOException;publicclassMain{staticNlpPipelinenlpPipeline=null;publicstaticvoidprocessText(Stringtext){nlpPipeline.estimatingSentiment(text);}publicstaticvoidmain(String[]args){Stringtext=“这是一本优秀的书。我喜欢阅读它。我可以在星期天阅读。今天只能在星期二。不能等待下一个星期天。工作周是难以忍受的长。太棒了。”;nlpPipeline=newNlpPipeline();nlpPipeline.init();processText(text);正如您从中的示例中了解到的,StanfordCoreNLP可以返回句子的情感。然而,有许多用例需要分析多段文本的情感,每段文本可能包含多个句子。例如,您可能想要分析来自电子商务网站的推文或客户评论的情绪。要使用StanfordCoreNLP计算多句文本样本的情绪,您可以使用多种不同的技术。在处理推文时,你可能会分析推文中每个句子的情绪,如果有一些正面或负面的句子,你可以单独对整个推文进行排名,忽略情绪中性的句子。如果推文中的所有(或几乎所有)句子都是中性的,则可以将推文归类为中性。但是,有时您甚至不必分析每个句子来估计整个文本的情绪。例如,在分析客户评论时,您可以依赖他们的标题,这些标题通常由一句话组成。要完成以下示例,您需要一组客户评论。您可以使用本文随附的NlpBookReviews.csv文件中的评论。该文件包含在AmazonReviewExport的帮助下从亚马逊网页下载的实际评论集,AmazonReviewExport是一种GoogleChrome浏览器扩展程序,可让您将产品评论及其标题和评级下载到逗号分隔值(CSV)文件中。(您可以使用此工具探索不同的注释集以供分析。)将以下代码添加到NlpPipelinepublicStringfindSentiment(Stringtext){intsentimentInt=2;StringsentimentName="NULL";if(text!=null&&text.length()>0){Annotationannotation=pipeline.process(text);CoreMapsentence=annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0);Treetree=sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);sentimentInt=RNNCoreAnnotations.getPredictedClass(tree);sentimentName=sentence.get(SentimentCoreAnnotations.SentimentClass.class);}returnssentimentName;}你可能会注意到上面的代码与上一节定义的estimatingSentiment()方法中的代码类似。唯一值得注意的区别是这次您没有迭代输入文本中的句子。相反,您只会看到第一句话,因为在大多数情况下,评论的标题只包含一个句子。以下代码将从CSV文件中读取评论并将它们传递给新创建的findSentiment()以进行处理,如下所示:){String[]row;while((row=reader.readNext())!=null){System.out.println("评论:"+row[1]+"\t"+"亚马逊评分:"+row[4]+"\t"+"Sentiment:"+nlpPipeline.findSentiment(row[1]));}}catch(IOException|CsvValidationExceptione){e.printStackTrace();}}执行结果:完成代码:NlpPipeline。javapackagecom.zh.ch.corenlp;importedu.stanford.nlp.ling.CoreAnnotations;importedu.stanford.nlp.neural.rnn.RNNCoreAnnotations;importedu.stanford.nlp.pipeline.Annotation;importedu.stanford.nlp。流水线(){Propertiesprops=newProperties();props.setProperty("annotators","tokenize,ssplit,parse,sentiment");pipeline=newStanfordCoreNLP(props);}publicvoidestimatingSentiment(Stringtext){intsentimentInt;StringsentimentName;Annotationannotation=pipeline.process(文本);for(CoreMapsentence:annotation.get(CoreAnnotations.SentencesAnnotation.class)){Treetree=sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);sentimentInt=RNNCoreAnnotations.getPredictedClass(tree);sentimentName=sentence.get(SentimentCoreAnnotations.SentimentClass.class);System.out.println(sentimentName+"\t"+sentimentInt+"\t"+sentence);}}publicStringfindSentiment(Stringtext){intsentimentInt=2;StringsentimentName="NULL";if(text!=null&&text.length()>0){Annotationannotation=pipeline.process(text);CoreMapsentence=annotation.get(CoreAnnotations.SentencesAnnotation.class).get(0);Treetree=sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);sentimentInt=RNNCoreAnnotations.getPredictedClass(tree);sentimentName=sentence.get(SentimentCoreAnnotations.SentimentClass.class);}returnsentimentName;}}Main.javapackagecom.zh.ch.corenlp;importcom.opencsv.CSVReader;importcom.opencsv.CSVReaderBuilder;importcom.opencsv.exceptions.CsvValidationException;importjava.io.FileReader;importjava.io.IOException;publicclassMain{staticNlpPipelinenlpPipeline=null;publicstaticvoidprocessCsvComment(StringcsvCommentFilePath){try(CSVReaderreader=newCSVReaderLipPath).vFileReader(cs).1).build()){String[]row;while((row=reader.readNext())!=null){System.out.println("评论:"+row[1]+"\t"+"Amazonrating:"+row[4]+"\t"+"Sentiment:"+nlpPipeline.findSentiment(row[1]));}}catch(IOException|CsvValidationExceptione){e.printStackTrace();}}publicstaticvoidprocessText(Stringtext){nlpPipeline.estimatingSentiment(text);}publicstaticvoidmain(String[]args){Stringtext="Thisisanexcellentbook.Ienjoyreadingit.IcanreadonSundays.TodayisonlyTuesday.Can'twaitfornextSunday.Theworkingweekisunbearablylong.It'ssawful.";nlpPipeline=newNlpPipeline();nlpPipeline.init();//processText(文本);processCsvComment("src/main/resources/NlpBookReviews.csv");}}