0

I'm working with StanfordNLP to extract data from a parsed Tree.

I'm using Scala for coding.

val tp = TregexPattern.compile("SOME_PATTERN")
val res = tp.matcher("SOME_TREE")

to read the results of this I use

while (res.find()) {
  println(res.getMatch.getLeaves.mkString(" "))
}

I want to rewrite this while-loop in for-loop.

1 Answer 1

1

How about this:

val tp = TregexPattern.compile("SOME_PATTERN")
val res = tp.matcher("SOME_TREE")
for(it <- Iterator.continually(res.getMatch).takeWhile(_ => res.find)) {
  println(it.getLeaves.mkString(" "))
}
Sign up to request clarification or add additional context in comments.

3 Comments

Works well, but the first result of it returned is a null, how to handle this?
something like this works tags <- Iterator.continually(res.getMatch).takeWhile(_ => res.find) if tags != null but is there a better way? or the whole code in another way?!
Put a "res.find" in front of the for loop to avoid getting null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.