Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assorted minor cleanup #1902

Merged
merged 14 commits into from
Nov 29, 2015
36 changes: 36 additions & 0 deletions opencog/nlp/chatbot/DIARY
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,42 @@ Relex failed to mark 4 of the 6 WH-words as interrogatives!
word-inst-get-lemma on LEFT-WALL, got nil, and then crashed.
Fix to not run these rules when there is no lemma.

====================================================

(use-modules (opencog)
(opencog nlp)
(opencog nlp microplanning))

(define lap
(SequentialAndLink
(InheritanceLink
(ConceptNode "Linas@862d2b73-dc84-4fc1-bc87-896476ba6541")
(ConceptNode "Linas"))
(EvaluationLink
(DefinedLinguisticPredicateNode "definite")
(ListLink
(ConceptNode "Linas@862d2b73-dc84-4fc1-bc87-896476ba6541")))
(ImplicationLink
(PredicateNode "ate@37537945-4593-4136-bc70-236f4c86c43c")
(PredicateNode "eat"))
(InheritanceLink
(ConceptNode "apple@5409a01f-f0d1-4665-af02-5811c9806b0e")
(ConceptNode "apple"))
(EvaluationLink
(PredicateNode "ate@37537945-4593-4136-bc70-236f4c86c43c")
(ListLink
(ConceptNode "Linas@862d2b73-dc84-4fc1-bc87-896476ba6541")
(ConceptNode "apple@5409a01f-f0d1-4665-af02-5811c9806b0e")))
(InheritanceLink
(SpecificEntityNode "Linas@862d2b73-dc84-4fc1-bc87-896476ba6541")
(DefinedLinguisticConceptNode "male"))
(InheritanceLink
(SpecificEntityNode "Linas@862d2b73-dc84-4fc1-bc87-896476ba6541")
(ConceptNode "Linas"))
))

(microplanning lap "declarative" *default_chunks_option* #f)

====================================================
bugs:

Expand Down
47 changes: 14 additions & 33 deletions opencog/nlp/chatbot/process-query.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,22 @@
; Somewhat generic, somewhat IRC-specific.
;

;------------------------------------------------------------------
(define (get-utterance-type sent)
"
get-utterance-type SENT -- Check the utterance speech act type
(use-modules (opencog nlp) (opencog nlp fuzzy))

Expect SENT to be (SentenceNode \"sentence@45c470a6-29...\")
Will return (DefinedLinguisticConceptNode ACT) where ACT is
one of DeclarativeSpeechAct, InterrogativeSpeechAct,
TruthQuerySpeechAct, etc...
;-------------------------------------------------------------------
;--------------------------------------------------------------------
(define (wh_query_process query)
"
; parse will be (ParseNode "sentence@a6_parse_0")
(define parse (car (cog-chase-link 'ParseLink 'ParseNode sent)))
; interp will be (InterpretationNode "sentence@a610_interpretation_$X")

(define interp (car
(cog-chase-link 'InterpretationLink 'InterpretationNode parse)))

; act-type will be (DefinedLinguisticConceptNode "DeclarativeSpeechAct")
(define act-type (cog-chase-link
'InheritanceLink 'DefinedLinguisticConceptNode interp))
Process wh-question using the fuzzy hyper graph Matcher
QUERY should be a SentenceNode

; Return act-type
act-type
)

;-------------------------------------------------------------------
Wrapper around get-ansers provided by (opencog nlp fuzzy)
"
(define temp (get-answers query))
(cond
((equal? '() temp) "Sorry, I don't know the answer.")
(else (car temp))
))
;--------------------------------------------------------------------
(define-public (process-query user query)
"
Expand All @@ -54,7 +44,7 @@
; Call the `get-utterance-type` function to get the speech act type
; of the utterance. The response processing will be based on the
; type of the speech act.
(let* ((gutr (get-utterance-type querySentence))
(let* ((gutr (sentence-get-utterance-type querySentence))
(utr (if (equal? '() gutr) '() (car gutr)))
)
(cond
Expand Down Expand Up @@ -100,15 +90,6 @@
(set! bc (cog-bc
(cog-new-link 'InheritanceLink (VariableNode "$x") (gdr tmp)) rule-base (SetLink)))
)
;--------------------------------------------------------------------
; Process wh-question using the fuzzy hyper graph Matcher
;--------------------------------------------------------------------
(define (wh_query_process query)
(define temp (get-answers query))
(cond
((equal? '() temp) "Sorry, I don't know the answer.")
(else (car temp))
))
;-------------------------------------------------------------------
; Used by 'truth_query_process' to find the input for the backward
; chaining.
Expand Down
90 changes: 40 additions & 50 deletions opencog/nlp/fuzzy/fuzzy.scm
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,45 @@
(opencog nlp sureal)
(opencog nlp microplanning))

(define-public (get-answers question)
; ----------------------------------------------------------
(define-public (get-answers sent-node)
"
Find answers (i.e., similar sentences that share some keyword) from
the Atomspace by using the fuzzy pattern matcher. By default, it
excludes sentences with TruthQuerySpeechAct and InterrogativeSpeechAct.

Accepts either an actual sentence or a SentenceNode as the input.
Also accepts an optional argument \"exclude-list\" which is a list of
atoms that we don't want them to exist in the hypergraphs of the
answers. By default they are
(DefinedLinguisticConceptNode \"TruthQuerySpeechAct\")
and (DefinedLinguisticConceptNode \"InterrogativeSpeechAct\").

Returns one or more sentences -- the answers.
Accepts a SentenceNode as the input.
Returns one or more sentence strings -- the answers.

For example:
(get-answers \"What did Pete eat?\")
(get-answers (car (nlp-parse \"What did Pete eat?\")))
OR:
(get-answers (SentenceNode \"sentence@123\"))

Possible result:
(Pete ate apples .)
"
; Check if the input is an actual sentence or a SentenceNode,
; parse and return the corresponding SentenceNode if it is a sentence
(define (process-q input)
(if (cog-atom? input)
(if (equal? 'SentenceNode (cog-type input))
input
#f
)
(car (nlp-parse input))
)
)

(let ((sent-node (process-q question))
(ex-list (list (DefinedLinguisticConceptNode "TruthQuerySpeechAct")
(DefinedLinguisticConceptNode "InterrogativeSpeechAct"))))
(if sent-node
(sent-matching sent-node ex-list)
(display "Please input a SentenceNode only")
)
)
(sent-matching sent-node
(list (DefinedLinguisticConceptNode "TruthQuerySpeechAct")
(DefinedLinguisticConceptNode "InterrogativeSpeechAct")))
)

; ----------------------------------------------------------
(define-public (sent-matching sent-node exclude-list)
"
The main function for finding similar sentences
Returns one or more sentences that are similar to the input one but
contains no atoms that are listed in the exclude-list
contain no atoms that are listed in the exclude-list.
"
; Generate sentences from each of the R2L-SetLinks
; (define (generate-sentences r2l-setlinks) (if (> (length r2l-setlinks) 0) (map sureal r2l-setlinks) '()))

; Generate sentences for each of the SetLinks found by the fuzzy matcher
; TODO: May need to filter out some of the contents of the SetLinks
; before sending each of them to Microplanner
(define (generate-sentences setlinks)

(define (gen-sentences setlinks)

; Find the speech act from the SetLink and use it for Microplanning
(define (get-speech-act setlink)
(let* ((speech-act-node-name
Expand All @@ -84,7 +65,14 @@
(append-map (lambda (r)
; Send each of the SetLinks found by the fuzzy matcher to
; Microplanner to see if they are good
(let ((m-results (microplanning (SequentialAndLink (cog-outgoing-set r)) (get-speech-act r) *default_chunks_option* #f)))
(let* ( (spe-act (get-speech-act r))
(seq-and (AndLink (cog-outgoing-set r)))
(m-results (microplanning seq-and spe-act
*default_chunks_option* #f)))
(trace-msg "duuude sp-act is ")
(trace-msg spe-act)
(trace-msg "duuude m-res is ")
(trace-msg m-results)
; Don't send it to SuReal in case it's not good
; (i.e. Microplanner returns #f)
(if m-results
Expand All @@ -101,23 +89,25 @@
)
)

(begin
; Delete identical sentences from the return set
(delete-duplicates
; Use Mircoplanner and SuReal to generate sentences from the SetLinks found
(generate-sentences
; Search for any similar SetLinks in the atomspace
(cog-outgoing-set (cog-fuzzy-match
; Get the R2L SetLink of the input sentence
(car (cog-chase-link 'ReferenceLink 'SetLink
(car (cog-chase-link 'InterpretationLink 'InterpretationNode
(car (cog-chase-link 'ParseLink 'ParseNode sent-node))
))
))
'SetLink
exclude-list
))
)
(let* ( ; parse is the ParseNode for the sentence
(parse (car (cog-chase-link 'ParseLink 'ParseNode sent-node)))

; intrp is the InterpretationNode for the sentence
(intrp (car (cog-chase-link 'InterpretationLink 'InterpretationNode parse)))
; setlk is the R2L SetLink of the input sentence
(setlk (car (cog-chase-link 'ReferenceLink 'SetLink intrp)))

; fzset is the set of similar sets.
(fzset (cog-fuzzy-match setlk 'SetLink exclude-list))

; reply is the generated reply sentences.
; generated by Microplanner and SuReal
(reply (gen-sentences (cog-outgoing-set fzset)))
)
(trace-msg "duuude gens is ")
(trace-msg reply)

; Delete identical sentences from the return set
(delete-duplicates reply)
)
)
Loading