<commit_before>package edu.cmu.lti.nlp.amr

import scala.util.matching.Regex
import scala.collection.mutable.Map
import scala.collection.mutable.Set
import scala.collection.mutable.ArrayBuffer
import scala.util.parsing.combinator._

object EvalSpans {

    val usage = """Usage: scala -classpath . edu.cmu.lti.nlp.amr.EvalSpans < amr_file"""
    type OptionMap = Map[Symbol, Any]

    def parseOptions(map : OptionMap, list: List[String]) : OptionMap = {
        def isSwitch(s : String) = (s(0) == '-')
        list match {
            case Nil => map
            //case "--train" :: tail =>
            //          parseOptions(map ++ Map('train -> true), tail)
            //case "-a" :: value :: tail =>
            //          parseOptions(map ++ Map('amrfile -> value), tail)
            //case "--only" :: tail =>
            //          parseOptions(map ++ Map('only -> true), tail)
            case "-h" :: value :: tail =>
                      parseOptions(map ++ Map('help -> value.toInt), tail)
            case "-v" :: value :: tail =>
                      parseOptions(map ++ Map('verbosity -> value.toInt), tail)
             //case string :: opt2 :: tail if isSwitch(opt2) => 
            //          parseOptions(map ++ Map('infile -> string), list.tail)
            //case string :: Nil =>  parseOptions(map ++ Map('infile -> string), list.tail)
            case option :: tail => println("Error: Unknown option "+option) 
                               sys.exit(1) 
      }
    }

    def main(args: Array[String]) {
        val options = parseOptions(Map(),args.toList)
        if (options.contains('help)) { println(usage); sys.exit(1) }

        verbosity = 0
        if (options.contains('verbosity)) {
            verbosity = options('verbosity).asInstanceOf[Int]
        }

        var correct = 0.0
        var aligner_total = 0.0
        var gold_total = 0.0
        var n = 0

        val Alignments = """.*::alignments ([^:]*) .*""".r
        val SpanRegex = """([*]?)([0-9]+)-([0-9]+)\|(.*)""".r   // TODO: move to Span
        def normalizeSpanStr(spanstr: String) : String = {
            val SpanRegex = """([^|]*)\|(.*)""".r
            val SpanRegex(wordspan, nodestr) = spanstr
            wordspan+"|"+nodestr.split("[+]").sorted.mkString("+")
        }

        for ((block, i) <- Corpus.splitOnNewline(Source.stdin.getLines).zipWithIndex) {
            val lines = block.split("\n")
            val alignerStrs = lines.filter(x => x.matches(".*Aligner .*"))
            val annotatorStrs = lines.filter(x => x.matches(".*AlignerTool.*")).filterNot(x => x.matches(".*Aligner .*"))

            if (alignerStrs.size != 0 && annotatorStrs.size != 0) {
                assert(lines.filter(x => x.matches("^# ::tok .*")).size == 1, "Invalid # of tokenized")
                val tokenized = lines.filter(x => x.matches("^# ::tok .*"))(0).drop(8).split(" ")

                logger(2,"Index: "+i.toString)
                n += 1
                val Alignments(alignerStr) = alignerStrs(alignerStrs.size-1)
                val Alignments(annotatorStr) = annotatorStrs(annotatorStrs.size-1)
                val aligner = alignerStr.split(" ").filterNot(_.matches("")).filterNot(_.matches("[*].*")).map(x => normalizeSpanStr(x)).distinct
                val annotator = annotatorStr.split(" ").filterNot(_.matches("")).filterNot(_.matches("[*].*")).map(x => normalizeSpanStr(x)).distinct
                logger(2,"aligner = "+aligner.toList.toString)
                logger(2,"annotator = "+annotator.toList.toString)
                val extra = aligner.diff(annotator)
                val missing = annotator.diff(aligner)
                for (i <- missing) {
                    val SpanRegex(corefStr, start, end, nodeStr) = i
                    val blah : String = tokenized.slice(start.toInt, end.toInt).mkString(" ")
                    //logger(1,"MISSINGt: "+tokenized.slice(start.toInt, end.toInt).toList.map(x => {val y: String = x; y }).toString)
                    logger(1,"MISSING: "+tokenized.slice(start.toInt, end.toInt).map(_.toString).mkString(" ").toString+" = "+i)<commit_msg>Update EvalSpans<commit_after>package edu.cmu.lti.nlp.amr

import scala.util.matching.Regex
import scala.collection.mutable.Map
import scala.collection.mutable.Set
import scala.collection.mutable.ArrayBuffer
import scala.util.parsing.combinator._

object EvalSpans {

    val usage = """Usage: ${JAMR_HOME}/run EvalSpans < amr_file"""
    //val usage = """Usage: scala -classpath . edu.cmu.lti.nlp.amr.EvalSpans < amr_file"""
    type OptionMap = Map[Symbol, Any]

    def parseOptions(map : OptionMap, list: List[String]) : OptionMap = {
        def isSwitch(s : String) = (s(0) == '-')
        list match {
            case Nil => map
            //case "--train" :: tail =>
            //          parseOptions(map ++ Map('train -> true), tail)
            //case "-a" :: value :: tail =>
            //          parseOptions(map ++ Map('amrfile -> value), tail)
            //case "--only" :: tail =>
            //          parseOptions(map ++ Map('only -> true), tail)
            case "-h" :: value :: tail =>
                      parseOptions(map ++ Map('help -> value.toInt), tail)
            case "-v" :: value :: tail =>
                      parseOptions(map ++ Map('verbosity -> value.toInt), tail)
             //case string :: opt2 :: tail if isSwitch(opt2) => 
            //          parseOptions(map ++ Map('infile -> string), list.tail)
            //case string :: Nil =>  parseOptions(map ++ Map('infile -> string), list.tail)
            case option :: tail => println("Error: Unknown option "+option) 
                               sys.exit(1) 
      }
    }

    def main(args: Array[String]) {
        val options = parseOptions(Map(),args.toList)
        if (options.contains('help)) { println(usage); sys.exit(1) }

        verbosity = 0
        if (options.contains('verbosity)) {
            verbosity = options('verbosity).asInstanceOf[Int]
        }

        var correct = 0.0
        var aligner_total = 0.0
        var gold_total = 0.0
        var n = 0

        val Alignments = """.*::alignments ([^:]*) .*""".r
        val SpanRegex = """([*]?)([0-9]+)-([0-9]+)\|(.*)""".r   // TODO: move to Span
        def normalizeSpanStr(spanstr: String) : String = {
            val SpanRegex = """([^|]*)\|(.*)""".r
            val SpanRegex(wordspan, nodestr) = spanstr
            wordspan+"|"+nodestr.split("[+]").sorted.mkString("+")
        }

        for ((block, i) <- Corpus.splitOnNewline(Source.stdin.getLines).zipWithIndex) {
            val lines = block.split("\n")
            val alignerStrs = lines.filter(x => x.matches(".*Aligner .*"))
            val annotatorStrs = lines.filter(x => x.matches(".*AlignerTool.*")).filterNot(x => x.matches(".*Aligner .*"))

            if (alignerStrs.size != 0 && annotatorStrs.size != 0) {
                assert(lines.filter(x => x.matches("^# ::tok .*")).distinct.size == 1, "Multiple ::tok fields that are not the same, not sure which one to use.")
                val tokenized = lines.filter(x => x.matches("^# ::tok .*"))(0).drop(8).split(" ")

                logger(2,"Index: "+i.toString)
                n += 1
                val Alignments(alignerStr) = alignerStrs(alignerStrs.size-1)
                val Alignments(annotatorStr) = annotatorStrs(annotatorStrs.size-1)
                val aligner = alignerStr.split(" ").filterNot(_.matches("")).filterNot(_.matches("[*].*")).map(x => normalizeSpanStr(x)).distinct
                val annotator = annotatorStr.split(" ").filterNot(_.matches("")).filterNot(_.matches("[*].*")).map(x => normalizeSpanStr(x)).distinct
                logger(2,"aligner = "+aligner.toList.toString)
                logger(2,"annotator = "+annotator.toList.toString)
                val extra = aligner.diff(annotator)
                val missing = annotator.diff(aligner)
                for (i <- missing) {
                    val SpanRegex(corefStr, start, end, nodeStr) = i
                    val blah : String = tokenized.slice(start.toInt, end.toInt).mkString(" ")
                    //logger(1,"MISSINGt: "+tokenized.slice(start.toInt, end.toInt).toList.map(x => {val y: String = x; y }).toString)
                    logger(1,"MISSING: "+tokenized.slice(start.toInt, end.toInt).map(_.toString).mkString(" ").toString+" = "+i)
{'fraction_non_alphanumeric': 0.1734207389749702, 'fraction_numerical': 0.007151370679380214, 'mean_word_length': 2.0680073126142595, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '725393', 'n_tokens_mistral': 2646, 'n_tokens_neox': 2542, 'n_words': 511}