(self, pi, word2idx)
| 180 | ) |
| 181 | |
| 182 | def generate(self, pi, word2idx): |
| 183 | # convert word2idx -> idx2word |
| 184 | idx2word = {v:k for k,v in iteritems(word2idx)} |
| 185 | V = len(pi) |
| 186 | |
| 187 | # generate 4 lines at a time |
| 188 | n_lines = 0 |
| 189 | |
| 190 | # why? because using the START symbol will always yield the same first word! |
| 191 | X = [ np.random.choice(V, p=pi) ] |
| 192 | print(idx2word[X[0]], end=" ") |
| 193 | |
| 194 | while n_lines < 4: |
| 195 | # print "X:", X |
| 196 | P = self.predict_op(X)[-1] |
| 197 | X += [P] |
| 198 | if P > 1: |
| 199 | # it's a real word, not start/end token |
| 200 | word = idx2word[P] |
| 201 | print(word, end=" ") |
| 202 | elif P == 1: |
| 203 | # end token |
| 204 | n_lines += 1 |
| 205 | print('') |
| 206 | if n_lines < 4: |
| 207 | X = [ np.random.choice(V, p=pi) ] # reset to start of line |
| 208 | print(idx2word[X[0]], end=" ") |
| 209 | |
| 210 | |
| 211 | def train_poetry(): |
no outgoing calls
no test coverage detected