Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- File "/home/yudai/Desktop/keras_test.py", line 24
- loaded_model = model_from json(loaded_model_json)
- ^
- SyntaxError: invalid syntax
- from keras.models import Sequential
- from keras.layers import Dense
- from keras.models import model_from_json
- import json
- from collections import OrderedDict
- import MeCab
- import codecs
- from slackbot.bot import default_reply
- from slackbot.bot import Bot
- import numpy
- import os
- import io, sys
- sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
- tagger = MeCab.Tagger('mecabrc')
- #モデルの構造を読む
- id2word = json.load(open('keras_AE.json', 'r'))
- id2word = {int(key): value for key, value in id2word.items()}
- word2id
- id2word.close()
- #モデルをロードする
- loaded_model = model_from json(id2word)
- #重みを適用する
- loded_model.load_weights('AE.h5')
- model.train = False
- @default_reply
- def replay_message(message):
- parsed_sentence = []
- try:
- for chunk in tagger.parse(message.body["text"].encode("utf-8")).splitlines()[:-1]:
- (surface, feature) = chunk.decode("utf-8").split('t')
- parsed_sentence.append(surface)
- parsed_sentence = ["<start>"] + parsed_sentence + ["<eos>"]
- ids = []
- for word in parsed_sentence:
- if word in word2id:
- id = word2id[word]
- ids.append(id)
- else:
- ids.append(0)
- ids_question = ids
- sentence = "".join(model.generate_sentence(ids_question, dictionary=id2word)).encode("utf-8")
- sentence = sentence.replace("◯", "")
- message.reply(sentence)
- except Exception as e:
- print (e)
- message.reply("解析できなかったのでもう一度おねがいします。")
- def main():
- bot = Bot()
- bot.run()
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment