Exemplo n.º 1
0
class Robot:
    def __init__(self, **kwargs):
        self.knowledge = Knowledge(**kwargs)
        self.encoder = Encoder()
        self.decoder = Decoder()

    def reply(self, chat: str) -> str:
        for que_tag in self.knowledge.get_dialog_question():
            ctx = self.encoder.encode(chat, que_tag, self.knowledge)
            # print(ctx)
            if ctx is not None:
                ctx.infer(self.knowledge)
                # print(ctx)
                choice = []
                for dialog in self.knowledge.get_dialog_answer(que_tag):
                    # print(dialog)
                    dialog = self.decoder.decode(dialog, ctx, self.knowledge)
                    if dialog is not None:
                        choice.append(dialog)

                if len(choice) == 0:
                    continue

                return random.choice(choice)

        return None

    def study_entity(self, tag: str, match: str):
        self.knowledge.study_entity(tag, match)
        self.save()

    def study_connect(self, pre: dict, post: dict, double: bool):
        self.knowledge.study_connect(pre, post, double)
        self.save()

    def study_dialog(self, question: str, answer: str):
        self.knowledge.study_dialog(question, answer)
        self.save()

    def save(self):
        self.knowledge.save()

    def load(self):
        self.knowledge.load()
Exemplo n.º 2
0
import pandas as pd

from knowledge import Knowledge
from model.location import Location

df = pd.read_excel('tmp/Danh sách cấp xã.xls')
locations = []
# xã
for i, row in df.iterrows():
    if (i > 0) and (i % 1000 == 0):
        print(f"Extract {i} entities")
    location = Location(name=row["Tên"], level=row["Cấp"])
    locations.append(location)
# quận, huyện
level1_entities = df["Quận Huyện"].unique()
for name in level1_entities:
    location = Location(name=name, level="Quận Huyện")
    locations.append(location)
# tỉnh, thành phố
level0_entities = df["Tỉnh / Thành Phố"].unique()
for name in level0_entities:
    location = Location(name=name, level="Tỉnh / Thành Phố")
    locations.append(location)
savepath = "data/locations.jl"
Knowledge.save(locations, savepath)
print(f"{len(locations)} entities is saved in {savepath}")