Skip to content

Commit

Permalink
funktion add Category in Mycroft ai
Browse files Browse the repository at this point in the history
  • Loading branch information
gras64 committed Mar 31, 2019
1 parent ce81e3f commit e288192
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 34 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Andreas Reinle(@gras64)
Save Data To ~/.mycroft/skills/LearningSkill/public/humor/en-us/vocab/know.siri.intent
You can edit the path at home.mycroft.ai.

## Add a new category
if you want to add a new category. you just have to add a category under mycroft ai. Mycroft will then ask you for the name in your language.




Expand Down
69 changes: 37 additions & 32 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self):
# self.settings.get('public_path'),
self.public_path = self.file_system.path+"/public"
# self.settings.get('allow_category'),
self.allow_category = ["humor", "love", "science"]
self.allow_category = "humor,love,science"
LOG.debug('local path enabled: %s' % self.local_path)
# self.intent_path = "/home/pi/.mycroft/skills/LearningSkill/public/humor/vocab/de-de/hallo"
self.privacy = ""
Expand All @@ -37,26 +37,39 @@ def initialize(self):
if self.settings.get('local_path_ex') != "":
self.local_path = self.settings.get('local_path_ex')
if self.settings.get('allow_category_ex') != "":
self.allow_category = self.settings.get('allow_category_ex')

path = dirname(abspath(__file__))

path_to_humor_words = join(path, 'vocab', self.lang, 'Humor.voc')
self._humor_words = self._lines_from_path(path_to_humor_words)

path_to_science_words = join(path, 'vocab', self.lang, 'Science.voc')
self._science_words = self._lines_from_path(path_to_science_words)

path_to_love_words = join(path, 'vocab', self.lang, 'Love.voc')
self._love_words = self._lines_from_path(path_to_love_words)

path_to_cancel_words = join(path, 'vocab', self.lang, 'Cancel.voc')
self._cancel_words = self._lines_from_path(path_to_cancel_words)
self.allow_category = self.settings.get('allow_category_ex', '')

if self.enable_fallback is "True":
self.register_fallback(self.handle_fallback, 5)
LOG.debug('Learning-skil-fallback enabled: %s' % self.enable_fallback)

def init_category(self, cat):
path = dirname(abspath(__file__)) + '/vocab/'
if os.path.isfile(path + self.lang + "/" + cat +'.voc'):
path_to_words = path + self.lang + "/" + cat +'.voc'
words = self._lines_from_path(path_to_words)
else:
path = self.file_system.path + "/category/"
if os.path.isfile(path + self.lang + "/" + cat +'.voc'):
path_to_words = path + self.lang + "/" + cat +'.voc'
words = self._lines_from_path(path_to_words)
else:
self.add_category(cat, path)
path_to_words = path + self.lang +"/"+ cat+'.voc'
words = self._lines_from_path(path_to_words)
return words

def add_category(self, cat, path):
path = path + "/"+ self.lang
category = self.get_response("add.category",
data={"cat": cat})
if not os.path.isdir(path):
os.makedirs(path)
save_category = open(path +"/"+ cat+'.voc', "w")
save_category.write(cat)
save_category.close()
return True

def _lines_from_path(self, path):
with open(path, 'r') as file:
lines = [line.strip().lower() for line in file]
Expand Down Expand Up @@ -94,7 +107,7 @@ def load_fallback(self, utterance, path):
e = join(dig_path, 'dialog', self.lang, i +'.dialog')
self.log.debug('Load Falback File: ' + e)
lines = open(e).read().splitlines()
i =random.choice(lines)
i = random.choice(lines)
self.speak_dialog(i)
return True
self.log.debug('fallback learning: ignoring')
Expand All @@ -110,22 +123,14 @@ def handle_interaction(self, message):
else:
privacy = self.local_path
catego = self.get_response("begin.private")
# privacy = self.public_path
if catego in self._humor_words:
# self.speak("humor")
Category = "humor"
elif catego in self._science_words:
# self.speak("science")
Category = "science"
elif catego in self._love_words:
# self.speak("love")
Category = "love"
elif catego in self._cancel_words:
self.speak_dialog("Cancel")
return
else:
privacy = self.public_path
Category =""
for cat in self.allow_category.split(","):
if catego in self.init_category(cat):
Category = cat # set category
if not Category:
self.speak_dialog("invalid.category")
return catego
return
question = self.get_response("question")
if not question:
return # user cancelled
Expand Down
1 change: 1 addition & 0 deletions dialog/da-dk/add.category.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jeg anerkendte en ny kategori {{cat}}, hvad vil du kalde det?
1 change: 1 addition & 0 deletions dialog/de-de/add.category.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ich habe eine neu Kategorry {{cat}} erkannt, wie möchtest du sie nennen?
1 change: 1 addition & 0 deletions dialog/en-us/add.category.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I recognized a new category {{cat}}, what would you like to call it?
4 changes: 2 additions & 2 deletions settingsmeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
{
"name": "allow_category_ex",
"type": "text",
"label": "allow category current humor, love, science",
"value": "humor, love, science"
"label": "allow category current humor,love,science",
"value": "humor,love,science"
}
]
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e288192

Please sign in to comment.