PLN NLP Examples

From OpenCog

(Redirected from Walkthrough)
Jump to: navigation, search

[Status] : Under Construction (if you know a better layout, feel free to change) Err, well, development already provides a how-to overview ...

note: opencog right now is used either with scheme as a remote scheme repl at which one can modify the contents of the atomspace, or using python, or using the web interface, or (only as a desperate last resort) as c++ code.

note2: The purpose of this document is not only to learn the basics, but to also be able to type in some simple code while doing the tutorial. So, interactive learning! See however, the cookbook which already covers much of this.

Contents

Installation/Compiling Opencog

See Building OpenCog for details on install and compiling.

Setting up Programming Environment

See Development.

Scheme

Connecting to the remote opencog server and its scheme repl plugin...

Emacs

See Cookbook#Emacs_Remote_Scheme_Shell Remote Scheme Shell

Bare Basics

PLN

Probabilistic Logic Networks - a comprehensive framework for uncertain inference.

Summary

'Socrates' PLN Logic Examples

See PLN usage for a HOWTO on using PLN. Connect to the remote opencog scheme shell using Emacs. Copy and paste the below code for very basic examples of Deduction, Abduction, and Induction.

Deduction

(ConceptNode "Socrates" (stv .01 .3))
(ConceptNode "man" (stv .01 .3))
(ConceptNode "mortal" (stv .1 .8))
(InheritanceLink (ConceptNode "Socrates") (ConceptNode "man") (stv 1 0.8))
(InheritanceLink (ConceptNode "man") (ConceptNode "mortal") (stv 0.9 0.8))
 
(pln-bc (InheritanceLink (ConceptNode "Socrates") (ConceptNode "mortal")) 200)
;-->
;(InheritanceLink (stv 0.89999998 0.80000001) (ConceptNode "Socrates" (stv 0.0099999998 0.30000001)) (ConceptNode "mortal" (stv 0.1 0.80000001)))

Induction

(ConceptNode "Socrates" (stv .01 .3))
(ConceptNode "man" (stv .01 .3))
(ConceptNode "mortal" (stv .1 .8))
(InheritanceLink (ConceptNode "Socrates") (ConceptNode "man") (stv 1 0.8))
(InheritanceLink (ConceptNode "Socrates") (ConceptNode "mortal") (stv 0.9 0.8))
 
(pln-bc (InheritanceLink (ConceptNode "man") (ConceptNode "mortal")) 200)
;-->
;(InheritanceLink (stv 0.89999998 0.80000001) (ConceptNode "man" (stv 0.0099999998 0.30000001)) (ConceptNode "mortal" (stv 0.1 0.80000001)))

Abduction

(ConceptNode "Socrates" (stv .01 .3))
(ConceptNode "man" (stv .01 .3))
(ConceptNode "mortal" (stv .1 .8))
(InheritanceLink (ConceptNode "Socrates") (ConceptNode "mortal") (stv 1 0.8))
(InheritanceLink (ConceptNode "man") (ConceptNode "mortal") (stv 0.9 0.8))
(pln-bc (InheritanceLink (ConceptNode "Socrates") (ConceptNode "man")) 200)
;-->
;(InheritanceLink (stv 0.089999996 0.80000001) (ConceptNode "Socrates" (stv 0.0099999998 0.30000001))(ConceptNode "man" (stv 0.0099999998 0.30000001)))

PLN Atom/Link Types

TODO: very important to fill in, a list of possible atom links and relationships we can use! See atom types for a listing of those that are currently documented.

PLN Math

TODO: Effects of stv of concepts on outcomes, math outline of PLN, etc...

Links

Organized links to opencog wiki or similar pages that are relevant to understanding PLN.

Natural Language Processing

See natural language processing.

RelEx

RelEx is the current natural language processing code which parses English sentences and outputs opencog atoms/links.

Installing

Relex Launchpad

Follow the README. You can, i believe, use the appropriate ubuntu repositories for most dependencies.

One problem i had with installing on ubuntu is that JAVA_HOME is not set automatically. So, in ~/.bashrc, i added

JAVA_HOME="/usr/lib/jvm/default-java"

export JAVA_HOME

and then at the command prompt typed source ~/.bashrc

Also, for jwnl, i had to change build.xml in relex to point to the jar path, since copying the jar to /usr/local/share/java didn't seem to work.

Summary

First, make sure that link-parser (one of the dependencies) works by typing in link-parse at the command prompt and typing in a random sentence.

Now, go to the relex directory and type ./opencog-server.sh or similar. To connect, type telnet localhost 4444

Lets now look at some of the output that one gets after typing a simple sentence into the telnet prompt.

"Hello, my name is John."

Summary: First, it outputs the grammatical parsing of the sentence. It uses link-parser for this. See Constituent tree for abbreviations.

Second, it reconstructs the grammatical structure of the sentence using opencog links. Noticeably, each word is given a unique id and then associated with the 'human readable' WordNode. Also the words with unique ids are associated a 'sentence parse' ConceptNode.

Finally it also encodes the 'linguistic meaning' of some words, like "john" with "person", etc, using _________ algorithm.

; (S (NP Hello , my name) (VP is (NP John)) .)

(ReferenceLink (stv 1.0 1.0)
  (WordInstanceNode "Hello@e2e5e176-bd7f-4fc4-9e24-3adae507326d")
  (WordNode "Hello")
)
(WordInstanceLink (stv 1.0 1.0)
  (WordInstanceNode "Hello@e2e5e176-bd7f-4fc4-9e24-3adae507326d")
  (ParseNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50_parse_0")
)
(ReferenceLink (stv 1.0 1.0)
  (WordInstanceNode ",@eb7f099c-f32a-481a-9151-a32a9f2956cd")
  (WordNode ",")
)
(WordInstanceLink (stv 1.0 1.0)
  (WordInstanceNode ",@eb7f099c-f32a-481a-9151-a32a9f2956cd")
  (ParseNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50_parse_0")
)
(ReferenceLink (stv 1.0 1.0)
  (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
  (WordNode "my")
)
(WordInstanceLink (stv 1.0 1.0)
  (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
  (ParseNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50_parse_0")
)
(ReferenceLink (stv 1.0 1.0)
  (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
  (WordNode "name")
)
(WordInstanceLink (stv 1.0 1.0)
  (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
  (ParseNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50_parse_0")
)
(ReferenceLink (stv 1.0 1.0)
  (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
  (WordNode "is")
)
(WordInstanceLink (stv 1.0 1.0)
  (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
  (ParseNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50_parse_0")
)
(ReferenceLink (stv 1.0 1.0)
  (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  (WordNode "John")
)
(WordInstanceLink (stv 1.0 1.0)
  (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  (ParseNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50_parse_0")
)
(ReferenceLink (stv 1.0 1.0)
  (WordInstanceNode ".@3c9c956b-0d01-4a9c-b3a4-7187ec7158c6")
  (WordNode ".")
)
(WordInstanceLink (stv 1.0 1.0)
  (WordInstanceNode ".@3c9c956b-0d01-4a9c-b3a4-7187ec7158c6")
  (ParseNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50_parse_0")
)
(ReferenceLink (stv 1.0 1.0)
  (ParseNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50_parse_0")
  (ListLink
     (WordInstanceNode "Hello@e2e5e176-bd7f-4fc4-9e24-3adae507326d")
     (WordInstanceNode ",@eb7f099c-f32a-481a-9151-a32a9f2956cd")
     (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
     (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
     (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
     (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
     (WordInstanceNode ".@3c9c956b-0d01-4a9c-b3a4-7187ec7158c6")
  )
)
(ParseLink (stv 1 1)
  (ParseNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50_parse_0" (stv 1.0 0.8763))
  (SentenceNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50")
)
(LemmaLink (stv 1.0 1.0)
  (WordInstanceNode "Hello@e2e5e176-bd7f-4fc4-9e24-3adae507326d")
  (WordNode "Hello")
)
(LemmaLink (stv 1.0 1.0)
  (WordInstanceNode ",@eb7f099c-f32a-481a-9151-a32a9f2956cd")
  (WordNode ",")
)
(LemmaLink (stv 1.0 1.0)
  (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
  (WordNode "me")
)
(LemmaLink (stv 1.0 1.0)
  (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
  (WordNode "name")
)
(LemmaLink (stv 1.0 1.0)
  (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
  (WordNode "be")
)
(LemmaLink (stv 1.0 1.0)
  (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  (WordNode "John")
)
(LemmaLink (stv 1.0 1.0)
  (WordInstanceNode ".@3c9c956b-0d01-4a9c-b3a4-7187ec7158c6")
  (WordNode ".")
)
; _obj (<<be>>, <<John>>) 
(EvaluationLink (stv 1.0 1.0)
  (DefinedLinguisticRelationshipNode "_obj")
  (ListLink
     (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
     (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  )
)
; _advmod (<<be>>, <<Hello>>) 
(EvaluationLink (stv 1.0 1.0)
  (DefinedLinguisticRelationshipNode "_advmod")
  (ListLink
     (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
     (WordInstanceNode "Hello@e2e5e176-bd7f-4fc4-9e24-3adae507326d")
  )
)
; _subj (<<be>>, <<name>>) 
(EvaluationLink (stv 1.0 1.0)
  (DefinedLinguisticRelationshipNode "_subj")
  (ListLink
     (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
     (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
  )
)
; tense (be, present)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
  (DefinedLinguisticConceptNode "present")
)
; inflection-TAG (be, .v)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
  (DefinedLinguisticConceptNode ".v")
)
; pos (be, verb)
(PartOfSpeechLink (stv 1.0 1.0)
  (WordInstanceNode "is@88521e05-9853-41f1-8e48-0a4ef638dfbf")
  (DefinedLinguisticConceptNode "verb")
)
; pos (., punctuation)
(PartOfSpeechLink (stv 1.0 1.0)
  (WordInstanceNode ".@3c9c956b-0d01-4a9c-b3a4-7187ec7158c6")
  (DefinedLinguisticConceptNode "punctuation")
)
; gender (John, masculine)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  (DefinedLinguisticConceptNode "masculine")
)
; definite-FLAG (John, T)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  (DefinedLinguisticConceptNode "definite")
)
; inflection-TAG (John, .m)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  (DefinedLinguisticConceptNode ".m")
)
; person-FLAG (John, T)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  (DefinedLinguisticConceptNode "person")
)
; pos (John, noun)
(PartOfSpeechLink (stv 1.0 1.0)
  (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  (DefinedLinguisticConceptNode "noun")
)
; noun_number (John, singular)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "John@c9ba05a2-0429-4040-8764-c86850d784cc")
  (DefinedLinguisticConceptNode "singular")
)
; pos (,, punctuation)
(PartOfSpeechLink (stv 1.0 1.0)
  (WordInstanceNode ",@eb7f099c-f32a-481a-9151-a32a9f2956cd")
  (DefinedLinguisticConceptNode "punctuation")
)
; pronoun-FLAG (me, T)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
  (DefinedLinguisticConceptNode "pronoun")
)
; gender (me, person)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
  (DefinedLinguisticConceptNode "person")
)
; definite-FLAG (me, T)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
  (DefinedLinguisticConceptNode "definite")
)
; pos (me, adj)
(PartOfSpeechLink (stv 1.0 1.0)
  (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
  (DefinedLinguisticConceptNode "adj")
)
; noun_number (me, singular)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
  (DefinedLinguisticConceptNode "singular")
)
; definite-FLAG (Hello, T)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "Hello@e2e5e176-bd7f-4fc4-9e24-3adae507326d")
  (DefinedLinguisticConceptNode "definite")
)
; pos (Hello, adv)
(PartOfSpeechLink (stv 1.0 1.0)
  (WordInstanceNode "Hello@e2e5e176-bd7f-4fc4-9e24-3adae507326d")
  (DefinedLinguisticConceptNode "adv")
)
; noun_number (Hello, singular)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "Hello@e2e5e176-bd7f-4fc4-9e24-3adae507326d")
  (DefinedLinguisticConceptNode "singular")
)
; _poss (<<name>>, <<me>>) 
(EvaluationLink (stv 1.0 1.0)
  (DefinedLinguisticRelationshipNode "_poss")
  (ListLink
     (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
     (WordInstanceNode "my@8619717a-8ca1-4201-b3b1-8a429c0f6d4d")
  )
)
; definite-FLAG (name, T)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
  (DefinedLinguisticConceptNode "definite")
)
; inflection-TAG (name, .n)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
  (DefinedLinguisticConceptNode ".n")
)
; pos (name, noun)
(PartOfSpeechLink (stv 1.0 1.0)
  (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
  (DefinedLinguisticConceptNode "noun")
)
; noun_number (name, singular)
(InheritanceLink (stv 1.0 1.0)
  (WordInstanceNode "name@004fdd04-a342-471c-989f-eb041f879ce8")
  (DefinedLinguisticConceptNode "singular")
)
 
(ListLink (stv 1 1)
  (AnchorNode "# New Parsed Sentence")
  (SentenceNode "sentence@b2f180e1-e98e-4897-8d5a-4d71f6df5a50")
)
; END OF SENTENCE

New Atom Types?

Currently-documented atom types.

TODO: how the WordInstanceNode and similar are defined in c++/scheme code.

Currently, new atom types cannot be added at runtime; these can only be added by mangling C++. Adding new atom types is discouraged. (Why?)

Using from Scheme

TODO: connecting using telnet, using scheme (eval) to read scheme code and evaluate...  ?? why? the scheme and cookbook pages already explains how!

See also

Organized links to opencog wiki or similar pages that are relevant to understanding RelEx.

In the opencog source code, opencog/nlp is where most/all natural language processing code is related. There are READMEs there which are useful and descriptive.