Created
August 1, 2012 07:35
-
-
Save fozziethebeat/3224640 to your computer and use it in GitHub Desktop.
A simple example using Stanford's Named Entity Recognizer as a library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" ")) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment