lib/nlp: introduce nitnlp bin as a client example for lib/nlp.
[nit.git] / lib / nlp / nitnlp.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Natural Language Processor based on the StanfordNLP core.
16 #
17 # This tool provides a document comparison service from command line based on
18 # StanfordNLP and NLPVector consine similarity.
19 #
20 # See http://nlp.stanford.edu/software/corenlp.shtml.
21 module nitnlp
22
23 import opts
24 import nlp
25
26 # Option management
27 var opt_java_cp = new OptionString("Java classpath for StanfordNLP jars", "--cp")
28 var options = new OptionContext
29 options.add_option(opt_java_cp)
30 options.parse(args)
31 var arguments = options.rest
32
33 # Processor initialization
34 var java_cp = opt_java_cp.value
35 if java_cp == null then java_cp = "*"
36 var proc = new NLPProcessor(java_cp)
37
38 if arguments.length != 2 then
39 print "Usage: nitnlp text1 text2\n"
40 options.usage
41 sys.exit 1
42 end
43
44 var doc1 = proc.process(arguments.first)
45 print doc1.vector.join(":", ",")
46 var doc2 = proc.process(arguments.last)
47 print doc2.vector.join(":", ",")
48
49 print doc1.vector.cosine_similarity(doc2.vector)