Skip to content

Commit

Permalink
feat(*.py): Add Dictionary switching function. fix #454 @4.0h
Browse files Browse the repository at this point in the history
  • Loading branch information
みぞ@CrazyBeatCoder committed Oct 27, 2018
1 parent c969c0c commit 863bdde
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
52 changes: 48 additions & 4 deletions domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import infra

ASK_CONTINUE_EVENT = u'ASK_CONTINUE_EVENT'
ASK_DIC_EVENT = u'ASK_DIC_EVENT'
ASK_WORD_EVENT = u'ASK_WORD_EVENT'
DECLARE_USER_LOSE_EVENT = u'DECLARE_USER_LOSE_EVENT'
DECLARE_GOOGLE_HOME_LOSE_EVENT = u'DECLARE_GOOGLE_HOME_LOSE_EVENT'
Expand Down Expand Up @@ -50,7 +51,7 @@ def ask_continue(obj):
else:
return {
u'followupEventInput': {
u'name': ASK_WORD_EVENT,
u'name': ASK_DIC_EVENT,
u'languageCode': queryResult[u'languageCode'],
}
}
Expand All @@ -65,11 +66,52 @@ def set_continue(obj):
userId = originalDetectIntentRequest[u'payload'][u'user'][u'userId']
user = infra.load_user(userId)
infra.reset_datastore(user)
return {
u'followupEventInput': {
u'name': ASK_DIC_EVENT,
u'languageCode': queryResult[u'languageCode'],
}
}
elif startMode == PARAM_START_MODE_CONTINUE:
logging.info(PARAM_START_MODE_CONTINUE)
return {
u'followupEventInput': {
u'name': ASK_WORD_EVENT,
u'languageCode': queryResult[u'languageCode'],
}
}
else:
raise RuntimeError(u"Unknown startMode: " + startMode)


def set_dic(obj):
queryResult = obj[u'queryResult']

try:
# float 型から int 型へ変換する
dicNum = int(queryResult[u'parameters'][u'dicNum'])
except ValueError:
dicNum = 0

logging.info(u'set_dic dicNum: ' + unicode(dicNum))

originalDetectIntentRequest = obj['originalDetectIntentRequest']
userId = originalDetectIntentRequest[u'payload'][u'user'][u'userId']
user = infra.load_user(userId)

if dicNum == 1:
# 1番、普通の辞書
infra.save_json_file_path(user, u'data/dict.json')
elif dicNum == 2:
# 2番、ポケモン辞書
infra.save_json_file_path(user, u'data/pokemon.json')
else:
return {
u'fulfillmentText': u'辞書を番号で選んでください。1番、普通の辞書。2番、ポケモン辞書。',
}

logging.info(u'set_dic user.json_file_path: ' + user.json_file_path)

return {
u'followupEventInput': {
u'name': ASK_WORD_EVENT,
Expand All @@ -95,7 +137,8 @@ def response_word_inner(obj, user):
}
else:
logging.info(queryText)
req_word_record = infra.search_reading_from_dic(queryText)
json_dic = infra.load_dic(user)
req_word_record = infra.search_reading_from_dic(queryText, json_dic)
if req_word_record:
req_word_reading = req_word_record[u'key']
logging.info(req_word_reading)
Expand All @@ -119,7 +162,7 @@ def response_word_inner(obj, user):
}
else:
resp_word_record = infra.search_word_record_from_dic(
user, req_word_reading_end)
user, req_word_reading_end, json_dic)
logging.info(resp_word_record)
word = resp_word_record[u'org'][0]
infra.save_word_datastore(user, resp_word_record)
Expand Down Expand Up @@ -148,10 +191,11 @@ def response_lose_word(obj):
userId = originalDetectIntentRequest[u'payload'][u'user'][u'userId']

user = infra.load_user(userId)
json_dic = infra.load_dic(user)
reading_end = user.last_word_end

word_record = infra.search_lose_word_record_from_dic(
user, reading_end)
user, reading_end, json_dic)
logging.info(word_record)
fulfillmentText = u''
if word_record:
Expand Down
50 changes: 35 additions & 15 deletions infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@
WORDS_COUNT_LIMIT = 10


with open('data/dict.json') as json_file:
json_dic = json.load(json_file)
json_start_word_dic = json.loads('''
[{
"key": "シリトリ",
"org": ["尻取り"],
"first": "シ",
"end": "リ"
}]''')
json_dic.extend(json_start_word_dic)
def load_dic(user):
with open(user.json_file_path) as json_file:
json_dic = json.load(json_file)
json_start_word_dic = json.loads('''
[{
"key": "シリトリ",
"org": ["尻取り"],
"first": "シ",
"end": "リ"
}]''')
json_dic.extend(json_start_word_dic)
return json_dic


class User(ndb.Model):
Expand All @@ -46,6 +48,7 @@ class User(ndb.Model):
last_word = ndb.TextProperty()
last_word_end = ndb.TextProperty()
count = ndb.IntegerProperty()
json_file_path = ndb.TextProperty()
date = ndb.DateTimeProperty(auto_now_add=True)


Expand All @@ -60,10 +63,11 @@ def contains_user(user_id):
return False


def load_user(user_id, default_last_word=u'シリトリ'):
def load_user(user_id):
try:
user = User.get_by_id(user_id)
if user:
user.json_file_path
return user
except Exception as e:
logging.exception(u'load_user: %s', e)
Expand All @@ -74,7 +78,13 @@ def load_user(user_id, default_last_word=u'シリトリ'):
user.last_word = u''
user.last_word_end = u''
user.count = 0
word_record = search_reading_from_dic(default_last_word)
user.json_file_path = u'data/dict.json'
word_record = {
u'key': u'シリトリ',
u'org': [u'尻取り'],
u'first': u'シ',
u'end': u'リ'
}
save_word_datastore(user, word_record)
return user

Expand Down Expand Up @@ -160,10 +170,16 @@ def save_word_datastore(user, save_word_record):
user.put()


def search_reading_from_dic(search_word):
def save_json_file_path(user, json_file_path):
user.json_file_path = json_file_path
user.put()


def search_reading_from_dic(search_word, json_dic):
"""json形式の辞書ファイルを全探索し、引数の文字列を含む単語レコードを返す。
:param unicode search_word: 検索する文字列
:param unicode json_dic: json形式の辞書ファイル
:rtype: unicode
:return: 検索した単語レコード(辞書にない場合は空の辞書)
"""
Expand All @@ -176,11 +192,13 @@ def search_reading_from_dic(search_word):
return {}


def search_word_record_from_dic(user, search_first):
def search_word_record_from_dic(user, search_first, json_dic):
"""json形式の辞書ファイルを全探索し、読みが search_first で始まる適当な単語レコードを返す。
無効な単語(既出の単語・存在しない単語・「ん」で終わる単語)のレコードは返さない。
:param infra.User user: ユーザ
:param unicode search_first: カタカナ 1 文字
:param unicode json_dic: json形式の辞書ファイル
:rtype: dict
:return: 検索した単語レコード(辞書にない場合は空の辞書)
"""
Expand All @@ -202,11 +220,13 @@ def search_word_record_from_dic(user, search_first):
return {}


def search_lose_word_record_from_dic(user, search_first):
def search_lose_word_record_from_dic(user, search_first, json_dic):
"""json形式の辞書ファイルを全探索し、読みが search_first で始まり
かつ、「ん」で終わる単語レコードを返す。
:param infra.User user: ユーザ
:param unicode search_first: カタカナ 1 文字
:param unicode json_dic: json形式の辞書ファイル
:rtype: dict
:return: 検索した単語レコード(辞書にない場合は空の辞書)
"""
Expand Down
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

GOOGLE_ASSISTANT_WELCOME_INTENT = u'Google Assistant Welcome Intent'
ASK_CONTINUE_INTENT = u'Ask Continue Intent'
ASK_DIC_INTENT = u'Ask Dic Intent'
ASK_WORD_INTENT = u'Ask Word Intent'
DECLARE_GOOGLE_HOME_LOSE_INTENT = u'Declare Google Home Lose Intent'

Expand All @@ -53,6 +54,8 @@ def post(self):
obj = domain.ask_continue(obj)
elif intentDisplayName == ASK_CONTINUE_INTENT:
obj = domain.set_continue(obj)
elif intentDisplayName == ASK_DIC_INTENT:
obj = domain.set_dic(obj)
elif intentDisplayName == ASK_WORD_INTENT:
obj = domain.response_word(obj)
elif intentDisplayName == DECLARE_GOOGLE_HOME_LOSE_INTENT:
Expand Down

0 comments on commit 863bdde

Please sign in to comment.