(we_file, w2i_file, use_brown=True, n_files=50)
| 160 | |
| 161 | |
| 162 | def main(we_file, w2i_file, use_brown=True, n_files=50): |
| 163 | if use_brown: |
| 164 | cc_matrix = "cc_matrix_brown.npy" |
| 165 | else: |
| 166 | cc_matrix = "cc_matrix_%s.npy" % n_files |
| 167 | |
| 168 | # hacky way of checking if we need to re-load the raw data or not |
| 169 | # remember, only the co-occurrence matrix is needed for training |
| 170 | if os.path.exists(cc_matrix): |
| 171 | with open(w2i_file) as f: |
| 172 | word2idx = json.load(f) |
| 173 | sentences = [] # dummy - we won't actually use it |
| 174 | else: |
| 175 | if use_brown: |
| 176 | keep_words = set([ |
| 177 | 'king', 'man', 'woman', |
| 178 | 'france', 'paris', 'london', 'rome', 'italy', 'britain', 'england', |
| 179 | 'french', 'english', 'japan', 'japanese', 'chinese', 'italian', |
| 180 | 'australia', 'australian', 'december', 'november', 'june', |
| 181 | 'january', 'february', 'march', 'april', 'may', 'july', 'august', |
| 182 | 'september', 'october', |
| 183 | ]) |
| 184 | sentences, word2idx = get_sentences_with_word2idx_limit_vocab(n_vocab=5000, keep_words=keep_words) |
| 185 | else: |
| 186 | sentences, word2idx = get_wikipedia_data(n_files=n_files, n_vocab=2000) |
| 187 | |
| 188 | with open(w2i_file, 'w') as f: |
| 189 | json.dump(word2idx, f) |
| 190 | |
| 191 | V = len(word2idx) |
| 192 | model = Glove(100, V, 10) |
| 193 | model.fit(sentences, cc_matrix=cc_matrix, epochs=200) |
| 194 | model.save(we_file) |
| 195 | |
| 196 | |
| 197 | if __name__ == '__main__': |
no test coverage detected