Skip to content

Instantly share code, notes, and snippets.

@fozziethebeat
Created August 1, 2012 07:35
Show Gist options
  • Select an option

  • Save fozziethebeat/3224640 to your computer and use it in GitHub Desktop.

Select an option

Save fozziethebeat/3224640 to your computer and use it in GitHub Desktop.

Revisions

  1. fozziethebeat created this gist Aug 1, 2012.
    29 changes: 29 additions & 0 deletions NamedEntityRecognitionExample
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import edu.stanford.nlp.ie.crf.CRFClassifier
    import edu.stanford.nlp.ling.CoreLabel
    import edu.stanford.nlp.ling.Word
    import edu.stanford.nlp.util.StringUtils
    import edu.stanford.nlp.sequences.PlainTextDocumentReaderAndWriter
    import edu.stanford.nlp.sequences.PlainTextDocumentReaderAndWriter.OutputStyle

    import scala.collection.JavaConversions.collectionAsScalaIterable
    import scala.collection.JavaConversions.seqAsJavaList
    import scala.io.Source


    object NamedEntityRecognitionExample {
    def main(args: Array[String]) {
    val props = StringUtils.argsToProperties(args)
    val crf = new CRFClassifier[CoreLabel](props)
    val loadPath = crf.flags.loadClassifier;
    val textFile = crf.flags.textFile;
    crf.loadClassifierNoExceptions(loadPath, props)

    val readerAndWriter = new PlainTextDocumentReaderAndWriter[CoreLabel]()
    readerAndWriter.init(crf.flags)
    for (line<- Source.fromFile(textFile).getLines) {
    println(crf.classify(line)
    .map(tagged => readerAndWriter.getAnswers(tagged, OutputStyle.INLINE_XML, true))
    .mkString(" "))
    }
    }
    }