All Downloads are FREE. Search and download functionalities are using the official Maven repository.

epic.trees.StandardTreeProcessor.scala Maven / Gradle / Ivy

The newest version!
package epic.trees

import breeze.util.Interner
import java.io.{ObjectInputStream, IOException}


/*
 Copyright 2012 David Hall

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/**
 *
 * @author dlwh
 */
case class StandardTreeProcessor(headFinder: HeadFinder[AnnotatedLabel] = HeadFinder.collins) extends (Tree[String]=>BinarizedTree[AnnotatedLabel]) {
  import Trees.Transforms._
  private def ens = new EmptyNodeStripper[String]
  private def xox = new XOverXRemover[String]
  private val dummyLabelStripper  = new StripLabels("EDITED")

  // Don't delete.
  @throws(classOf[IOException])
  @throws(classOf[ClassNotFoundException])
  private def readObject(oin: ObjectInputStream) {
    oin.defaultReadObject()
  }


  def apply(tree: Tree[String]):BinarizedTree[AnnotatedLabel] = {
    var transformed = xox(dummyLabelStripper(ens(tree).get))
    transformed = if(transformed.children.length != 1) {
      Tree("", IndexedSeq(transformed), transformed.span)
    } else {
      transformed
    }
    val ann = transformed.map { AnnotatedLabel.parseTreebank(_) }


    def makeIntermediate(l: AnnotatedLabel, tag: AnnotatedLabel) = {
       l.copy("@"+l.label, headTag = Some(tag.baseLabel))
    }

    def extend(a: AnnotatedLabel, sib: Either[AnnotatedLabel, AnnotatedLabel]) = sib match {
      case Left(s) => a.copy(siblings = a.siblings :+ Left(s.baseLabel))
      case Right(s) => a.copy(siblings = a.siblings :+ Right(s.baseLabel))
    }



    Trees.binarize(ann, makeIntermediate, extend, headFinder).relabelRoot(_ => AnnotatedLabel.TOP)
  }
}

object StandardTreeProcessor {


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy