这个问题已经在这里有了答案:

我的目标是执行情感分析,主题提取,实体...当我在Thrift服务器启动时执行以下Java代码时...

     package com.diskoverorta.tamanager;

     import com.diskoverorta.entities.EntityManager;
   //import com.diskoverorta.lifesciences.LSInterface;
     import com.diskoverorta.osdep.StanfordNLP;
     import com.diskoverorta.utils.EntityUtils;
     import com.diskoverorta.vo.*;
     import com.google.common.base.Charsets;
     import com.google.common.io.Files;
     import com.google.gson.Gson;
     import com.google.gson.GsonBuilder;
     import com.diskoverorta.pyinterface.*;
   //import com.serendio.diskoverer.lifesciences.document.LifeScienceDocument;
     import java.io.File;
     import java.util.List;
     import java.util.Set;
     import java.util.TreeSet;
     import org.kohsuke.args4j.Argument;
     import org.kohsuke.args4j.CmdLineException;
     import org.kohsuke.args4j.CmdLineParser;
     import org.kohsuke.args4j.Option;
     import java.io.IOException;  
     import java.util.ArrayList;
     import java.nio.charset.Charset;

     public class TextManager
     {
@Option(name = "-analyze", aliases = {"-analyze"}, usage = "Accepted options for analyze : Entity, Sentiment, Topic, Keyword, All")
String analysis;

@Option(name = "-Text", aliases = {"-text"}, usage = "Extracts Text analytics components for given Text")
String text;

@Option(name = "-File", aliases = {"-file"}, usage = "Extracts Text analytics components for given File")
String fileName;

@Argument
List<String> arguments = new ArrayList<String>();

static StanfordNLP nlpStanford = null;
static ThriftClient pyClient = null;

DocumentObject doc = null;

public TextManager()
{
    if(nlpStanford==null)
    {
        nlpStanford = new StanfordNLP();
    }
    if(pyClient==null)
    {
        pyClient = new ThriftClient("localhost",8002);
    }
}

public DocSentObject tagTextAnalyticsComponents(String sDoc,TAConfig config)
{
    DocSentObject doc = new DocSentObject();
    List<String> sentList = nlpStanford.splitSentencesINDocument(sDoc);

    for(int i=0; i < sentList.size(); i++)
    {
        SentenceObject temp = new SentenceObject();
        temp.sentenceText = sentList.get(i);

        if(config.analysisConfig.get("Entity") == "TRUE")
            temp.entities = (new EntityManager().getSelectedEntitiesForSentence(temp.sentenceText,config.entityConfig));

        doc.docSentences.add(temp);
    }
    return doc;
}

public DocumentObject aggregateDocumentComponentsFromSentences(DocSentObject docSent)
{
    DocumentObject docObject = new DocumentObject();
    for (SentenceObject sentObj : docSent.docSentences)
    {
        docObject.entities.currency.addAll(sentObj.entities.currency);
        docObject.entities.date.addAll(sentObj.entities.date);
        docObject.entities.location.addAll(sentObj.entities.location);
        docObject.entities.organization.addAll(sentObj.entities.organization);
        docObject.entities.percent.addAll(sentObj.entities.percent);
        docObject.entities.person.addAll(sentObj.entities.person);
        docObject.entities.time.addAll(sentObj.entities.time);
    }
    docObject.entitiesMeta = EntityUtils.extractEntityMap(docObject.entities);
    return docObject;
}

public String tagTextAnalyticsComponentsINJSON(String sDoc,TAConfig config)
{
    DocSentObject doc = tagTextAnalyticsComponents(sDoc,config);
    DocumentObject docObject = aggregateDocumentComponentsFromSentences(doc);
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String jsonOutput = gson.toJson(docObject);
    return jsonOutput;
}

public String tagUniqueTextAnalyticsComponentsINJSON(String sDoc,TAConfig config)
{
    String jsonOutput = "";
    APIOutput apiOut = new APIOutput();

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    if(config.analysisConfig.get("Entity") == "TRUE")
    {
        DocSentObject doc = tagTextAnalyticsComponents(sDoc, config);
        DocumentObject docObject = aggregateDocumentComponentsFromSentences(doc);
        EntityAPISet apiSet = EntityUtils.getEntitySet(docObject);
        apiOut.entity_general = apiSet;
    }

   // if(config.analysisConfig.get("LSEntity") == "TRUE")
   //     apiOut.entity_lifesciences = gson.fromJson(LSInterface.getLSEntitiesinJSON(sDoc),LifeScienceDocument.class);
    if(config.analysisConfig.get("Topic") == "TRUE")
    {
        Set<String> topic_set = new TreeSet<String>();
        if (apiOut.text_information == null)
            apiOut.text_information = new TextInformation();
        if(pyClient != null)
        {

            Charset.forName("UTF-8").encode(sDoc);
            List<String> topics = pyClient.getTopics(sDoc);


            for (String topic : topics)
                topic_set.add(topic);
        }
        if (topic_set.isEmpty() == true)
        {
            topic_set.add("Topic analyzer not working, Start Thrift server at port 8002");
        }
        apiOut.text_information.topics = topic_set;
    }

    if(config.analysisConfig.get("Keyword") == "TRUE")
    {
        Set<String> keyword_set = new TreeSet<String>();
        if (apiOut.text_information == null)
            apiOut.text_information = new TextInformation();
        if(pyClient != null)
        {
            List<String> keywords = pyClient.getKeywords(sDoc);
            for (String keyword : keywords)
                keyword_set.add(keyword);
        }
        if (keyword_set.isEmpty() == true)
        {
            keyword_set.add("Keyword extractor not working, Start Thrift server at port 8002");
        }
        apiOut.text_information.keywords = keyword_set;
    }

    if(config.analysisConfig.get("Sentiment") == "TRUE")
    {
        String senti_temp = null;
        Set<String> senti_set = new TreeSet<String>();

        if (apiOut.text_information == null)
            apiOut.text_information = new TextInformation();

        if(pyClient != null)
        {
            if (apiOut.text_information == null)
                apiOut.text_information = new TextInformation();

            if (config.sentimentConfig.get("textType") == "blogs_news")
                senti_temp = pyClient.getSentimentScore(sDoc, config.sentimentConfig.get("title"), config.sentimentConfig.get("middleParas"), config.sentimentConfig.get("lastPara"), 1);
            if (config.sentimentConfig.get("textType") == "reviews")
                senti_temp = pyClient.getSentimentScore(sDoc, config.sentimentConfig.get("title"), config.sentimentConfig.get("topDomain"), config.sentimentConfig.get("subDomain"));
            else
                senti_temp = pyClient.getSentimentScore(sDoc, config.sentimentConfig.get("textType"));
        }
        if (senti_temp == null)
        {
            senti_temp = "Sentiment analyzer not working, Start Thrift server at port 8002";
        }

        senti_set.add(senti_temp);
        apiOut.text_information.sentiment = senti_set;
    }
    return gson.toJson(apiOut);
}

public static void main(String args[])
{
    TAConfig config = new TAConfig();
    TextManager temp = new TextManager();
    CmdLineParser parser = new CmdLineParser(temp);
    String sample = "The last week of Parliament is unlikely to see any business being conducted with the Congress party showing no signs of relenting on its demands and continuing to seek the resignation of Union and state ministers, while hitting out at the BJP for its “politics of abuse.Congress leaders parried questions on whether the MPs would continue to show placards in the House, an act that had angered the presiding officer. But Monday morning saw several Congress MPs carry placards to the House.Here’s the latest from Parliament:3:22 pm: Lok Sabha adjourned for the day amid continued Opposition protests over Lalitgate and Vyapam issue.2:38 pm: Rajya Sabha adjourned for the day after uproar over Lalit Modi, Vyapam and some other issues.: http://indianexpress.com/article/india/politics/parliament-logjam-25-suspended-mps-return-will-congress-let-house-function/#sthash.63UHi4eu.dpuf";


    String trialtext = "";
    try {
        parser.parseArgument(args);
    }catch(CmdLineException ex)
    {
        ex.printStackTrace();
    }
    System.out.println("Command line API :");
    parser.printUsage(System.out);
    config.entityConfig.put("person", "TRUE");
    config.entityConfig.put("organization", "TRUE");
    config.entityConfig.put("location", "TRUE");
    config.entityConfig.put("date", "TRUE");
    config.entityConfig.put("time", "TRUE");
    config.entityConfig.put("currency", "TRUE");
    config.entityConfig.put("percent", "TRUE");

    if((temp.text == null) && (temp.fileName == null)) {
        trialtext = "The last week of Parliament is unlikely to see any business being conducted with the Congress party showing no signs of relenting on its demands and continuing to seek the resignation of Union and state ministers, while hitting out at the BJP for its “politics of abuse.Congress leaders parried questions on whether the MPs would continue to show placards in the House, an act that had angered the presiding officer. But Monday morning saw several Congress MPs carry placards to the House.Here’s the latest from Parliament:3:22 pm: Lok Sabha adjourned for the day amid continued Opposition protests over Lalitgate and Vyapam issue.2:38 pm: Rajya Sabha adjourned for the day after uproar over Lalit Modi, Vyapam and some other issues.: http://indianexpress.com/article/india/politics/parliament-logjam-25-suspended-mps-return-will-congress-let-house-function/#sthash.63UHi4eu.dpuf";
        System.out.println("No Text source provided considering this default text for analysis : "+trialtext);
    }
    else if((temp.text != null) && (temp.fileName != null)) {
        System.out.println("Command line text input considered for Text Analytics : " + temp.text);
        trialtext = temp.text;
    }
    else if((temp.text != null)) {
        System.out.println("Command line text input considered for Text Analytics : " + temp.text);
        trialtext = temp.text;
    }
    else if((temp.fileName != null)) {
        System.out.println("Command line text input considered for Text Analytics");
        try {
            trialtext = Files.toString(new File(temp.fileName), Charsets.UTF_8);
        }catch(IOException ex)
        {
            ex.printStackTrace();
        }
        System.out.println("File input considered for Text Analytics : " + trialtext);
    }

    if((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("Entity") == true)) {
        config.analysisConfig.put("Entity", "TRUE");
        System.out.println("Analyzing Entity in given text");
    }
    else if ((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("Sentiment") == true)) {
        config.analysisConfig.put("Sentiment", "TRUE");
        System.out.println("Analyzing Sentiment in given text");
    }
    else if ((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("Topic") == true)) {
        config.analysisConfig.put("Topic", "TRUE");
        System.out.println("Analyzing Topic in given text");
    }
    else if ((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("Keyword") == true)) {
        config.analysisConfig.put("Keyword", "TRUE");
        System.out.println("Extracting keywords in given text");
    }
    else if ((temp.analysis != null) && (temp.analysis.equalsIgnoreCase("All") == true)) {
        System.out.println("Analyzing Entity, Sentiment, Topic and Keywords");
        config.analysisConfig.put("Entity","TRUE");
        config.analysisConfig.put("Sentiment", "TRUE");
        config.analysisConfig.put("Topic", "TRUE");
        config.analysisConfig.put("Keyword","TRUE");
    }
    else {
        System.out.println("Analysis options not specified. Possible values : Entity, Sentiment, Topic, Kaeyword, All");
        System.out.println("Choosing All by default");
        config.analysisConfig.put("Entity","TRUE");
        config.analysisConfig.put("Sentiment", "TRUE");
        config.analysisConfig.put("Topic", "TRUE");
        config.analysisConfig.put("Keyword","TRUE");
    }
    System.out.println("Text Analytics output : ");

    System.out.println(temp.tagUniqueTextAnalyticsComponentsINJSON(trialtext, config));

}
}

我收到这样的错误...

org.org.apache.thrift.transport.TIOStreamTransport.read(TIOStreamTransport.java:132)org.apache.thrift.transport.TTransport.readAll(TTransport.java:86)处的org.apache.thrift.transport.TTransportException org.apache.thrift.protocol.TBinaryProtocol.readI32(TBinaryProtocol.java:318)上的apache.thrift.protocol.TBinaryProtocol.readAll(TBinaryProtocol.java:429)位于org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin(TBinary) org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:69)处com.diskoverorta.pyinterface.PyInterface $ Client.recv_getTopics(PyInterface.java:142)处com.diskoverorta.pyinterface.PyInterface $ Client处的java:219) com.diskoverorta.pyinterface.ThriftClient.getTopics(ThriftClient.java:123)处的com.diskoverorta.tamanager.TextManager.tagUniqueTextAnalyticsComponentsINJSON(TextManager.java:142)处的com.diskoverorta.tamanager处的.getTopics(PyInterface.java:129)。 TextManager.main(TextManager.java:285)线程“主”中的异常java.lang.NullPointerExceptio n在com.diskoverorta.tamanager.TextManager.main(TextManager.java:285)处的com.diskoverorta.tamanager.TextManager.tagUniqueTextAnalyticsComponentsINJSON(TextManager.java:145)

我对此完全陌生。...我无法弄清楚问题出在哪里...请帮助,谢谢!!

#1楼 票数:0

似乎config.analysisConfig为null。 因此,在调用config.analysisConfig.get时出现NullPointerException

您应该在所有使用config.analysisConfig的if中添加如下内容: config.analysisConfig != null && as:

if(config.analysisConfig != null && config.analysisConfig.get("Entity") == "TRUE") {
...
}

  ask by Karthik translate from so

未解决问题?本站智能推荐:

1回复

如何修复“ main”线程java.lang.NullPointerException中的异常? [重复]

这个问题已经在这里有了答案: 什么是NullPointerException,我该如何解决? 12个答案 我是Java编程的初学者。 我知道有关此特定消息错误的主题数量,但我无法解决此问题。 在下面的代码中,该错误指示在第23行中。 我知道问题似乎与空引用有
5回复

线程“main”java.lang.NullPointerException中的异常? [重复]

这个问题在这里已有答案: 什么是NullPointerException,我该如何解决? 12个答案 我是java编程的初学者。 我正在尝试重新创建卡片游戏大战的简化版本。 我运行了下面发布的程序,它返回了这个错误: 线程“main”java.lang.
5回复

线程“ main”中的异常java.lang.NullPointerException [重复]

这个问题已经在这里有了答案: 什么是NullPointerException,我该如何解决? 12个答案 嗨,我是Java的新手,遇到了硬件问题,我们被要求构建一个Class RandomBag-一个通用的袋子容器,该容器允许元素的“ RandomPick”。
1回复

Java中的Thrift中发生java.lang.OutOfMemoryError错误

我开始使用Thrift框架,基本示例成功,只有当我尝试使应用程序使用更多方法时,才会发生错误java.lang.OutOfMemoryError。 下面抛出异常。 我开始如下所述的客户端TTransport:
1回复

如何从Thirft IDL中的Java运行时异常生成异常对象

Thrift IDL文件中的默认异常声明生成从Java Exceptions类继承的Exception对象。 如何从Java RuntimeException类在Thrift IDL中生成异常? 谢谢。
2回复

Thrift / Java中的异常类型层次结构

有没有一种方法可以将类型层次结构信息添加到Java的节俭异常类型中。 我想定义一些常见的例外集,人们可以重用。 客户经常要处理一组异常,而不是处理单个异常。 例如,假设一个Java客户端想要捕获所有错误请求并打印异常消息。 客户端不想处理个别异常,例如TooBigParamExcep
9回复

为什么我会收到 java.lang.AbstractMethodError 错误?

ABstractMethodError 的可能原因是什么? 线程“pool-1-thread-1”中的异常java.lang.AbstractMethodError:
2回复

发生异常后,如何在Java中重新产生Thrift服务器?

我是Java的新手,并用Java编写了一个小型但非常重要的节俭服务。 我注意到它偶尔会停止投放而不会出现任何错误消息; 看来Java进程只是随机死亡,没有堆栈跟踪或异常。 确保即使发生错误后此过程仍然有效的最佳方法是什么? 这是主要功能,如果有帮助的话: