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