Package: glovpy
glovpyPackage for interfacing Stanford's C GloVe implementation from Python.InstallationInstall glovpy from PyPI:pipinstallglovpyAdditionally the first time you import glopy it will build GloVe from scratch on your system.RequirementsWe highly recommend that you use a Unix-based system, preferably a variant of Debian.
The package needsgit,makeand a C compiler (clangorgcc) installed.Otherwise the implementation is as barebones as it gets, only the standard library and gensim are being used (gensim only for producing KeyedVectors).Example UsageHere's a quick example of how to train GloVe on 20newsgroups using Gensim's tokenizer.fromgensim.utilsimporttokenizefromsklearn.datasetsimportfetch_20newsgroupsfromglovpyimportGloVetexts=fetch_20newsgroups().datacorpus=[list(tokenize(text,lowercase=True,deacc=True))fortextintexts]model=GloVe(vector_size=25)model.train(corpus)forword,similarityinmodel.wv.most_similar("god"):print(f"{word}, sim:{similarity}")wordsimilarityexistence0.9156746864jesus0.8746870756lord0.8555182219christ0.8517201543bless0.8298447728faith0.8237065077saying0.8204566240therefore0.8177698255desires0.8094088435telling0.8083973527API Referenceclass glovpy.GloVe(vector_size, window_size, symmetric, distance_weighting, alpha, min_count, iter, initial_learning_rate, threads, memory)Wrapper around the original C implementation of GloVe.ParametersParameterTypeDescriptionDefaultvector_sizeintNumber of dimensions the trained word vectors should have.50window_sizeintNumber of context words to the left (and to the right, if symmetric is True).15alphafloatParameter in exponent of weighting function; default 0.750.75symmetricboolIf true, both future and past words will be used as context, otherwise only past words will be used.Truedistance_weightingboolIf False, do not weight cooccurrence count by distance between words. If True (default), weight the cooccurrence count by inverse of distance between the target word and the context word.Truemin_countintMinimum number of times a token has to appear to be kept in the vocabulary.5iterintNumber of training iterations.25initial_learning_ratefloatInitial learning rate for training.0.05threadsintNumber of threads to use for training.8memoryfloatSoft limit for memory consumption, in GB. (based on simple heuristic, so not extremely accurate)4.0AttributesNameTypeDescriptionwvKeyedVectorsToken embeddings in the form ofGensim keyed vectors.Methodsglovpy.GloVe.train(tokens)Train the model on a stream of texts.ParameterTypeDescriptiontokensIterable[list[str]]Stream of documents in the form of lists of tokens. The stream has to be reusable, as the model needs at least two passes over the corpus.glovpy.utils.reusable(gen_func)Function decorator that turns your generator function into an
iterator, thereby making it reusable.
You can use this if you want to reuse a generator function so that multiple passes can be made.ParametersParameterTypeDescriptiongen_funcCallableGenerator function that you want to be reusable.ReturnsReturnsTypeDescription_multigenCallableIterator class wrapping the generator function.Example usageHere's how to stream a very long file line by line in a reusable manner.fromgensim.utilsimporttokenizefromglovpy.utilsimportreusablefromglovpyimportGloVe@reusabledefstream_lines():withopen("very_long_text_file.txt")asf:forlineinf:yieldlist(tokenize(line))model=GloVe()model.train(stream_lines())