maciejkula / glove-python

Toy Python implementation of http://www-nlp.stanford.edu/projects/glove/
Apache License 2.0
1.25k stars 319 forks source link

__contains__ and __getitem__ support #29

Closed ChristianSch closed 8 years ago

ChristianSch commented 9 years ago

Hey there!

Have you thought about implementing __contains__ for "man" in model or __getitem__ for model["man"] support? That would be really neat. I tried monkey patching them, but it did not work (not sure why to be honest). The functions could be something like this (I think):

def __getitem__(self, word):
    try:
        word_idx = self.dictionary[word]
    except KeyError:
        raise Exception('Word not in dictionary')

    return self.word_vectors[word_idx]

def __contains__(self, word):
    return word in self.dictionary

I would have done a pull request but I am too uncertain about cython and stuff.