lib/nlp: provide more examples
[nit.git] / lib / nlp / README.md
index 9c718a9..3ed2c5e 100644 (file)
@@ -11,7 +11,9 @@ This wrapper needs the Stanford CoreNLP jars that run on Java 1.8+.
 
 See http://nlp.stanford.edu/software/corenlp.shtml.
 
-## Usage
+## NLPProcessor
+
+### Java client
 
 ~~~nitish
 var proc = new NLPProcessor("path/to/StanfordCoreNLP/jars")
@@ -25,52 +27,41 @@ for sentence in doc.sentences do
 end
 ~~~
 
-## Nit API
-
-For ease of use, this wrapper introduce a Nit model to handle CoreNLP XML results.
-
-### NLPDocument
-
-[[doc: NLPDocument]]
-
-[[doc: nlp::NLPDocument::from_xml]]
-[[doc: nlp::NLPDocument::from_xml_file]]
-[[doc: nlp::NLPDocument::sentences]]
-
-### NLPSentence
-
-[[doc: NLPSentence]]
+### NLPServer
 
-[[doc: nlp::NLPSentence::tokens]]
+The NLPServer provides a wrapper around the StanfordCoreNLPServer.
 
-### NLPToken
+See `https://stanfordnlp.github.io/CoreNLP/corenlp-server.html`.
 
-[[doc: NLPToken]]
-
-[[doc: nlp::NLPToken::word]]
-[[doc: nlp::NLPToken::lemma]]
-[[doc: nlp::NLPToken::pos]]
+~~~nitish
+var cp = "/path/to/StanfordCoreNLP/jars"
+var srv = new NLPServer(cp, 9000)
+srv.start
+~~~
 
-### NLP Processor
+### NLPClient
 
-[[doc: NLPProcessor]]
+The NLPClient is used as a NLPProcessor with a NLPServer backend.
 
-[[doc: nlp::NLPProcessor::java_cp]]
+~~~nitish
+var cli = new NLPClient("http://localhost:9000")
+var doc = cli.process("String to analyze")
+~~~
 
-[[doc: nlp::NLPProcessor::process]]
-[[doc: nlp::NLPProcessor::process_file]]
-[[doc: nlp::NLPProcessor::process_files]]
+## NLPIndex
 
-## NitNLP binary
+NLPIndex extends the StringIndex to use a NLPProcessor to tokenize, lemmatize and
+tag the terms of a document.
 
-The `nitnlp` binary is given as an example of NitNLP client.
-It compares two strings and display ther cosine similarity value.
+~~~nitish
+var index = new NLPIndex(proc)
 
-Usage:
+var d1 = index.index_string("Doc 1", "/uri/1", "this is a sample")
+var d2 = index.index_string("Doc 2", "/uri/2", "this and this is another example")
+assert index.documents.length == 2
 
-~~~raw
-nitnlp --cp "/path/to/jars" "sort" "Sorting array data"
-0.577
+matches = index.match_string("this sample")
+assert matches.first.document == d1
 ~~~
 
 ## TODO