2015-10-21 28 views
0

我想通過一些樹對象工作,並需要將它的所有格(POS)節點連接到它們各自的名詞(NN)。使用樹外科醫生毗鄰POS到神經網絡

我目前希望tsurgeon工具能夠做到這一點,而且他們確實看起來是爲了完成這項任務而設計的。但是,我的錯誤很奇怪,並且沒有生產力。

我會嘗試儘可能使用應用程序的上下文和輸出結果來設置它,一個小的測試程序已經寫出來瞭解這個用例,但我恐怕即使是這樣大而複雜,請原諒我的設置。

List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class); 
//Pattern: http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/tregex/TregexPattern.html 
TregexPattern adjoinPOS = TregexPattern.compile("POS=pos , NN=noun"); 
TsurgeonPattern tsurgeon = Tsurgeon.parseOperation("adjoin [email protected] noun"); 
for(CoreMap sentence : sentences) { 
    Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); 
    tree = Tsurgeon.processPattern(adjoinPOS, tsurgeon, tree); 
    tree.pennPrint(); 
} 

這不幸的是,什麼都不做生產,而不是我得到斯坦福NLP內的空指針異常:

Exception in thread "main" java.lang.NullPointerException 
at edu.stanford.nlp.trees.tregex.tsurgeon.AdjoinNode$Matcher.evaluate(AdjoinNode.java:49) 
at edu.stanford.nlp.trees.tregex.tsurgeon.TsurgeonPatternRoot$Matcher.evaluate(TsurgeonPatternRoot.java:63) 
at edu.stanford.nlp.trees.tregex.tsurgeon.Tsurgeon.processPattern(Tsurgeon.java:579) 
at my.code.line of the processPattern call (yeah, I cleaned this up a little for brevity) 

讓我們假設句樹是:

(ROOT(SBARQ(WHNP(WP What))(SQ( VBZ是)(NP(NP)(NP(NP))(NP (NN飛行)))))(NP最大值)(NP高度) (。 ?)))

任何人都可以給我任何關於如何使用樹外科醫生來編輯這棵樹的指針嗎?

回答

0

你不想使用在這種情況下,因爲用於在PTB格式的子樹和另一個樹中的節點相結合。

我想你想要做的是這樣的事情:

Tree t = Tree.valueOf("(ROOT (SBARQ (WHNP (WP What)) (SQ (VBZ is) (NP (NP (NP (DT the) (NN cannonball) (POS 's)) (NN maximum) (NN altitude)) (PP (IN during) (NP (NN flight))))) (. ?)))"); 
//Pattern: http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/tregex/TregexPattern.html 
TregexPattern adjoinPOS = TregexPattern.compile("(POS=postag < __=pos) $- NN=noun"); 
TsurgeonPattern tsurgeon = Tsurgeon.parseOperation("[move pos >-1 noun] [delete postag]"); 
Tsurgeon.processPattern(adjoinPOS, tsurgeon, t); 

這將移動附着詞素旁邊炮彈(神經網絡屆時將有兩個孩子),並刪除POS節點。

+0

節點已經處於正確的位置,緊跟着NN,也許我沒有在問題中明確我的意圖。我需要把它放到名詞的末尾,而不是把它當作一個單獨的標記。這是爲了稍後的連鎖處理。我認爲這是相鄰的用例。這會是重命名並刪除它的情況嗎? –

+0

是的,您可以使用* relabel *做如下:「relabel noun /^(.*)$/$1={pos}/」,然後刪除POS節點。您還必須將Tregex更改爲「(POS = postag <__ = pos)$ - (NN <__ = noun)」。 –

+0

對不起,正確的relabel命令實際上是「relabel noun /^.*$/={noun}={pos}/」。 –