You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@opennlp.apache.org by Carlos Scheidecker <na...@gmail.com> on 2014/02/05 21:28:52 UTC

Fwd: How to convert the tree string representation into a tree object

Hello all,

If you have something like this:

Statement : On Tuesday, John Smith bought a Honda Accord

(TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
bought) (NP (DT a) (NNP Honda) (NNP Accord))))))

I have generated that from the Parse class Show() method as bellow:

 /**
   * Displays this parse using Penn Treebank-style formatting.
   */
  public void show() {
    StringBuffer sb = new StringBuffer(text.length()*4);
    show(sb);
    System.out.println(sb);
  }


My question is: How can I translate (TOP (SBAR (IN On) (S (NP (NNP
Tuesday,) (NNP John) (NNP Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda)
(NNP Accord)))))) into a tree object?

I think I would parse that with ( -> new node, (-> new child, ) -> end node.

I was thinking on doing something like this, but instead of returning a
string buffer I would return a tree object:

/**
   * Appends the specified string buffer with a string representation of
this parse.
   *
   * @param sb A string buffer into which the parse string can be appended.
   */
  public void show(StringBuffer sb) {
    int start;
    start = span.getStart();
    if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
      sb.append("(");
      sb.append(type).append(" ");
      //System.out.print(label+" ");
      //System.out.print(head+" ");
      //System.out.print(df.format(prob)+" ");
    }
    for (Iterator<Parse> i = parts.iterator(); i.hasNext();) {
      Parse c = i.next();
      Span s = c.span;
      if (start < s.getStart()) {
        //System.out.println("pre "+start+" "+s.getStart());
        sb.append(encodeToken(text.substring(start, s.getStart())));
      }
      c.show(sb);
      start = s.getEnd();
    }
    if (start < span.getEnd()) {
      sb.append(encodeToken(text.substring(start, span.getEnd())));
    }
    if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
      sb.append(")");
    }
  }

But is there some example that someone might have already done and you
could refer to me?

Thanks,

Carlos.

Re: Fwd: How to convert the tree string representation into a tree object

Posted by Carlos Scheidecker <na...@gmail.com>.
So,

Stuff I would like to accomplish is once I have the tree object:

- GetUncles
- GetParents
- GetChildren


On Fri, Feb 7, 2014 at 2:36 PM, Carlos Scheidecker <na...@gmail.com>wrote:

> Tim & Boris,
>
> Thanks for the reply. Couldn't find the reply. Thought there was something
> wrong.
>
> What I want to do is to get a Sentence String and convert it to a Tree
> object.
>
> So, if you have this:
>
> Statement : On Tuesday, John Smith bought a Honda Accord
>
> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
>
> Then, I would like that the following resulting string of the tree
> representation "(TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP
> Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda) (NNP Accord))))))" and
> convert it to a Tree object that I can traverse.
>
> cheers,
>
> Carlos.
>
>
>
>
>
>
> On Wed, Feb 5, 2014 at 1:37 PM, Tim Miller <
> timothy.miller@childrens.harvard.edu> wrote:
>
>> Carlos,
>> I am not sure I totally understand your question, but I think you're
>> asking how to get a tree structure from a Parse object? In which case the
>> answer is that the Parse object is a tree structure (with a bunch of other
>> stuff). You can use getChildCount(), getChildren(), getType(), etc. to
>> navigate the tree. If you want to see an example look at this class from
>> cTAKES:
>>
>> https://svn.apache.org/repos/asf/ctakes/trunk/ctakes-
>> constituency-parser/src/main/java/org/apache/ctakes/
>> constituency/parser/util/TreeUtils.java
>>
>> where in the recursivelyCreateStructure() method I translate from the
>> Parse data structure into a tree in the ctakes typesystem.
>>
>> Tim
>>
>>
>> On 02/05/2014 03:28 PM, Carlos Scheidecker wrote:
>>
>>> Hello all,
>>>
>>> If you have something like this:
>>>
>>> Statement : On Tuesday, John Smith bought a Honda Accord
>>>
>>> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
>>> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
>>>
>>> I have generated that from the Parse class Show() method as bellow:
>>>
>>>   /**
>>>     * Displays this parse using Penn Treebank-style formatting.
>>>     */
>>>    public void show() {
>>>      StringBuffer sb = new StringBuffer(text.length()*4);
>>>      show(sb);
>>>      System.out.println(sb);
>>>    }
>>>
>>>
>>> My question is: How can I translate (TOP (SBAR (IN On) (S (NP (NNP
>>> Tuesday,) (NNP John) (NNP Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda)
>>> (NNP Accord)))))) into a tree object?
>>>
>>> I think I would parse that with ( -> new node, (-> new child, ) -> end
>>> node.
>>>
>>> I was thinking on doing something like this, but instead of returning a
>>> string buffer I would return a tree object:
>>>
>>> /**
>>>     * Appends the specified string buffer with a string representation of
>>> this parse.
>>>     *
>>>     * @param sb A string buffer into which the parse string can be
>>> appended.
>>>     */
>>>    public void show(StringBuffer sb) {
>>>      int start;
>>>      start = span.getStart();
>>>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>>        sb.append("(");
>>>        sb.append(type).append(" ");
>>>        //System.out.print(label+" ");
>>>        //System.out.print(head+" ");
>>>        //System.out.print(df.format(prob)+" ");
>>>      }
>>>      for (Iterator<Parse> i = parts.iterator(); i.hasNext();) {
>>>        Parse c = i.next();
>>>        Span s = c.span;
>>>        if (start < s.getStart()) {
>>>          //System.out.println("pre "+start+" "+s.getStart());
>>>          sb.append(encodeToken(text.substring(start, s.getStart())));
>>>        }
>>>        c.show(sb);
>>>        start = s.getEnd();
>>>      }
>>>      if (start < span.getEnd()) {
>>>        sb.append(encodeToken(text.substring(start, span.getEnd())));
>>>      }
>>>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>>        sb.append(")");
>>>      }
>>>    }
>>>
>>> But is there some example that someone might have already done and you
>>> could refer to me?
>>>
>>> Thanks,
>>>
>>> Carlos.
>>>
>>>
>>
>

Re: Fwd: How to convert the tree string representation into a tree object

Posted by Jörn Kottmann <ko...@gmail.com>.
Hello,

you can parse a parse tree string, like the one below, with
Parse.parseString which returns you a Parse object tree.

Is that what you are looking for?

HTH,
Jörn

On 02/07/2014 10:36 PM, Carlos Scheidecker wrote:
> Tim & Boris,
>
> Thanks for the reply. Couldn't find the reply. Thought there was something
> wrong.
>
> What I want to do is to get a Sentence String and convert it to a Tree
> object.
>
> So, if you have this:
>
> Statement : On Tuesday, John Smith bought a Honda Accord
>
> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
>
> Then, I would like that the following resulting string of the tree
> representation "(TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP
> Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda) (NNP Accord))))))" and
> convert it to a Tree object that I can traverse.
>
> cheers,
>
> Carlos.
>
>
>
>
>
>
> On Wed, Feb 5, 2014 at 1:37 PM, Tim Miller <
> timothy.miller@childrens.harvard.edu> wrote:
>
>> Carlos,
>> I am not sure I totally understand your question, but I think you're
>> asking how to get a tree structure from a Parse object? In which case the
>> answer is that the Parse object is a tree structure (with a bunch of other
>> stuff). You can use getChildCount(), getChildren(), getType(), etc. to
>> navigate the tree. If you want to see an example look at this class from
>> cTAKES:
>>
>> https://svn.apache.org/repos/asf/ctakes/trunk/ctakes-
>> constituency-parser/src/main/java/org/apache/ctakes/
>> constituency/parser/util/TreeUtils.java
>>
>> where in the recursivelyCreateStructure() method I translate from the
>> Parse data structure into a tree in the ctakes typesystem.
>>
>> Tim
>>
>>
>> On 02/05/2014 03:28 PM, Carlos Scheidecker wrote:
>>
>>> Hello all,
>>>
>>> If you have something like this:
>>>
>>> Statement : On Tuesday, John Smith bought a Honda Accord
>>>
>>> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
>>> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
>>>
>>> I have generated that from the Parse class Show() method as bellow:
>>>
>>>    /**
>>>      * Displays this parse using Penn Treebank-style formatting.
>>>      */
>>>     public void show() {
>>>       StringBuffer sb = new StringBuffer(text.length()*4);
>>>       show(sb);
>>>       System.out.println(sb);
>>>     }
>>>
>>>
>>> My question is: How can I translate (TOP (SBAR (IN On) (S (NP (NNP
>>> Tuesday,) (NNP John) (NNP Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda)
>>> (NNP Accord)))))) into a tree object?
>>>
>>> I think I would parse that with ( -> new node, (-> new child, ) -> end
>>> node.
>>>
>>> I was thinking on doing something like this, but instead of returning a
>>> string buffer I would return a tree object:
>>>
>>> /**
>>>      * Appends the specified string buffer with a string representation of
>>> this parse.
>>>      *
>>>      * @param sb A string buffer into which the parse string can be
>>> appended.
>>>      */
>>>     public void show(StringBuffer sb) {
>>>       int start;
>>>       start = span.getStart();
>>>       if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>>         sb.append("(");
>>>         sb.append(type).append(" ");
>>>         //System.out.print(label+" ");
>>>         //System.out.print(head+" ");
>>>         //System.out.print(df.format(prob)+" ");
>>>       }
>>>       for (Iterator<Parse> i = parts.iterator(); i.hasNext();) {
>>>         Parse c = i.next();
>>>         Span s = c.span;
>>>         if (start < s.getStart()) {
>>>           //System.out.println("pre "+start+" "+s.getStart());
>>>           sb.append(encodeToken(text.substring(start, s.getStart())));
>>>         }
>>>         c.show(sb);
>>>         start = s.getEnd();
>>>       }
>>>       if (start < span.getEnd()) {
>>>         sb.append(encodeToken(text.substring(start, span.getEnd())));
>>>       }
>>>       if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>>         sb.append(")");
>>>       }
>>>     }
>>>
>>> But is there some example that someone might have already done and you
>>> could refer to me?
>>>
>>> Thanks,
>>>
>>> Carlos.
>>>
>>>


Re: Fwd: How to convert the tree string representation into a tree object

Posted by Carlos Scheidecker <na...@gmail.com>.
So,

Stuff I would like to accomplish is once I have the tree object:

- GetUncles
- GetParents
- GetChildren


On Fri, Feb 7, 2014 at 2:36 PM, Carlos Scheidecker <na...@gmail.com>wrote:

> Tim & Boris,
>
> Thanks for the reply. Couldn't find the reply. Thought there was something
> wrong.
>
> What I want to do is to get a Sentence String and convert it to a Tree
> object.
>
> So, if you have this:
>
> Statement : On Tuesday, John Smith bought a Honda Accord
>
> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
>
> Then, I would like that the following resulting string of the tree
> representation "(TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP
> Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda) (NNP Accord))))))" and
> convert it to a Tree object that I can traverse.
>
> cheers,
>
> Carlos.
>
>
>
>
>
>
> On Wed, Feb 5, 2014 at 1:37 PM, Tim Miller <
> timothy.miller@childrens.harvard.edu> wrote:
>
>> Carlos,
>> I am not sure I totally understand your question, but I think you're
>> asking how to get a tree structure from a Parse object? In which case the
>> answer is that the Parse object is a tree structure (with a bunch of other
>> stuff). You can use getChildCount(), getChildren(), getType(), etc. to
>> navigate the tree. If you want to see an example look at this class from
>> cTAKES:
>>
>> https://svn.apache.org/repos/asf/ctakes/trunk/ctakes-
>> constituency-parser/src/main/java/org/apache/ctakes/
>> constituency/parser/util/TreeUtils.java
>>
>> where in the recursivelyCreateStructure() method I translate from the
>> Parse data structure into a tree in the ctakes typesystem.
>>
>> Tim
>>
>>
>> On 02/05/2014 03:28 PM, Carlos Scheidecker wrote:
>>
>>> Hello all,
>>>
>>> If you have something like this:
>>>
>>> Statement : On Tuesday, John Smith bought a Honda Accord
>>>
>>> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
>>> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
>>>
>>> I have generated that from the Parse class Show() method as bellow:
>>>
>>>   /**
>>>     * Displays this parse using Penn Treebank-style formatting.
>>>     */
>>>    public void show() {
>>>      StringBuffer sb = new StringBuffer(text.length()*4);
>>>      show(sb);
>>>      System.out.println(sb);
>>>    }
>>>
>>>
>>> My question is: How can I translate (TOP (SBAR (IN On) (S (NP (NNP
>>> Tuesday,) (NNP John) (NNP Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda)
>>> (NNP Accord)))))) into a tree object?
>>>
>>> I think I would parse that with ( -> new node, (-> new child, ) -> end
>>> node.
>>>
>>> I was thinking on doing something like this, but instead of returning a
>>> string buffer I would return a tree object:
>>>
>>> /**
>>>     * Appends the specified string buffer with a string representation of
>>> this parse.
>>>     *
>>>     * @param sb A string buffer into which the parse string can be
>>> appended.
>>>     */
>>>    public void show(StringBuffer sb) {
>>>      int start;
>>>      start = span.getStart();
>>>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>>        sb.append("(");
>>>        sb.append(type).append(" ");
>>>        //System.out.print(label+" ");
>>>        //System.out.print(head+" ");
>>>        //System.out.print(df.format(prob)+" ");
>>>      }
>>>      for (Iterator<Parse> i = parts.iterator(); i.hasNext();) {
>>>        Parse c = i.next();
>>>        Span s = c.span;
>>>        if (start < s.getStart()) {
>>>          //System.out.println("pre "+start+" "+s.getStart());
>>>          sb.append(encodeToken(text.substring(start, s.getStart())));
>>>        }
>>>        c.show(sb);
>>>        start = s.getEnd();
>>>      }
>>>      if (start < span.getEnd()) {
>>>        sb.append(encodeToken(text.substring(start, span.getEnd())));
>>>      }
>>>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>>        sb.append(")");
>>>      }
>>>    }
>>>
>>> But is there some example that someone might have already done and you
>>> could refer to me?
>>>
>>> Thanks,
>>>
>>> Carlos.
>>>
>>>
>>
>

Re: Fwd: How to convert the tree string representation into a tree object

Posted by Carlos Scheidecker <na...@gmail.com>.
Tim & Boris,

Thanks for the reply. Couldn't find the reply. Thought there was something
wrong.

What I want to do is to get a Sentence String and convert it to a Tree
object.

So, if you have this:

Statement : On Tuesday, John Smith bought a Honda Accord

(TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
bought) (NP (DT a) (NNP Honda) (NNP Accord))))))

Then, I would like that the following resulting string of the tree
representation "(TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP
Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda) (NNP Accord))))))" and
convert it to a Tree object that I can traverse.

cheers,

Carlos.






On Wed, Feb 5, 2014 at 1:37 PM, Tim Miller <
timothy.miller@childrens.harvard.edu> wrote:

> Carlos,
> I am not sure I totally understand your question, but I think you're
> asking how to get a tree structure from a Parse object? In which case the
> answer is that the Parse object is a tree structure (with a bunch of other
> stuff). You can use getChildCount(), getChildren(), getType(), etc. to
> navigate the tree. If you want to see an example look at this class from
> cTAKES:
>
> https://svn.apache.org/repos/asf/ctakes/trunk/ctakes-
> constituency-parser/src/main/java/org/apache/ctakes/
> constituency/parser/util/TreeUtils.java
>
> where in the recursivelyCreateStructure() method I translate from the
> Parse data structure into a tree in the ctakes typesystem.
>
> Tim
>
>
> On 02/05/2014 03:28 PM, Carlos Scheidecker wrote:
>
>> Hello all,
>>
>> If you have something like this:
>>
>> Statement : On Tuesday, John Smith bought a Honda Accord
>>
>> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
>> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
>>
>> I have generated that from the Parse class Show() method as bellow:
>>
>>   /**
>>     * Displays this parse using Penn Treebank-style formatting.
>>     */
>>    public void show() {
>>      StringBuffer sb = new StringBuffer(text.length()*4);
>>      show(sb);
>>      System.out.println(sb);
>>    }
>>
>>
>> My question is: How can I translate (TOP (SBAR (IN On) (S (NP (NNP
>> Tuesday,) (NNP John) (NNP Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda)
>> (NNP Accord)))))) into a tree object?
>>
>> I think I would parse that with ( -> new node, (-> new child, ) -> end
>> node.
>>
>> I was thinking on doing something like this, but instead of returning a
>> string buffer I would return a tree object:
>>
>> /**
>>     * Appends the specified string buffer with a string representation of
>> this parse.
>>     *
>>     * @param sb A string buffer into which the parse string can be
>> appended.
>>     */
>>    public void show(StringBuffer sb) {
>>      int start;
>>      start = span.getStart();
>>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>        sb.append("(");
>>        sb.append(type).append(" ");
>>        //System.out.print(label+" ");
>>        //System.out.print(head+" ");
>>        //System.out.print(df.format(prob)+" ");
>>      }
>>      for (Iterator<Parse> i = parts.iterator(); i.hasNext();) {
>>        Parse c = i.next();
>>        Span s = c.span;
>>        if (start < s.getStart()) {
>>          //System.out.println("pre "+start+" "+s.getStart());
>>          sb.append(encodeToken(text.substring(start, s.getStart())));
>>        }
>>        c.show(sb);
>>        start = s.getEnd();
>>      }
>>      if (start < span.getEnd()) {
>>        sb.append(encodeToken(text.substring(start, span.getEnd())));
>>      }
>>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>        sb.append(")");
>>      }
>>    }
>>
>> But is there some example that someone might have already done and you
>> could refer to me?
>>
>> Thanks,
>>
>> Carlos.
>>
>>
>

Re: Fwd: How to convert the tree string representation into a tree object

Posted by Carlos Scheidecker <na...@gmail.com>.
Tim & Boris,

Thanks for the reply. Couldn't find the reply. Thought there was something
wrong.

What I want to do is to get a Sentence String and convert it to a Tree
object.

So, if you have this:

Statement : On Tuesday, John Smith bought a Honda Accord

(TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
bought) (NP (DT a) (NNP Honda) (NNP Accord))))))

Then, I would like that the following resulting string of the tree
representation "(TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP
Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda) (NNP Accord))))))" and
convert it to a Tree object that I can traverse.

cheers,

Carlos.






On Wed, Feb 5, 2014 at 1:37 PM, Tim Miller <
timothy.miller@childrens.harvard.edu> wrote:

> Carlos,
> I am not sure I totally understand your question, but I think you're
> asking how to get a tree structure from a Parse object? In which case the
> answer is that the Parse object is a tree structure (with a bunch of other
> stuff). You can use getChildCount(), getChildren(), getType(), etc. to
> navigate the tree. If you want to see an example look at this class from
> cTAKES:
>
> https://svn.apache.org/repos/asf/ctakes/trunk/ctakes-
> constituency-parser/src/main/java/org/apache/ctakes/
> constituency/parser/util/TreeUtils.java
>
> where in the recursivelyCreateStructure() method I translate from the
> Parse data structure into a tree in the ctakes typesystem.
>
> Tim
>
>
> On 02/05/2014 03:28 PM, Carlos Scheidecker wrote:
>
>> Hello all,
>>
>> If you have something like this:
>>
>> Statement : On Tuesday, John Smith bought a Honda Accord
>>
>> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
>> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
>>
>> I have generated that from the Parse class Show() method as bellow:
>>
>>   /**
>>     * Displays this parse using Penn Treebank-style formatting.
>>     */
>>    public void show() {
>>      StringBuffer sb = new StringBuffer(text.length()*4);
>>      show(sb);
>>      System.out.println(sb);
>>    }
>>
>>
>> My question is: How can I translate (TOP (SBAR (IN On) (S (NP (NNP
>> Tuesday,) (NNP John) (NNP Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda)
>> (NNP Accord)))))) into a tree object?
>>
>> I think I would parse that with ( -> new node, (-> new child, ) -> end
>> node.
>>
>> I was thinking on doing something like this, but instead of returning a
>> string buffer I would return a tree object:
>>
>> /**
>>     * Appends the specified string buffer with a string representation of
>> this parse.
>>     *
>>     * @param sb A string buffer into which the parse string can be
>> appended.
>>     */
>>    public void show(StringBuffer sb) {
>>      int start;
>>      start = span.getStart();
>>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>        sb.append("(");
>>        sb.append(type).append(" ");
>>        //System.out.print(label+" ");
>>        //System.out.print(head+" ");
>>        //System.out.print(df.format(prob)+" ");
>>      }
>>      for (Iterator<Parse> i = parts.iterator(); i.hasNext();) {
>>        Parse c = i.next();
>>        Span s = c.span;
>>        if (start < s.getStart()) {
>>          //System.out.println("pre "+start+" "+s.getStart());
>>          sb.append(encodeToken(text.substring(start, s.getStart())));
>>        }
>>        c.show(sb);
>>        start = s.getEnd();
>>      }
>>      if (start < span.getEnd()) {
>>        sb.append(encodeToken(text.substring(start, span.getEnd())));
>>      }
>>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>>        sb.append(")");
>>      }
>>    }
>>
>> But is there some example that someone might have already done and you
>> could refer to me?
>>
>> Thanks,
>>
>> Carlos.
>>
>>
>

Re: Fwd: How to convert the tree string representation into a tree object

Posted by Tim Miller <ti...@childrens.harvard.edu>.
Carlos,
I am not sure I totally understand your question, but I think you're 
asking how to get a tree structure from a Parse object? In which case 
the answer is that the Parse object is a tree structure (with a bunch of 
other stuff). You can use getChildCount(), getChildren(), getType(), 
etc. to navigate the tree. If you want to see an example look at this 
class from cTAKES:

https://svn.apache.org/repos/asf/ctakes/trunk/ctakes-constituency-parser/src/main/java/org/apache/ctakes/constituency/parser/util/TreeUtils.java

where in the recursivelyCreateStructure() method I translate from the 
Parse data structure into a tree in the ctakes typesystem.

Tim

On 02/05/2014 03:28 PM, Carlos Scheidecker wrote:
> Hello all,
>
> If you have something like this:
>
> Statement : On Tuesday, John Smith bought a Honda Accord
>
> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
>
> I have generated that from the Parse class Show() method as bellow:
>
>   /**
>     * Displays this parse using Penn Treebank-style formatting.
>     */
>    public void show() {
>      StringBuffer sb = new StringBuffer(text.length()*4);
>      show(sb);
>      System.out.println(sb);
>    }
>
>
> My question is: How can I translate (TOP (SBAR (IN On) (S (NP (NNP
> Tuesday,) (NNP John) (NNP Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda)
> (NNP Accord)))))) into a tree object?
>
> I think I would parse that with ( -> new node, (-> new child, ) -> end node.
>
> I was thinking on doing something like this, but instead of returning a
> string buffer I would return a tree object:
>
> /**
>     * Appends the specified string buffer with a string representation of
> this parse.
>     *
>     * @param sb A string buffer into which the parse string can be appended.
>     */
>    public void show(StringBuffer sb) {
>      int start;
>      start = span.getStart();
>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>        sb.append("(");
>        sb.append(type).append(" ");
>        //System.out.print(label+" ");
>        //System.out.print(head+" ");
>        //System.out.print(df.format(prob)+" ");
>      }
>      for (Iterator<Parse> i = parts.iterator(); i.hasNext();) {
>        Parse c = i.next();
>        Span s = c.span;
>        if (start < s.getStart()) {
>          //System.out.println("pre "+start+" "+s.getStart());
>          sb.append(encodeToken(text.substring(start, s.getStart())));
>        }
>        c.show(sb);
>        start = s.getEnd();
>      }
>      if (start < span.getEnd()) {
>        sb.append(encodeToken(text.substring(start, span.getEnd())));
>      }
>      if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>        sb.append(")");
>      }
>    }
>
> But is there some example that someone might have already done and you
> could refer to me?
>
> Thanks,
>
> Carlos.
>


RE: How to convert the tree string representation into a tree object

Posted by Boris Galitsky <bg...@hotmail.com>.
Hi Carlos
  Please take a look at the attached class which is a wrapper for OpenNLP for various applications.  I think a tree object you are looking for is 'Parse' object in OpenNLP.  I hope you will find a number of useful examples there.
RegardsBoris
---------------------------------------------------------------------
public synchronized Parse parseSentenceNlp(String sentence,      boolean normalizeText) {    // don't try to parse very short sentence, not much info in it anyway,    // most likely a heading    if (sentence == null || sentence.trim().length() < MIN_SENTENCE_LENGTH)      return null;
    Parse[] parseArray = null;    try {      parseArray = ParserTool.parseLine(sentence, parser, 1);    } catch (Throwable t) {      LOG.log(Level.WARNING, "failed to parse the sentence : '" + sentence); //, t);      return null;    }    // there should be only one result parse    if (parseArray != null && parseArray.length > 0)      return parseArray[0];    else      return null;  }
> Date: Wed, 5 Feb 2014 13:28:52 -0700
> Subject: Fwd: How to convert the tree string representation into a tree object
> From: nando.nlp@gmail.com
> To: dev@opennlp.apache.org; users@opennlp.apache.org
> 
> Hello all,
> 
> If you have something like this:
> 
> Statement : On Tuesday, John Smith bought a Honda Accord
> 
> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
> 
> I have generated that from the Parse class Show() method as bellow:
> 
>  /**
>    * Displays this parse using Penn Treebank-style formatting.
>    */
>   public void show() {
>     StringBuffer sb = new StringBuffer(text.length()*4);
>     show(sb);
>     System.out.println(sb);
>   }
> 
> 
> My question is: How can I translate (TOP (SBAR (IN On) (S (NP (NNP
> Tuesday,) (NNP John) (NNP Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda)
> (NNP Accord)))))) into a tree object?
> 
> I think I would parse that with ( -> new node, (-> new child, ) -> end node.
> 
> I was thinking on doing something like this, but instead of returning a
> string buffer I would return a tree object:
> 
> /**
>    * Appends the specified string buffer with a string representation of
> this parse.
>    *
>    * @param sb A string buffer into which the parse string can be appended.
>    */
>   public void show(StringBuffer sb) {
>     int start;
>     start = span.getStart();
>     if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>       sb.append("(");
>       sb.append(type).append(" ");
>       //System.out.print(label+" ");
>       //System.out.print(head+" ");
>       //System.out.print(df.format(prob)+" ");
>     }
>     for (Iterator<Parse> i = parts.iterator(); i.hasNext();) {
>       Parse c = i.next();
>       Span s = c.span;
>       if (start < s.getStart()) {
>         //System.out.println("pre "+start+" "+s.getStart());
>         sb.append(encodeToken(text.substring(start, s.getStart())));
>       }
>       c.show(sb);
>       start = s.getEnd();
>     }
>     if (start < span.getEnd()) {
>       sb.append(encodeToken(text.substring(start, span.getEnd())));
>     }
>     if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>       sb.append(")");
>     }
>   }
> 
> But is there some example that someone might have already done and you
> could refer to me?
> 
> Thanks,
> 
> Carlos.
 		 	   		  

RE: How to convert the tree string representation into a tree object

Posted by Boris Galitsky <bg...@hotmail.com>.
Hi Carlos
  Please take a look at the attached class which is a wrapper for OpenNLP for various applications.  I think a tree object you are looking for is 'Parse' object in OpenNLP.  I hope you will find a number of useful examples there.
RegardsBoris
---------------------------------------------------------------------
public synchronized Parse parseSentenceNlp(String sentence,      boolean normalizeText) {    // don't try to parse very short sentence, not much info in it anyway,    // most likely a heading    if (sentence == null || sentence.trim().length() < MIN_SENTENCE_LENGTH)      return null;
    Parse[] parseArray = null;    try {      parseArray = ParserTool.parseLine(sentence, parser, 1);    } catch (Throwable t) {      LOG.log(Level.WARNING, "failed to parse the sentence : '" + sentence); //, t);      return null;    }    // there should be only one result parse    if (parseArray != null && parseArray.length > 0)      return parseArray[0];    else      return null;  }
> Date: Wed, 5 Feb 2014 13:28:52 -0700
> Subject: Fwd: How to convert the tree string representation into a tree object
> From: nando.nlp@gmail.com
> To: dev@opennlp.apache.org; users@opennlp.apache.org
> 
> Hello all,
> 
> If you have something like this:
> 
> Statement : On Tuesday, John Smith bought a Honda Accord
> 
> (TOP (SBAR (IN On) (S (NP (NNP Tuesday,) (NNP John) (NNP Smith)) (VP (VBD
> bought) (NP (DT a) (NNP Honda) (NNP Accord))))))
> 
> I have generated that from the Parse class Show() method as bellow:
> 
>  /**
>    * Displays this parse using Penn Treebank-style formatting.
>    */
>   public void show() {
>     StringBuffer sb = new StringBuffer(text.length()*4);
>     show(sb);
>     System.out.println(sb);
>   }
> 
> 
> My question is: How can I translate (TOP (SBAR (IN On) (S (NP (NNP
> Tuesday,) (NNP John) (NNP Smith)) (VP (VBD bought) (NP (DT a) (NNP Honda)
> (NNP Accord)))))) into a tree object?
> 
> I think I would parse that with ( -> new node, (-> new child, ) -> end node.
> 
> I was thinking on doing something like this, but instead of returning a
> string buffer I would return a tree object:
> 
> /**
>    * Appends the specified string buffer with a string representation of
> this parse.
>    *
>    * @param sb A string buffer into which the parse string can be appended.
>    */
>   public void show(StringBuffer sb) {
>     int start;
>     start = span.getStart();
>     if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>       sb.append("(");
>       sb.append(type).append(" ");
>       //System.out.print(label+" ");
>       //System.out.print(head+" ");
>       //System.out.print(df.format(prob)+" ");
>     }
>     for (Iterator<Parse> i = parts.iterator(); i.hasNext();) {
>       Parse c = i.next();
>       Span s = c.span;
>       if (start < s.getStart()) {
>         //System.out.println("pre "+start+" "+s.getStart());
>         sb.append(encodeToken(text.substring(start, s.getStart())));
>       }
>       c.show(sb);
>       start = s.getEnd();
>     }
>     if (start < span.getEnd()) {
>       sb.append(encodeToken(text.substring(start, span.getEnd())));
>     }
>     if (!type.equals(AbstractBottomUpParser.TOK_NODE)) {
>       sb.append(")");
>     }
>   }
> 
> But is there some example that someone might have already done and you
> could refer to me?
> 
> Thanks,
> 
> Carlos.