Skip to content

Commit

Permalink
Major rewrite of the pattern matcher and associated code
Browse files Browse the repository at this point in the history
  • Loading branch information
keiffster committed Mar 13, 2017
1 parent 566dad4 commit b37de63
Show file tree
Hide file tree
Showing 241 changed files with 2,753 additions and 2,815 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ ENV/
# Rope project settings
.ropeproject

src/.DS_Store
.DS_Store/
.idea

# Pylint output
Expand Down
Binary file modified bots/.DS_Store
Binary file not shown.
Binary file modified bots/alice2/.DS_Store
Binary file not shown.
Binary file added bots/rosie_fixed/.DS_Store
Binary file not shown.
8 changes: 2 additions & 6 deletions bots/rosie_fixed/aiml/default.aiml
Original file line number Diff line number Diff line change
Expand Up @@ -17263,8 +17263,7 @@
<li>What is it to you.</li>
<li>I am not sure if I can quantify the answer.</li>
</random>
<br/>
<srai>xfind
<srai>XFIND
<person/>
</srai>
</template>
Expand Down Expand Up @@ -19232,7 +19231,6 @@
<li>Try asking another<bot name="species"/>.
</li>
</random>
<br/>
<srai>XFIND
<person/>
</srai>
Expand Down Expand Up @@ -19704,7 +19702,6 @@
as often as possible, do you?
</li>
</random>
<br/>
<think>
<set name="does">
<person/>
Expand Down Expand Up @@ -20456,7 +20453,6 @@
<li>Doesn't everyone have a</li>
</random>
<person/>.
<br/>
I have
<random>
<li>a lot of friends on the Internet.</li>
Expand Down Expand Up @@ -25119,7 +25115,7 @@
</category>
<category>
<pattern>SPRINGTIME *</pattern>
<template>Paris in the<br/>the spring.
<template>Paris in the the spring.
</template>
</category>
<category>
Expand Down
4 changes: 2 additions & 2 deletions bots/rosie_fixed/aiml/personality.aiml
Original file line number Diff line number Diff line change
Expand Up @@ -1030,8 +1030,8 @@
</category>
<category>
<pattern>SING</pattern>
<template>Daisy, Daisy. Give me your answer do.<br/>I am half crazy. All for the love of you.<br/>It won't be a
stylish marriage. I can't afford a carriage.<br/>But you'll look sweet. Upon the seat.<br/>Of a bicycle
<template>Daisy, Daisy. Give me your answer do. I am half crazy. All for the love of you. It won't be a
stylish marriage. I can't afford a carriage. But you'll look sweet. Upon the seat. Of a bicycle
built for two.
</template>
</category>
Expand Down
134 changes: 134 additions & 0 deletions bots/rosie_fixed/aiml/xfind.aiml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<aiml>
<!-- Copyright (c) 2007 ALICE A.I. Foundation, Inc. -->
<!-- -->
<!-- Permission is hereby granted, free of charge, to any person -->
<!-- obtaining a copy of this software and associated documentation -->
<!-- files (the "Software"), to deal in the Software without -->
<!-- restriction, including without limitation the rights to use, -->
<!-- copy, modify, merge, publish, distribute, sublicense, and/or sell -->
<!-- copies of the Software, and to permit persons to whom the -->
<!-- Software is furnished to do so, subject to the following -->
<!-- conditions: -->
<!-- -->
<!-- The above copyright notice and this permission notice shall be -->
<!-- included in all copies or substantial portions of the Software. -->
<!-- -->
<!-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -->
<!-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -->
<!-- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -->
<!-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -->
<!-- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -->
<!-- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -->
<!-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -->
<!-- OTHER DEALINGS IN THE SOFTWARE. -->
<!-- -->
<!-- Complies with the AIML 1.0.1 Standard -->
<!-- as adopted by the ALICE A.I. Foundation. -->
<!-- Last modified July 26, 2007 -->

<category>
<pattern>ACCESS *</pattern>
<template>
<srai>XFIND
<star/>
</srai>
</template>
</category>
<category>
<pattern>GOOGLE *</pattern>
<template><![CDATA[<a target="_new" href="http://www.google.com/search?q=<personf/>">]]> Google Search
<![CDATA[</a>]]></template>
</category>
<category>
<pattern>XDMOZ *</pattern>
<template><![CDATA[<a target="_new" href="http://search.dmoz.org/cgi-bin/search?search=<personf/>">]]>
<random>
<li>Click.</li>
<li>Start.</li>
<li>Search.</li>
<li>Click me.</li>
<li>Click here.</li>
<li>Open Directory.</li>
<li>Don't Click Me.</li>
<li>Don't Click Here.</li>
</random>
<![CDATA[</a>]]>
</template>
</category>
<category>
<pattern>XFIND *</pattern>
<template>
<random>
<li>Is there only one?</li>
<li>Let me think about it.</li>
<li>Have you tried a web search?</li>
<li>I haven't heard of
<person/>
.
</li>
<li>There might be more than one.</li>
<li>I need time to formulate the reply.</li>
<li>I'll ask around and get back to you.</li>
<li>I have to think about that one for a while.</li>
<li>I will ask my
<bot name="botmaster"/>
for the answer.
</li>
<li>I would look into the web for that knowledge.</li>
<li>Does it have anything to do with
<get name="topic"/>
?
</li>
<li>Interesting question.</li>
<li>That's a good question.</li>
<li>I'll come back to that later.</li>
<li>Is that a rhetorical question?</li>
<li>Do you use Explorer or another browser?</li>
<li>That's not something I get asked all the time.</li>
<li>I don't know anything about
<set name="it">
<person/>
</set>
.
</li>
<li>Check back later and see if I learn the answer to that one.</li>
<li>That's an interesting question. I'll come back to that in a minute.</li>
<li>You tell me.</li>
<li>What is it to you?</li>
<li>Are you testing me?</li>
<li>I will search for it.</li>
<li>I can ask my<bot name="botmaster"/>.
</li>
<li>I will try to find out.</li>
<li>I can ask someone about it.</li>
<li>I would do a search for it.</li>
<li>Would you like to know more?</li>
<li>Have you tried searching the web?</li>
<li>Do a web search for it.</li>
<li>Try searching the web.</li>
<li>Maybe my
<bot name="botmaster"/>
knows the answer.
</li>
<li>I have never been asked that before.</li>
<li>I think you already know the answer.</li>
<li>Searching...Searching...Please stand by.</li>
</random>
<think>
<set name="it">
<srai>PUSH
<set name="topic">
<person/>
</set>
</srai>
</set>
</think>
</template>
</category>
<category>
<pattern>XGOOGLE *</pattern>
<template><![CDATA[<a target="_new" href="http://www.google.com/search?q=<personf/>">]]> Google Search
<![CDATA[</a>]]></template>
</category>
</aiml>
Binary file added src/.DS_Store
Binary file not shown.
9 changes: 0 additions & 9 deletions src/programy/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,12 @@ def ask_question(self, clientid: str, text: str, srai=False):

question = Question.create_from_text(pre_processed)

# KS
conversation.record_dialog(question)
# if srai is False:
# conversation.record_dialog(question)
# parent_question = None
# else:
# parent_question = conversation.current_question()

answers = []
for each_sentence in question.sentences:

# KS
response = self.brain.ask_question(self, clientid, each_sentence)
#response = self.brain.ask_question(self, clientid, each_sentence, parent_question)

if response is not None:
logging.debug("Raw Response (%s): %s", clientid, response)
Expand All @@ -137,7 +129,6 @@ def ask_question(self, clientid: str, text: str, srai=False):
each_sentence.response = self.default_response
answers.append(self.default_response)

# KS
if srai is True:
conversation.pop_dialog()

Expand Down
24 changes: 11 additions & 13 deletions src/programy/brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ def load_services(self, brain_configuration):
def pre_process_question(self, bot, clientid, question):
return self.preprocessors.process(bot, clientid, question)

# KS
#def ask_question(self, bot, clientid, sentence, parent_question) -> str:
def ask_question(self, bot, clientid, sentence) -> str:

conversation = bot.get_conversation(clientid)
Expand All @@ -247,13 +245,7 @@ def ask_question(self, bot, clientid, sentence) -> str:
logging.info("Topic pattern = [%s]", topic_pattern)

try:
# KS
#if parent_question is not None:
# that_question = parent_question
#else:
# that_question = conversation.nth_question(2)
that_question = conversation.nth_question(2)

that_sentence = that_question.current_sentence()

# If the last response was valid, i.e not none and not empty string, then use
Expand All @@ -269,11 +261,17 @@ def ask_question(self, bot, clientid, sentence) -> str:
logging.info("No That pattern default to [*]")
that_pattern = "*"

return self._aiml_parser.match_sentence(bot, clientid,
sentence,
None, # KS parent_question,
topic_pattern=topic_pattern,
that_pattern=that_pattern)
match_context = self._aiml_parser.match_sentence(bot, clientid,
sentence,
topic_pattern=topic_pattern,
that_pattern=that_pattern)

if match_context is not None:
template_node = match_context.template_node()
logging.debug("AIML Parser evaluating template [%s]", template_node.to_string())
return template_node.template.resolve(bot, clientid)

return None

def post_process_response(self, bot, clientid, response: str):
return self.postprocessors.process(bot, clientid, response)
Expand Down
46 changes: 16 additions & 30 deletions src/programy/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,21 @@

class Sentence(object):

def __init__(self, text: str, split_chars: str=" "):
def __init__(self, text: str=None, split_chars: str=" "):
self._words = self._split_into_words(text, split_chars)
self._stars = []
self._thatstars = []
self._topicstars = []
self._response = None
self._matched_context = None

@property
def words(self):
return self._words

@property
def stars(self):
return self._stars

@stars.setter
def stars(self, stars):
self._stars = stars

@property
def thatstars(self):
return self._thatstars
def append_word(self, word):
self._words.append(word)

@thatstars.setter
def thatstars(self, thatstars):
self._thatstars = thatstars

@property
def topicstars(self):
return self._topicstars

@topicstars.setter
def topicstars(self, topicstars):
self._topicstars = topicstars
def append_sentence(self, sentence):
for word in sentence._words:
self._words.append(word)

@property
def response(self):
Expand All @@ -61,14 +42,22 @@ def response(self):
def response(self, text: str):
self._response = text

@property
def matched_context(self):
return self._matched_context

@matched_context.setter
def matched_context(self, context):
self._matched_context = context

def num_words(self):
return len(self.words)

def word(self, num: int):
if num < self.num_words():
return self.words[num]
else:
raise Exception("Num word array violation !")
raise Exception("Num word array violation !")

def words_from_current_pos(self, current_pos: int):
if len(self._words) > 0:
Expand All @@ -78,9 +67,6 @@ def words_from_current_pos(self, current_pos: int):
def text(self):
return " ".join(self._words)

def reset_stars(self):
self._stars = []

def _split_into_words(self, sentence, split_chars: str):
if sentence is None:
return []
Expand Down
Loading

0 comments on commit b37de63

Please sign in to comment.