File size: 4,439 Bytes
db95b72
992ab47
db95b72
 
 
992ab47
db95b72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f18fc4e
 
 
 
db95b72
 
 
 
 
 
 
 
f18fc4e
 
 
 
 
 
db95b72
 
992ab47
db95b72
 
 
 
 
 
992ab47
 
 
 
 
 
 
 
 
 
 
 
 
 
db95b72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
992ab47
db95b72
 
992ab47
f18fc4e
 
992ab47
db95b72
 
992ab47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import gradio as gr
import re
import json

# JSON ํŒŒ์ผ ์ฝ๊ธฐ
with open('perio_demo.json', 'r') as file:
    node_info = json.load(file)

##############################################################################

class Stack:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return len(self.items) == 0

    def push(self, item):
        self.items.append(item)

    def pop(self):
        if not self.is_empty():
            return self.items.pop()
        else:
            return None  # ์Šคํƒ์ด ๋น„์–ด์žˆ์„ ๋•Œ๋Š” None ๋ฐ˜ํ™˜ ๋˜๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋„ ๊ฐ€๋Šฅ

    def peek(self):
        if not self.is_empty():
            return self.items[-1]
        else:
            return None  # ์Šคํƒ์ด ๋น„์–ด์žˆ์„ ๋•Œ๋Š” None ๋ฐ˜ํ™˜ ๋˜๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋„ ๊ฐ€๋Šฅ

    def size(self):
        return len(self.items)

    def clear(self):
        self.items = []  # ์Šคํƒ์„ ๋น„์šฐ๊ธฐ


stack = Stack()

# ๋ฐœํ™”์— ๋ช…๋ น์–ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ
# ๋ช…๋ น์–ด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ ์ถ”์ถœ
def classifier(utterance):

    print(utterance)

    if utterance == "reset":
        stack.clear()
        stack.push(node_info["items"][0])
        stack.push(node_info["items"][1])
        return str("Reset!")

    top = stack.peek()

    global_check = re.compile(r'^#global')

    # top node ์˜ dest node ๋ฅผ ๋ชจ๋‘ ๊ฐ€์ ธ์˜จ๋‹ค.
    for dest_node in top['dest_node']:

        # node_info ์—์„œ start_node == dest_node ์ธ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค.
        for node in node_info["items"]:

            if node["utterance"] == utterance:
                if node["fallback"] is not None:
                    if global_check.match(node["fallback"]):
                        node["fallback"] = int(re.sub(r'\D', '', node["fallback"]))
                        while(True):
                            if stack.size() == 0:
                                return
                            peek = stack.peek()
                            if peek['start_node'] == node["fallback"]:
                                return str(str(node['depth']) + ':' + node['intent'])
                            else:
                                stack.pop()

            if node['start_node'] == dest_node:
                # candidate_node.append(node)
                # ์ตœ์‹  ๋…ธ๋“œ๋ณด๋‹ค ๋ฐœํ™” ๋…ธ๋“œ๊ฐ€ ๋‹ค์Œ state ์ธ์ง€ ํ™•์ธ
                if top['depth']+1 == node['depth']:
                    # dest_node ์˜ uttr ์ถ”์ถœ -> ๋ฐœํ™”์— / ๋ฐœํ™”๊ฐ€ ํฌํ•จ๋˜๋Š”์ง€ ํ™•์ธ
                    # node['uttrance'] ์— ๊ฐ’์ด ๋‹ค์ˆ˜ ๋“ค์–ด์žˆ๋Š” ๊ฒฝ์šฐ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
                    # if '\n' in node['utterance']:
                    node_utter = node['utterance'].split('\n')
                    for n in node_utter:
                        if utterance in n or n in utterance:
                            stack.push(node)
                            # print(node['intent'])

                            if node['fallback']:
                                while(True):
                                    if stack.size() == 0:
                                        return
                                    peek = stack.peek()
                                    if peek['start_node'] == node['fallback']:
                                        return str(str(node['depth']) + ':' + node['intent'])
                                    else:
                                        stack.pop()
                            else:
                                return str(str(node['depth']) + ':' + node['intent'])
    return str("Can't run this command!")

# 1. ์ฐจํŠธ ์›น ๊ตฌ๋™
## state = 1
stack.push(node_info["items"][0])

# 2. ํŽ˜๋ฆฌ์˜ค / ์ž„ํ”Œ๋ž€ํŠธ ๊ฐ ์ฐจํŠธ ๋ถ„๊ธฐ
## ํŽ˜๋ฆฌ์˜ค ์ฐจํŠธ๋กœ ๋ถ„๊ธฐ
stack.push(node_info["items"][1])

demo = gr.Interface(fn=classifier, inputs="textbox", outputs="textbox",
                    examples=[
                        ["start listening"],
                        ["select tooth 1"],
                        ["pocket depth 1 2 3"],
                        ["select tooth 2"],
                        ["treatment note"],
                        ["missing"],
                        ["select tooth 5 buccal mesial pocket depth"],
                        ["save"],
                        ["stop listening"],
                        ["reset"]
                              ])
    
if __name__ == "__main__":
    demo.launch()