<commit_before>/**
 * Copyright 2011 <NAME>
 * 
 * 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.
 */
package junto

import org.scalatest.FunSpec
import io.Source

import java.io._
import junto.app._
import junto.config._

class PrepAttachSpec extends FunSpec {

  describe("Prepositional Phrase Attachment") {
    it ("should construct the graph, propagate labels, and evaluate") {

      val ppadir = "/data/ppa"

      // Convert files to PrepInfo lists
      val trainInfo = getInfo(ppadir+"/training")
      val devInfo = getInfo(ppadir+"/devset")
      val testInfo = getInfo(ppadir+"/test")

      // Create the edges and seeds
      val edges = createEdges(trainInfo) ::: createEdges(devInfo) ::: createEdges(testInfo)
      val seeds = createLabels(trainInfo)
      val gold = createLabels(devInfo)

      // Create the graph and run label propagation
      val graph = GraphBuilder(edges, seeds, gold)
      JuntoRunner(graph)
    }

  }

  def idNode (s: String)   = s+"_ID"
  def verbNode (s: String) = s+"_VERB"
  def nounNode (s: String) = s+"_NOUN"
  def prepNode (s: String) = s+"_PREP"
  def pobjNode (s: String) = s+"_POBJ"

  def createEdges (info: List[PrepInfo]): List[Edge] = {
    (for (item <- info) yield {
      List(Edge(idNode(item.id), verbNode(item.verb)),
           Edge(idNode(item.id), nounNode(item.noun)),
           Edge(idNode(item.id), prepNode(item.prep)),
           Edge(idNode(item.id), pobjNode(item.pobj)))
    }).flatten
  }

  def createLabels (info: List[PrepInfo]): List[Label] =
    info.map(item => Label(idNode(item.id), verbNode(item.label)))


  def getInfo(inputFile: String) = io.Source
    .fromInputStream(this.getClass.getResourceAsStream(inputFile))
    .getLines
    .toList
    .map(PrepInfoFromLine)
  
}

case class PrepInfo (
  id: String, verb: String, noun: String, prep: String, pobj: String, label: String)

object PrepInfoFromLine extends (String => PrepInfo) {
  def apply (line: String) = {
    val Array(id, verb, noun, prep, pobj, label) = line.split(" ")
    PrepInfo(id, verb, noun, prep, pobj, label)
  }
}
<commit_msg>Moved node creation methods into lazy vals in PrepInfo object.<commit_after>/**
 * Copyright 2011 <NAME>
 * 
 * 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.
 */
package junto

import org.scalatest.FunSpec
import io.Source

import java.io._
import junto.app._
import junto.config._

class PrepAttachSpec extends FunSpec {

  describe("Prepositional Phrase Attachment") {
    it ("should construct the graph, propagate labels, and evaluate") {

      // Convert files to PrepInfo lists
      val ppadir = "/data/ppa"
      val trainInfo = getInfo(ppadir+"/training")
      val devInfo = getInfo(ppadir+"/devset")
      val testInfo = getInfo(ppadir+"/test")

      // Create the edges and seeds
      val edges = createEdges(trainInfo) ++ createEdges(devInfo) ++ createEdges(testInfo)
      val seeds = createLabels(trainInfo)
      val gold = createLabels(devInfo)

      // Create the graph and run label propagation
      val graph = GraphBuilder(edges, seeds, gold)
      JuntoRunner(graph)
    }

  }

  def createEdges (info: Seq[PrepInfo]): Seq[Edge] = {
    (for (item <- info) yield
      Seq(Edge(item.idNode, item.verbNode),
          Edge(item.idNode, item.nounNode),
          Edge(item.idNode, item.prepNode),
          Edge(item.idNode, item.pobjNode))
     ).flatten
  }

  def createLabels (info: Seq[PrepInfo]): Seq[Label] =
    info.map(item => Label(item.idNode, item.label))

  def getInfo(inputFile: String) = io.Source
    .fromInputStream(this.getClass.getResourceAsStream(inputFile))
    .getLines
    .toList
    .map(PrepInfoFromLine)
  
}

case class PrepInfo (
  id: String, verb: String, noun: String, prep: String, pobj: String, label: String) {

  // Helpers for creating nodes of different types.
  def node(feature: String, nodeType: String) = s"$nodeType::$feature"
  lazy val idNode = node(id,"ID")
  lazy val verbNode = node(verb,"VERB")
  lazy val nounNode = node(noun,"NOUN")
  lazy val prepNode = node(prep,"PREP")
  lazy val pobjNode = node(pobj,"POBJ")

}

object PrepInfoFromLine extends (String => PrepInfo) {
  def apply (line: String) = {
    val Array(id, verb, noun, prep, pobj, label) = line.split(" ")
    PrepInfo(id, verb, noun, prep, pobj, label)
  }
}

{'fraction_non_alphanumeric': 0.11510926902788245, 'fraction_numerical': 0.003014318010550113, 'mean_word_length': 3.4538590604026846, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5379972', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1638, 'n_words': 523}