views:

52

answers:

2

I'm working with the Stanford Parser in ruby and want to search for all nodes of a tree with a particular label name. This is the recursive method i have coded so far

def searchTreeWithLabel(tree,lablename,listOfNodes)
  if tree.instance_of?(StanfordParser::Tree)
    if tree.lable.toString == lablename then
      listOfNodes << tree
    else
      tree.children.each { |c| searchTreeWithLabel(c, lablename, listOfNodes)}
    end
  end
  listOfNodes
end

i want the method to return a list of Tree nodes that have the label as labelname

A: 

I'm not familiar StanfordParser but I imagine you need to take the descent part of the traversal out of the inner conditional and always do it.

Also, did they really implement a toString method? Seriously? It's not .to_s? I mean, I enjoyed Java, before I found Ruby... :-)

DigitalRoss
they did not implement a toString, the method is being called on a Tree instance which is actually a java instance available in ruby via the Ruby java bridge
charudatta
Heh, I did figure there was a reasonable explanation, it was just funny to see...
DigitalRoss
A: 

my original code was correct but ruby was having some problem with the

if tree.lable.toString == lablename then

statement, turns out tree.value works as well, so now i'm checking

if tree.value == lablename then

and it works.

charudatta