Search code examples
javanlpstanford-nlp

How to name a node when insert it into the tree in Tsurgeon


When I use Tsurgeon in the Stanford Parser API, I wonder how to name a node when inserting it into the syntactic tree.

for example, I have two operations:

Tsurgeon.parseOperation("replace predphrase (MAINVP=newpred > PLACEHOLDER)");//Step 1
Tsurgeon.parseOperation("insert predphrase >-1 > newpred")//Step 2

After these operations, the tree (A (B=predphrase ...)) becomes (A (MAINVP (B=predphrase ...))).

However, I found that it fails to name the MAINVP node in the step 1, and the operation in step 2 is unable to find the node named newpred and throws an exception.


Solution

  • You need to give multiple Tsurgeon patterns in a single command so that the second pattern has access to the named nodes created in the first. Use square brackets to do so:

    TsurgeonPattern t = Tsurgeon.parseOperation(
        "[replace predphrase (MAINVP=newpred > PLACEHOLDER)]" +
        "[insert predphrase >-1 > newpred]");
    

    A simpler way to make the modification I think you want would be like so:

    [adjoinF (MAINVP > foot@) predphrase]
    

    That will place the matched node predphrase inside a new MAINVP tree.