forked from rentala/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSGFGrammar.py
171 lines (132 loc) · 4.5 KB
/
JSGFGrammar.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
This file lays out the class structure for a JSGF Grammar.
.. module:: JSGFGrammar
.. moduleauthor:: Timothy Ho <[email protected]>
"""
# @copyright: (c)Copyright 2014, THC All Rights Reserved.
# The source code contained or described here in and all documents related
# to the source code ("Material") are owned by THC or its
# suppliers or licensors. Title to the Material remains with THC
# or its suppliers and licensors. The Material contains trade secrets and
# proprietary and confidential information of THC or its suppliers and
# licensors.
# The Material is protected by worldwide copyright and trade secret laws and
# treaty provisions. No part of the Material may be used, copied, reproduced,
# modified, published, uploaded, posted, transmitted, distributed, or disclosed
# in any way without THC's prior express written permission.
# No license under any patent, copyright, trade secret or other intellectual
# property right is granted to or conferred upon you by disclosure or delivery
# of the Materials, either expressly, by implication, inducement, estoppel or
# otherwise. Any license under such intellectual property rights must be express
# and approved by THC in writing.
# @organization: THC Science
# @summary: This file lays out the class structure for a JSGF Grammar
# @since: 2014/06/02
class JSGFExpression():
pass
class Disjunction(JSGFExpression):
"""
Disjunction class stores disjuncts in a list
"""
def __init__(self, disjuncts):
self.disjuncts = disjuncts
def __str__(self):
return '( ' + ' | '.join(map(str,self.disjuncts)) + ' )'
def __repr__(self):
return str(self)
def __getitem__(self):
return self.disjuncts
class Optional(JSGFExpression):
"""
Optional class stores either a JSGFExpression, list, or string
as its optional element
"""
def __init__(self, option):
self.option = option
def __str__(self):
return ('[ ' + str(self.option) + ' ]')
def __repr__(self):
return str(self)
def __getitem__(self):
return self.option
class NonTerminal(JSGFExpression):
"""
NonTerminal class simply stores the label of the nonterminal
"""
def __init__(self, ntName):
self.name = ntName
def __str__(self):
return self.name
def __repr__(self):
return str(self)
def __getitem__(self):
return self.name
class Rule():
"""
Rule class, represents a JSGF rule, with a nonterminal name representing the
left hand side, and a list of possible expansions representing the right
hand side.
"""
def __init__(self):
"""
constructor with no args
"""
self.lhs = ''
self.rhs = []
def __init__(self, lhs):
"""
constructor with nonterminal name
"""
pass
def __init__(self, lhs, rhs):
"""
constructor with full rule definition
"""
self.lhs = lhs
self.rhs = rhs
def __str__(self):
return (str(self.lhs) + ' -> ' + str(self.rhs))
def __repr__(self):
return str(self)
class Grammar():
"""
Grammar class which contains a list for public rules and a list
for all rules.
"""
def __init__(self):
self.rules = []
self.publicRules = []
def addRule(self, rule):
"""
adds a rule to the list of rules
"""
self.rules.append(rule)
def addPublicRule(self, rule):
"""
adds a rule to the list of public rules
"""
self.publicRules.append(rule)
def getRHS(self, nt):
"""
returns rule definition
:param nt: Non-Terminal (variable) whose definition to get
"""
for rule in self.rules:
#print 'checking', nt.name, 'and', rule.lhs, type(nt.name), rule.lhs.name
if rule.lhs.name == nt.name:
return rule.rhs
raise ValueError("Rule not defined for " + str(nt))
def __getitem__(self, nt):
#rhsides = [] to do for multiple rhsides
for rule in self.rules:
if rule.lhs == nt:
return rule.rhs
else:
raise ValueError('Rule not defined for ' + str(nt))
def __str__(self):
return 'All Rules:' + str(self.rules) + '\n' + 'Public Rules:' + str(self.publicRules)
if __name__ == "__main__":
jgDisj = Disjunction(['hello', 'world'])
jgOpt = Optional(jgDisj)
jgRule = Rule("<greeting>", jgOpt)
print jgRule