Skip to content
This repository has been archived by the owner on Oct 2, 2018. It is now read-only.

Commit

Permalink
Merge pull request forslund#2 from nielstron/dev
Browse files Browse the repository at this point in the history
Enable AIML Fallback
  • Loading branch information
nielstron authored Feb 19, 2018
2 parents 00c07c9 + 8269684 commit 93ab5ff
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 730 deletions.
81 changes: 65 additions & 16 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,99 @@


import aiml
from os import listdir
import os
from os import listdir, remove as remove_file
from os.path import dirname, isfile
from mycroft.skills.core import FallbackSkill

from mycroft.api import DeviceApi
from mycroft.skills.core import FallbackSkill, intent_handler
from adapt.intent import IntentBuilder
from mycroft.util.log import getLogger

__author__ = 'jarbas'
__author__ = 'jarbas, nielstron'

LOGGER = getLogger(__name__)

def strTobool(v):
""" Converts String to boolean representation
From https://stackoverflow.com/questions/715417/
converting-from-a-string-to-boolean-in-python/715468#715468
"""
return v.lower() in ("yes", "true", "t", "1")

class AimlFallback(FallbackSkill):

def __init__(self):
super(AimlFallback, self).__init__(name='AimlSkill')
super(AimlFallback, self).__init__(name='AimlFallback')
self.kernel = aiml.Kernel()
# TODO read from config maybe?
self.aiml_path = dirname(__file__) + "/aiml"
self.brain_path = dirname(__file__) + "/bot_brain.brn"
#self.load_brain()
self.aiml_path = os.path.join(dirname(__file__),"aiml")
self.brain_path = os.path.join(dirname(__file__), "brain", "bot_brain.brn")
self.load_brain()
return

def initialize(self):
self.register_fallback(self.handle_fallback, 40)
return

def load_brain(self):
aimls = listdir(self.aiml_path)
for aiml in aimls:
self.kernel.bootstrap(learnFiles=self.aiml_path + "/" + aiml)
if isfile(self.brain_path):
self.kernel.bootstrap(brainFile = self.brain_path)
else:
aimls = listdir(self.aiml_path)
for aiml in aimls:
self.kernel.learn(os.path.join(self.aiml_path, aiml))
self.kernel.saveBrain(self.brain_path)
device = DeviceApi().get()
self.kernel.setBotPredicate("name", device["name"])
self.kernel.setBotPredicate("species", device["type"])
self.kernel.setBotPredicate("genus", "Mycroft")
self.kernel.setBotPredicate("family", "virtual personal assistant")
self.kernel.setBotPredicate("order", "virtual intelligence")
self.kernel.setBotPredicate("class", "computer program")
self.kernel.setBotPredicate("kingdom", "machine")
self.kernel.setBotPredicate("hometown", "127.0.0.1")
self.kernel.setBotPredicate("botmaster", "master")
self.kernel.setBotPredicate("master", "the community")
# IDEA: extract age from https://api.github.com/repos/MycroftAI/mycroft-core created_at date
self.kernel.setBotPredicate("age", "2")
return

def initialize(self):
#self.register_fallback(self.handle_fallback, 99)
pass
@intent_handler(IntentBuilder("ResetMemoryIntent").require("Reset").require("Memory"))
def handle_reset_brain(self, message):
# delete the brain file and reset memory
self.speak_dialog("reset.memory")
self.kernel.resetBrain()
remove_file(self.brain_path)
# also reload base knowledge
self.load_brain()
return

def ask_brain(self, utterance):
response = self.kernel.respond(utterance)
# make a security copy once in a while
# TODO maybe every 10th time?
self.kernel.saveBrain(self.brain_path)
return response

def handle_fallback(self, message):
utterance = message.data.get("utterance")
answer = self.ask_brain(utterance)
if answer != "":
self.speak(answer)
asked_question = False
if answer.endswith("?"):
asked_question = True
self.speak(answer, expect_response=asked_question)
return True
return False

def shutdown(self):
self.kernel.resetBrain() # Manual remove
#self.kernel.saveBrain(self.brain_path)
#self.kernel.resetBrain() # Manual remove
self.remove_fallback(self.handle_fallback)
super(AimlFallback, self).shutdown()

def stop(self):
pass

def create_skill():
return AimlFallback()
Loading

0 comments on commit 93ab5ff

Please sign in to comment.