从已通过斯坦福NER标记器的句子中提取多个字符串

时间:2016-09-26 09:27:54

标签: java nlp stanford-nlp

我编写代码从我的字符串中提取多个模式,这些模式已经通过斯坦福NER解析器并提供如下输出:

Input Sentence - Goldman profit at risk under Volcker rule 
Output Sentence - Goldman profit at risk under <PERSON>Volcker</PERSON> rule

我需要提取单词Volker并将其放在personTag地图中,最终会在代码中打印出来。下面的代码在list.add上给出了一个空指针异常(m.group(1));

我无法弄清楚原因。请帮助解决这个问题。

    ..............

    HashMap<String, String> regs = new HashMap<String, String>();

    regs.put("PERSON", "<PERSON>(.+?)</PERSON>");
    regs.put("LOCATION", "<LOCATION>(.+?)</LOCATION>");
    regs.put("TIME", "<TIME>(.+?)</TIME>");
    regs.put("PERCENT", "<PERCENT>(.+?)</PERCENT>");
    regs.put("MONEY", "<MONEY>(.+?)</MONEY>");
    regs.put("DATE", "<DATE>(.+?)</DATE>");


for (Entry<String, String> entry : regs.entrySet()) 
{
    String key = entry.getKey();
    String value = entry.getValue();
    Matcher m = Pattern.compile(value).matcher(NER);
    ArrayList<String> list = null;

    while (m.find())
    {
        if (key.contains("PERSON")){

            list.add(m.group(1));
            personTag.put(key, list);
            //System.out.println("Person Tag:" + personTag);
            roleStrings.put(SemanticRole.PERSON, personTag.toString());
        }
        else if (key.contains("LOCATION")){
            list.add(m.group());
            locationTag.put(key, list);
            roleStrings.put(SemanticRole.LOCATION, locationTag.toString());
        }
        else if (key.contains("TIME")){
            list.add(m.group(1));
            timeTag.put(key, list);
            roleStrings.put(SemanticRole.TIME, timeTag.toString());
        }
        else if (key.contains("DATE")){
            list.add(m.group(1));
            timeTag.put(key, list);
            roleStrings.put(SemanticRole.TIME, timeTag.toString());
        }
    }
}
return roleStrings;
}

1 个答案:

答案 0 :(得分:0)

别介意。我没有初始化我的列表因此得到空指针异常。这就是我必须要做的事情:

List<String> list = new ArrayList<String>();

而不是:

 List<String> list = null;