I am writing a project that works with NLP (natural language parser). I am using the stanford parser.
I create a thread pool that takes sentences and run the parser with them. When I create one thread its all works fine, but when I create more, I get errors. The "test" procedure is finding words that have some connections. If I do an synchronized its supposed to work like one thread but still I get errors. My problem is that I have errors on this code:
public synchronized String test(String s,LexicalizedParser lp )
{
if (s.isEmpty()) return "";
if (s.length()>80) return "";
System.out.println(s);
String[] sent = s.split(" ");
Tree parse = (Tree) lp.apply(Arrays.asList(sent));
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();
List list = new ArrayList(tdl);
//for (int i=0;i<list.size();i++)
//System.out.println(list.get(1).toString());
//remove scops and numbers like sbj(screen-4,good-6)->screen good
Pattern p = Pattern.compile(".*\\((.*?)\\-\\d+,(.*?)\\-\\d+\\).*");
if (list.size()>2){
// Split input with the pattern
Matcher m = p.matcher(list.get(1).toString());开发者_高级运维
//check if the result have more than 1 groups
if (m.find()&& m.groupCount()>1){
if (m.groupCount()>1)
{
System.out.println(list);
return m.group(1)+m.group(2);
}}
}
return "";
}
the errors that I have are:
at blogsOpinions.ParserText.(ParserText.java:47) at blogsOpinions.ThreadPoolTest$1.run(ThreadPoolTest.java:50) at blogsOpinions.ThreadPool$PooledThread.run(ThreadPoolTest.java:196) Recovering using fall through strategy: will construct an (X ...) tree. Exception in thread "PooledThread-21" java.lang.ClassCastException: java.lang.String cannot be cast to edu.stanford.nlp.ling.HasWord
at edu.stanford.nlp.parser.lexparser.LexicalizedParser.apply(LexicalizedParser.java:289) at blogsOpinions.ParserText.test(ParserText.java:174) at blogsOpinions.ParserText.insertDb(ParserText.java:76) at blogsOpinions.ParserText.(ParserText.java:47) at blogsOpinions.ThreadPoolTest$1.run(ThreadPoolTest.java:50) at blogsOpinions.ThreadPool$PooledThread.run(ThreadPoolTest.java:196)
and how can i get the discription of the subject like the screen is very good, and I want to get screen good from the list the I get and not like list.get(1)
.
You can't call LexicalizedParser.parse
on a List
of String
s; it expects a list of HasWord
objects. It's much easier to call the apply
method on your input string. This will also run a proper tokenizer on your input (instead of your simple split
on spaces).
To get relations such as subjectness out of the returned Tree
, call its dependencies
member.
Hm, I witnessed the same stack trace. Turned out I was loading two instances of the LexicalizedParser in the same JVM. This seemed to be the problem. When I made sure only one instance is created, I was able to call lp.apply(Arrays.asList(sent))
just fine.
精彩评论