Package: text-classification-keras
Text Classification KerasA high-level text classification library implementing various well-established models. With a clean and extendable interface to implement custom architectures.Quick startInstallpipinstalltext-classification-keras[full]The[full]will additionally installTensorFlow,Spacy, andDeep Plots. Choose this if you want to get started right away.Usagefromtexclaimportexperiment,datafromtexcla.modelsimportTokenModelFactory,YoonKimCNNfromtexcla.preprocessingimportFastTextWikiTokenizer# input textX=['some random text','another random text lala','peter',...]# input labelsy=['a','b','a',...]# use the special tokenizer used for constructing the embeddingstokenizer=FastTextWikiTokenizer()# preprocess data (once)experiment.setup_data(X,y,tokenizer,'data.bin',max_len=100)# load datads=data.Dataset.load('data.bin')# construct basefactory=TokenModelFactory(ds.num_classes,ds.tokenizer.token_index,max_tokens=100,embedding_type='fasttext.wiki.simple',embedding_dims=300)# choose a modelword_encoder_model=YoonKimCNN()# build a modelmodel=factory.build_model(token_encoder_model=word_encoder_model,trainable_embeddings=False)# use experiment.train as wrapper for Keras.fit()experiment.train(x=ds.X,y=ds.y,validation_split=0.1,model=model,word_encoder_model=word_encoder_model)Check out moreexamples.API Documenationhttps://github.io/jfilter/text-classification-keras/AdvancedEmbeddingsChoose a pre-trained word embedding by setting the embedding_type and the corresponding embedding dimensions. Setembedding_type=Noneto initialize the word embeddings randomly (but make sure to settrainable_embeddings=Trueso you actually train the embeddings).factory=TokenModelFactory(embedding_type='fasttext.wiki.simple',embedding_dims=300)FastTextSeveral pre-trainedFastTextembeddings are included. For now, we only have the word embeddings and not the n-gram features. All embedding have 300 dimensions.English Vectors: e.g.fasttext.wn.1M.300d,check out all avaiable embeddingsMultilang Vectors: in the formatfasttext.cc.LANG_CODEe.g.fasttext.cc.enWikipedia Vectors: in the formatfasttext.wiki.LANG_CODEe.g.fasttext.wiki.enGloVeTheGloVeembeddings are some kind of predecessor to FastText. In general choose FastText embeddings over GloVe. The dimension for the pre-trained embeddings varies.: e.g.glove.6B.50d,check out all avaiable embeddingsTokenzationTo work on token (or word) level, use a TokenTokenizer such e.g.TwokenizeTokenizerorSpacyTokenizer.To work on token and sentence level, useSpacySentenceTokenizer.To create an custom Tokenizer, extendTokenizerand implement thetoken_generatormethod.SpacyYou may usespaCyfor the tokenization. See instructions on how todownload modelfor your target language. E.g. for English:python-mspacydownloadenModelsToken-based ModelsWhen working on token level, useTokenModelFactory.fromtexcla.modelsimportTokenModelFactory,YoonKimCNNfactory=TokenModelFactory(tokenizer.num_classes,tokenizer.token_index,max_tokens=100,embedding_type='glove.6B.100d')word_encoder_model=YoonKimCNN()model=factory.build_model(token_encoder_model=word_encoder_model)Currently supported models include:Yoon Kim CNNStacked RNNsAttention (with/without context) based RNN encodersTokenModelFactory.build_modeluses the provided word encoder which is then classified via aDenselayer.Sentence-based ModelsWhen working on sentence level, useSentenceModelFactory.# Pad max sentences per doc to 500 and max words per sentence to 200.# Can also use `max_sents=None` to allow variable sized max_sents per mini-batch.factory=SentenceModelFactory(10,tokenizer.token_index,max_sents=500,max_tokens=200,embedding_type='glove.6B.100d')word_encoder_model=AttentionRNN()sentence_encoder_model=AttentionRNN()# Allows you to compose arbitrary word encoders followed by sentence encoder.model=factory.build_model(word_encoder_model,sentence_encoder_model)Hierarchical attention networks(HANs) can be build by composing two attention based RNN models. This is useful when a document is very large.For smaller document a reasonable way to encode sentences is to average words within it. This can be done by usingtoken_encoder_model=AveragingEncoder()Mix and match encoders as you see fit for your problem.SentenceModelFactory.build_modelcreated a tiered model where words within a sentence is first encoded usingword_encoder_model. All such encodings per sentence is then encoded usingsentence_encoder_model.Relatedhttps://github.com/brightmart/text_classificationhttps://github.com/allenai/allennlphttps://github.com/facebookresearch/pytexthttps://docs.fast.ai/text.htmlhttps://github.com/dkpro/dkpro-tcContributingIf you have aquestion, found abugor want to propose a newfeature, have a look at theissues page.Pull requestsare especially welcomed when they fix bugs or improve the code quality.AcknowledgementsBuilt upon the work by Raghavendra Kotikalapudi:keras-text.CitationIf you find Text Classification Keras useful for an academic publication, then please use the following BibTeX to cite it:@misc{raghakotfiltertexclakeras
    title={Text Classification Keras},
    author={Raghavendra Kotikalapudi, and Johannes Filter, and contributors},
    year={2018},
    publisher={GitHub},
    howpublished={\url{https://github.com/jfilter/text-classification-keras}},}LicenseMIT.