(we_file, w2i_file, use_brown=True, n_files=100)
| 122 | |
| 123 | |
| 124 | def main(we_file, w2i_file, use_brown=True, n_files=100): |
| 125 | if use_brown: |
| 126 | cc_matrix = "cc_matrix_brown.npy" |
| 127 | else: |
| 128 | cc_matrix = "cc_matrix_%s.npy" % n_files |
| 129 | |
| 130 | # hacky way of checking if we need to re-load the raw data or not |
| 131 | # remember, only the co-occurrence matrix is needed for training |
| 132 | if os.path.exists(cc_matrix): |
| 133 | with open(w2i_file) as f: |
| 134 | word2idx = json.load(f) |
| 135 | sentences = [] # dummy - we won't actually use it |
| 136 | else: |
| 137 | if use_brown: |
| 138 | keep_words = set([ |
| 139 | 'king', 'man', 'woman', |
| 140 | 'france', 'paris', 'london', 'rome', 'italy', 'britain', 'england', |
| 141 | 'french', 'english', 'japan', 'japanese', 'chinese', 'italian', |
| 142 | 'australia', 'australian', 'december', 'november', 'june', |
| 143 | 'january', 'february', 'march', 'april', 'may', 'july', 'august', |
| 144 | 'september', 'october', |
| 145 | ]) |
| 146 | sentences, word2idx = get_sentences_with_word2idx_limit_vocab(n_vocab=5000, keep_words=keep_words) |
| 147 | else: |
| 148 | sentences, word2idx = get_wikipedia_data(n_files=n_files, n_vocab=2000) |
| 149 | |
| 150 | with open(w2i_file, 'w') as f: |
| 151 | json.dump(word2idx, f) |
| 152 | |
| 153 | V = len(word2idx) |
| 154 | model = Glove(100, V, 10) |
| 155 | |
| 156 | # alternating least squares method |
| 157 | model.fit(sentences, cc_matrix=cc_matrix) |
| 158 | model.save(we_file) |
| 159 | |
| 160 | |
| 161 | if __name__ == '__main__': |
no test coverage detected