質問編集履歴
3
コードの入力、タグ付け
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
SyntaxError
|
1
|
+
SyntaxErrorでシステムが起動しない
|
test
CHANGED
@@ -1,26 +1,262 @@
|
|
1
|
+
SYNTAXERRORで動きませんどう修正すればよいでしょうか?
|
2
|
+
|
3
|
+
自分では調べたり何度か打ち直ししました。
|
4
|
+
|
5
|
+
|
6
|
+
|
1
|
-
|
7
|
+
```Python
|
8
|
+
|
9
|
+
import random
|
10
|
+
|
11
|
+
import re
|
12
|
+
|
13
|
+
from analyzer import *
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
class Dictionary:
|
20
|
+
|
21
|
+
def __init__(self):
|
22
|
+
|
23
|
+
self.random = []
|
24
|
+
|
25
|
+
rfile = open('random.txt', 'r', encoding = 'utf_8')
|
26
|
+
|
27
|
+
r_lines = rfile.readlines()
|
28
|
+
|
29
|
+
rfile.close()
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
self.random = []
|
34
|
+
|
35
|
+
for line in r_lines:
|
36
|
+
|
37
|
+
str = line.rstrip('\n')
|
38
|
+
|
39
|
+
if (str!=''):
|
40
|
+
|
41
|
+
self.random.append(str)
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
pfile = open('pattern.txt', 'r', encoding = 'utf_8')
|
46
|
+
|
47
|
+
p_lines = pfile.readlines()
|
48
|
+
|
49
|
+
pfile.close()
|
50
|
+
|
51
|
+
self.new_lines = []
|
52
|
+
|
53
|
+
for line in p_lines:
|
54
|
+
|
55
|
+
str = line.rstrip('\n')
|
56
|
+
|
57
|
+
if (str!=''):
|
58
|
+
|
59
|
+
self.new_lines.append(str)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
self.pattern = []
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
for line in self.new_lines:
|
68
|
+
|
69
|
+
ptn, prs = line.split('\t')
|
70
|
+
|
71
|
+
self.pattern.setdefault('pattern', []).append(ptn)
|
72
|
+
|
73
|
+
self.pattern.setdefault('phrases', []).append(prs)
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
def study(self, input, parts):
|
78
|
+
|
79
|
+
input = input.rstrip('\n')
|
80
|
+
|
81
|
+
self.study_random(input)
|
82
|
+
|
83
|
+
self.study_pattern(input, parts)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
def study_random(self, input):
|
90
|
+
|
91
|
+
if not input in self.random:
|
92
|
+
|
93
|
+
self.random.append(input)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
def study_pattern(self, input, parts):
|
98
|
+
|
99
|
+
for word, part in parts:
|
100
|
+
|
101
|
+
if (keyword_check(part)):
|
102
|
+
|
103
|
+
depend = False
|
104
|
+
|
105
|
+
for ptn_item in self.pattern:
|
106
|
+
|
107
|
+
m = re.search(ptn_item.pattern, word)
|
108
|
+
|
109
|
+
if(re.search(ptn_item.pattern, word)):
|
110
|
+
|
111
|
+
depend = ptn_item
|
112
|
+
|
113
|
+
break
|
114
|
+
|
115
|
+
if depend:
|
116
|
+
|
117
|
+
depend.add_phrase(input)
|
118
|
+
|
119
|
+
else:
|
120
|
+
|
121
|
+
self.pattern.append(ParseItem(word, input))
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
def save(self):
|
126
|
+
|
127
|
+
for index, element in enumerate(self.random):
|
128
|
+
|
129
|
+
self.random[index] = element +'\n'
|
130
|
+
|
131
|
+
with open('random.txt', 'w', encoding = 'utf_8') as f:
|
132
|
+
|
133
|
+
f.writelines(self.random)
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
pattern = []
|
138
|
+
|
139
|
+
for ptn_item in self.pattern:
|
140
|
+
|
141
|
+
pattern.append(ptn_item.make_line() + '\n')
|
142
|
+
|
143
|
+
with open('pattern.txt', 'w', encoding = 'utf_8') as f:
|
144
|
+
|
145
|
+
f.writelines(pattern)
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
class ParseItem:
|
150
|
+
|
151
|
+
SEPARATOR = '^((-?\d+)##)?(.*)$'
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def __init__(self, pattern, phrases):
|
156
|
+
|
157
|
+
m = re.findall(ParseItem.SEPARATOR, pattern)
|
158
|
+
|
159
|
+
self.modify = 0
|
160
|
+
|
161
|
+
if m[0][1]:
|
162
|
+
|
163
|
+
self.modify =int(m[0][1])
|
164
|
+
|
165
|
+
self.pattern = m[0][2]
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
self.phrases = []
|
170
|
+
|
171
|
+
self.dic = {}
|
172
|
+
|
173
|
+
for phrase in phrases.split('|'):
|
174
|
+
|
175
|
+
m = re.findall(ParseItem.SEPARATOR, phrase)
|
176
|
+
|
177
|
+
self.dic['need'] = 0
|
178
|
+
|
179
|
+
if m[0][1]:
|
180
|
+
|
181
|
+
self.dic['need'] = int(m[0][2])
|
182
|
+
|
183
|
+
self.dic['phrase'] = m[0][2]
|
184
|
+
|
185
|
+
self.phrases.append(self.dic.copy()
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
2
190
|
|
3
191
|
def match(self, str):
|
4
192
|
|
5
|
-
|
193
|
+
return re.search(self.pattern, str)
|
6
194
|
|
7
195
|
|
8
196
|
|
197
|
+
def choice(self, mood):
|
198
|
+
|
199
|
+
choices = []
|
200
|
+
|
201
|
+
for p in self.phrases:
|
202
|
+
|
203
|
+
if (self.suitable(p['need'], mood)):
|
204
|
+
|
205
|
+
choices.append(p['phrase'])
|
206
|
+
|
207
|
+
if (len(choices) == 0):
|
208
|
+
|
209
|
+
return None
|
210
|
+
|
211
|
+
return random.choice(choices)
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
def suitable(self, need, mood):
|
216
|
+
|
217
|
+
if (need == 0):
|
218
|
+
|
219
|
+
return True
|
220
|
+
|
221
|
+
elif (need > 0):
|
222
|
+
|
223
|
+
return (mood > need)
|
224
|
+
|
225
|
+
else:
|
226
|
+
|
227
|
+
return (mood < need)
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
def add_phrase(self, phrase):
|
232
|
+
|
233
|
+
for p in self.phrases:
|
234
|
+
|
235
|
+
if p['phrase'] == phrase:
|
236
|
+
|
237
|
+
return
|
238
|
+
|
239
|
+
self.phrases.append({'need': 0, 'phrase': phrase})
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
def make_line(self):
|
244
|
+
|
245
|
+
pattern = str(self.modify) + '##' + self.pattern
|
246
|
+
|
247
|
+
phrases= []
|
248
|
+
|
249
|
+
for p in self.phrases:
|
250
|
+
|
251
|
+
phrases.append(str(p['need']) + '##' + str(p['phrase']))
|
252
|
+
|
253
|
+
return pattern + '\t' + '|'.join(phrases)
|
254
|
+
|
9
255
|
```
|
10
256
|
|
11
|
-
|
257
|
+
ERRORコード```Python
|
12
|
-
|
13
|
-
|
258
|
+
|
14
|
-
|
15
|
-
どうすればよいでしょうか
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
```ここに言語を入力
|
20
|
-
|
21
|
-
コード
|
22
|
-
|
23
|
-
|
259
|
+
Traceback (most recent call last):
|
24
260
|
|
25
261
|
File "C:\Users\ssato\AppData\Local\Programs\Python\Python37-32\PtnaForm.py", line 3, in <module>
|
26
262
|
|
@@ -30,10 +266,12 @@
|
|
30
266
|
|
31
267
|
from dictionary import *
|
32
268
|
|
33
|
-
File "C:\Users\ssato\AppData\Local\Programs\Python\Python37-32\dictionary.py", line 9
|
269
|
+
File "C:\Users\ssato\AppData\Local\Programs\Python\Python37-32\dictionary.py", line 92
|
34
270
|
|
35
271
|
def match(self, str):
|
36
272
|
|
37
273
|
^
|
38
274
|
|
39
275
|
SyntaxError: invalid syntax
|
276
|
+
|
277
|
+
```
|
2
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
```phython
|
1
|
+
~~~~```phython
|
2
2
|
|
3
3
|
def match(self, str):
|
4
4
|
|
1
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
```phython
|
2
2
|
|
3
|
-
|
3
|
+
def match(self, str):
|
4
4
|
|
5
|
-
return re.search(self.pattern, str)
|
5
|
+
return re.search(self.pattern, str)
|
6
6
|
|
7
7
|
|
8
8
|
|