Package: keywords2vec
keywords2vecA simple and fast way to generate a word2vec model, with multi-word keywords instead of single words.Example resultFinding similar keywords for "obesity"indexterm0overweight1obese2physical inactivity3excess weight4obese adults5high bmi6obese adults7obese people8obesity-related outcomes9obesity among children10poor sleep quality11ssbs12obese populations13cardiometabolic risk14abdominal obesityInstallpip install keywords2vecHow to useLets download some example datadata_filepath = "epistemonikos_data_sample.tsv.gz"

!wget "https://s3.amazonaws.com/episte-labs/epistemonikos_data_sample.tsv.gz" -O "{data_filepath}"We create the model. If you need the vectors, take a lookherelabels, tree = similars_tree(data_filepath)processing file: epistemonikos_data_sample.tsv.gz<style>
        /* Turns off some styling */
        progress {
            /* gets rid of default border in Firefox and Opera. */
            border: none;
            /* Needs to be in here for Safari polyfill so background images work as expected. */
            background-size: auto;
        }
        .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {
            background: #F44336;
        }
    </style>
  
  100.00% [201/201 00:19<00:00]Then we can get the most similars keywordsget_similars(tree, labels, "obesity")['obesity',
 'overweight',
 'obese',
 'physical inactivity',
 'excess weight',
 'high bmi',
 'obese adults',
 'obese people',
 'obesity-related outcomes',
 'obesity among children',
 'poor sleep quality',
 'ssbs',
 'obese populations',
 'cardiometabolic risk',
 'abdominal obesity']get_similars(tree, labels, "heart failure")['heart failure',
 'hf',
 'chf',
 'chronic heart failure',
 'reduced ejection fraction',
 'unstable angina',
 'peripheral vascular disease',
 'peripheral arterial disease',
 'angina',
 'congestive heart failure',
 'left ventricular systolic dysfunction',
 'acute coronary syndrome',
 'heart failure patients',
 'acute myocardial infarction',
 'left ventricular dysfunction']MotivationThe idea started in the Epistemonikos databasewww.epistemonikos.org, a database of scientific articles for people making decisions concerning clinical or health-policy questions. In this context the scientific/health language used is complex. You can easily find keywords like:asthmaheart failuremedial compartment knee osteoarthritispreserved left ventricular systolic functionnon-selective non-steroidal anti-inflammatory drugsWe tried some approaches to find those keywords, like ngrams, ngrams + tf-idf, identify entities, among others. But we didn't get really good results.Our approachWe found that tokenizing using stopwords + non word characters was really useful for "finding" the keywords. An example:input: "Timing of replacement therapy for acute renal failure after cardiac surgery"output: [
"timing",
"replacement therapy",
"acute renal failure",
"cardiac surgery"
]So we basically split the text when we find:a stopworda non word character(/,!?. etc) (except from - and ')That's it.But as there were some problem with some keywords that cointain stopwords, like:Vitamin AHepatitis AWeb of ScienceSo we decided to add another method (nltk with some grammar definition) to cover most of the cases. To use this, you need to add the parameterkeywords_w_stopwords=True, this method is approx 20x slower.ReferencesSeem to be an old idea (2004):Mihalcea, Rada, and Paul Tarau. "Textrank: Bringing order into text." Proceedings of the 2004 conference on empirical methods in natural language processing. 2004.Reading an implementation of textrank, I realize they used stopwords to separate and create the graph. Then I though in using it as tokenizer for word2vecAs pointed by @deliprao in thistwitter thread. It's also used by Rake (2010):Rose, Stuart & Engel, Dave & Cramer, Nick & Cowley, Wendy. (2010). Automatic Keyword Extraction from Individual Documents. 10.1002/9780470689646.ch1.As noted by @astent in the Twitter thread, this concept is called chinking (chunking by exclusion)https://www.nltk.org/book/ch07.html#ChinkingMulti-lingualWe worked in an implementation, that could be used in multiple languages. Of course not all languages are sutable for using this approach. We have tried with good results in English, Spanish and PortugueseTry it onlineYou can try ithere(takes time to load, lowercase only, doesn't work in mobile yet) MPV :)These embedding were created using 827,341 title/abstract from @epistemonikos database.
With keywords that repeat at least 10 times. The total vocab is 349,080 keywords (really manageable number)Vocab sizeOne of the main benefit of this method, is the size of the vocabulary.
For example, using keywords that repeat at least 10 times, for the Epistemonikos dataset (827,341 title/abstract), we got the following vocab size:ngramskeywordscomp1127,82436%1,21,360,550388%1-33,204,099914%1-44,461,9301,272%1-55,133,6191,464%stopword tokenizer350,529100%More information regarding the comparison, take a look to the folderanalyze.CreditsThis project has been created usingnbdev