<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Moshe Hazoom on Medium]]></title>
        <description><![CDATA[Stories by Moshe Hazoom on Medium]]></description>
        <link>https://medium.com/@hazoommo?source=rss-d909549bbbf9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8CZKfk56xdFrtzw7KxMSjw.jpeg</url>
            <title>Stories by Moshe Hazoom on Medium</title>
            <link>https://medium.com/@hazoommo?source=rss-d909549bbbf9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 00:06:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hazoommo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Word2Vec For Phrases — Learning Embeddings For More Than One Word]]></title>
            <link>https://medium.com/data-science/word2vec-for-phrases-learning-embeddings-for-more-than-one-word-727b6cf723cf?source=rss-d909549bbbf9------2</link>
            <guid isPermaLink="false">https://medium.com/p/727b6cf723cf</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Moshe Hazoom]]></dc:creator>
            <pubDate>Sat, 22 Dec 2018 21:38:10 GMT</pubDate>
            <atom:updated>2018-12-26T08:24:10.681Z</atom:updated>
            <content:encoded><![CDATA[<h3>Word2Vec For Phrases — Learning Embeddings For More Than One Word</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BtlRQK8LORs-Poq5" /><figcaption>Photo by <a href="https://unsplash.com/@alex_tsl?utm_source=medium&amp;utm_medium=referral">Alexandra</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>How to learn similar terms in a given unsupervised corpus using Word2Vec</h3><p>When it comes to semantics, we all know and love the famous Word2Vec [1] algorithm for creating word embeddings by distributional semantic representations in many NLP applications, like NER, Semantic Analysis, Text Classification and many more.</p><p>However, the limitation of the current implementation of Word2Vec algorithm is its <strong>uni-gram natural behavior</strong>. In Word2Vec, we are trying to predict a given word based on its context (CBOW), or predicting a surrounding context based on a given word (Skip-Gram). But what if we would like to embed the term “American Airlines” as its whole? In this post I will explain how to create embeddings for more than uni grams using unsupervised text corpus. If you are familiar with Word2Vec algorithm and word embeddings, you can skip the first part of this post.</p><p>Specifically, we will cover:</p><ol><li>Introduction on words representation in NLP tasks.</li><li>The Distributional Hypothesis [2] and Word2Vec algorithm.</li><li>Learning phrases from unsupervised text.</li><li>How to extract similar phrases to a given phrase.</li></ol><h3>Background</h3><p>The current company I work for, <a href="https://www.amenityanalytics.com/">Amenity Analytics</a>, is building Text Analytics products while focusing on the Finance domain. It helps businesses get actionable insights on huge scale. Recently, we release a new search engine based on Elastic Search to help our clients get a more precise and focused view on their data. After looking into users’ queries in the search engine, we noticed that many clients are searching for financial terms, while naively performing a Full Text Search with the query is not good enough. For example, one term that came up many times in users’ searches is “Inflection Point”.</p><p>Looking for the definition of “Inflection Point” in <a href="https://www.investopedia.com/">Investopedia</a>:</p><p>“An inflection point is an event that results in a significant change in the progress of a company, industry, sector, economy or geopolitical situation and can be considered a turning point after which a dramatic change, with either positive or negative results, is expected to result”</p><p>Our clients want to see significant events in the companies they are following, thus, we need to search for more terms with the same meaning as “Inflection Point”, like “Turning Point”, “Tipping point”, etc.</p><h3>From Discrete Symbols to Continuous Vectors</h3><p><strong>Words Representation</strong></p><p>The most granular objects in language are characters, which forms words, or tokens. Words (and character) are discrete and symbolic. There is no way to tell that “Labrador” and “dog” are somehow related to each other just by looking on the words as is, or looking on the characters that compose them.</p><p><strong>Bag of Words (BOW)</strong></p><p>The most common feature extraction for NLP tasks is bag-of-words (BOW) approach. In bag-of-words, we look at the histogram of word occurrences in a given corpus, without considering the order. Often, we look for more than just one word, but also on bi-grams (“I want”), tri-gram (“I want to”), or n-grams in the general case. It’s a common approach to normalize the counts for each word because the documents can differ in length (in most cases).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*uQbVcvtoUeKVEwieOPhc5A.png" /><figcaption>Normalized BOW.</figcaption></figure><p>One of the main drawbacks of BOW representation is that it’s discrete and cannot capture semantic relationship between words.</p><p><strong>Term Frequency — Document Inverse Frequency (TF-IDF)</strong></p><p>One of the outcomes of BOW representation is that it gives a score for words that appeared many times, but many of them don’t give any meaningful information, like “to” and “from”. We want to distinguish between words that appear many times and are commons words to words that appear many times but gives information about the specific document. Weighting the BOW vectors is a common practice and one of the most used weighting approach is TF-IDF (Manning et al., 2008).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*bnMGC7iVd3qPRIf0HuWDEw.png" /><figcaption>TF-IDF weighting formula. There are many variations for TF-IDF, one can read more about it <a href="https://en.wikipedia.org/wiki/Tf%E2%80%93idf">here</a>.</figcaption></figure><p>However, both BOW and TF-IDF cannot capture the semantic meaning of words, because they represents words, or n-grams, in a discrete way.</p><h3>Learning Word From its Context</h3><h4>“Tell me who your friends are, and I will tell you who you are.”</h4><p><strong>The Distributional Hypothesis</strong> is that words that occur in the same contexts tend to have similar meanings [2]. It’s the basis for semantic analysis of text. The idea behind the hypothesis, is that we can learn words meaning by looking on the context they appear at. One can easily tell that the word “play” in the sentence “The boy loves to play outside” has a different meaning than the word “play” in the sentence “The play was fantastic”. In general, words that are close to the target word are more informative, but in some cases there are long dependencies in the sentences between the target word and words that “far” from it. Many approaches for learning word from its context have been developed during the years, among them the famous Word2Vec, which will be covered in this post because its massive popularity both in the academia and the industry.</p><p><strong>Word2Vec</strong></p><p>The Distributional Hypothesis is the main idea behind Word2Vec. In Word2Vec, we have a large unsupervised corpus and for each word in the corpus, we try to predict it by its given context (CBOW), or trying to predict the context given a specific word (Skip-Gram). Word2Vec is a (shallow) neural network with one hidden layer (with dimension d) and optimization function of Negative-Sampling or Hierarchical Softmax (One can read this <a href="https://arxiv.org/pdf/1310.4546">paper</a> for more details). The training phase we iterate through the tokens in the corpus (the target word) and look at a window of size k (k words to each side of the target word, with the values between 2–10 in general).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GkL9XH1XXSUU0RfAHKJGCg.png" /><figcaption>Image <a href="https://arxiv.org/pdf/1301.3781.pdf">source</a></figcaption></figure><p>At the end of the training, we will get from the network the following embedding matrix:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/1*rfxrcsI_lCYtes41Pw6eJQ.png" /><figcaption>Emdedding matrix after Word2Vec training</figcaption></figure><p>Now, each word will not be represented by a discrete and sparse vector, but by a d-dimension continuous vector, and the meaning of each word will be captured by its relation to other words [5]. The reason behind this is that in training time, if two target words share the some context, intuitively the weight of the network for this two target words will be close to each other and thus their matching vectors. Thus, we get a distributional representation for each word in the corpus, in contrast to count based approaches (like BOW and TF-IDF). Because of the distributional behavior, a specific dimension in the vector doesn’t give any valuable information, but looking the (distributional) vector as a whole, one can perform many similarity tasks. For example, we get that V(“King”)-V(“Man”)+V(“Woman) ~= V(“Queen”) and V(“Paris”)-V(“France)+V(“Spain”) ~= V(“Madrid”). In addition, we can perform similarity measures, like cosine-similarity, between the vectors and get that the vector of the word “president” will be close to “Obame”, “Trump”, “CEO”, “chairman”, etc.</p><p>As seen above, we can perform many similarity tasks on words using Word2Vec. But, as we mentioned above, we want to do the same for more than one word.</p><h3>Learning Phrases From Unsupervised Text (Collocation Extraction)</h3><p>We can easily create bi-grams with our unsupervised corpus and take it as an input to Word2Vec. For example, the sentence “I walked today to the park” will be converted to “I_walked walked_today today_to to_the the_park” and each bi-gram will be treated as a uni-gram in the Word2Vec training phrase. It will work, but there are some problems with this approach:</p><ol><li>It will learn embeddings <strong>only</strong> for bi-grams, while many of this bi-grams are not really meaningful (for example, “walked_today”) and we will miss embeddings for uni-gram, like “walked” and “today”.</li><li>Working only with bi-grams creates a very sparse corpus. Think for example about the above sentence “I walked today to the park”. Let’s say the target word is “walked_today”, this term is not very common in the corpus and we will not have many context examples to learn a representative vector for this term.</li></ol><p>So, how we overcome this problem? how do we extract only meaningful terms while keeping words as uni-gram if their mutual information is strong enough? As always, the answer is inside the question — <strong>mutual information</strong>.</p><p><strong>Mutual Information (MI)</strong></p><p>Mutual information between two random variables X and Y is a measure of the dependence between X and Y. Formally:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*GiOqNFlWcReu3xbeh2p2Uw.png" /><figcaption>Mutual Information (MI) of random variables X and Y.</figcaption></figure><p>In our case, X and Y represents all bi-grams in corpus such that y comes right after x.</p><p><strong>Pointwise Mutual Information (PMI)</strong></p><p>PMI is a measure of the dependence between a concrete occurrences of x of y. For example: x=walked, y=today. Formally:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*_cKLD7DLm_wWND6XQb_i9Q.png" /><figcaption>PMI of concrete occurrences of x and y.</figcaption></figure><p>It’s easy to see that when two words x and y appear together many times, but not alone, PMI(x;y) will have a high value, while it will have a value of 0 if x and y are completely independent.</p><p><strong>Normalized Pointwise Mutual Information (NPMI)</strong></p><p>While PMI is a measure for the dependence of occurrences of x and y, we don’t have an upper bound on its values [3]. We want a measure that can be compared between all bi-grams, thus we can choose only bi-grams above a certain threshold. We want the PMI measure to have a maximum value of 1 on perfectly correlated words x and y. Formally:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*mvtEkSN6835lSnAcpbqKkQ.png" /><figcaption>Normalized Pointwise Mutual Information of x and y.</figcaption></figure><p><strong>Data-driven Approach</strong></p><p>Another way to extract phrases from text is by using the next formula [4] that takes into account the uni-grams and bi-grams count and a discounting coefficient for preventing of creation of bi-grams of too rare words. Formally:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*hMbzVQ_sT8z8kUpJcdhnjw.png" /><figcaption>Read this <a href="https://arxiv.org/pdf/1310.4546.pdf">article</a> for more details.</figcaption></figure><h3>Extract Similar Phrases</h3><p>Now that we have a way to extract meaningful bi-grams from out large unsupervised corpus, we can replace bi-grams with a NPMI above a certain threshold to one uni-gram, for example: “inflection point” will be transformed to “inflection_point”. It’s easy to create tri-grams by using the transformed corpus with bi-grams and running again the process (with a lower threshold) for form tri-grams. Similarly, we can continue this process to n-grams with a decreasing threshold.</p><p>Our corpus consists of ~60 million sentences that contain 1.6 billion words in total. It took us 1 hour to construct bi-grams using the data-driven approach. Best results achieved with a threshold of 7 and a minimum term count of 5.</p><p>We measured the results using an evaluation set that contains important bi-grams that we want to identify, like financial terms, people names (mostly CEOs and CFOs) cities, countries, etc. The metric we used is a simple recall: from our extracted bi-grams, what is the coverage in the evaluation test. In this specific task, we care more about the recall instead of the precision so we allowed our self to use a relatively small threshold when extracting the bi-grams. We do take in consider that our precision might get worse when lowering the threshold and in turn we might extract bi-grams that are not very valuable, but that’s preferable than missing important bi-grams, when performing Query Expansion task.</p><p><strong>Example Code</strong></p><p>Reading corpus line by line (we assume each line contain one sentence) in a memory efficient approach:</p><pre>def get_sentences(input_file_pointer):<br>    while True:<br>        line = input_file_pointer.readline()<br>        if not line:<br>            break<br><br>        yield line</pre><p>Clean sentences by trimming leading and trailing spaces, lower case, remove punctuation, remove unnecessary characters and reduce duplicate space into a single space (note that this is not really necessary because we later on tokenize our sentence by space character):</p><pre>import re</pre><pre>def clean_sentence(sentence):<br>    sentence = sentence.lower().strip()<br>    sentence = re.sub(r’[^a-z0-9\s]’, &#39;’, sentence)<br>    return re.sub(r’\s{2,}’, &#39; &#39;, sentence)</pre><p>Tokenize each line by a simple space delimiter (more advanced techniques for tokenization exist, but tokenize by a simple space gave us good results and works well n practice), and remove stop-words. Removing stop-words is task dependent and in some NLP tasks, keeping the stop-words yields better results. One should evaluate both approaches. For this task, we used Spacy’s stop-word set.</p><pre>from spacy.lang.en.stop_words import STOP_WORDS</pre><pre>def tokenize(sentence):<br>    return [token for token in sentence.split() if token not in STOP_WORDS]</pre><p>Now, that we have a representations of our sentences by a 2-d matrix of cleaned tokens, we can build bi-grams. We will use <a href="https://radimrehurek.com/gensim/">Gensim</a> library that is really recommended for NLP semantic tasks. Fortunately, Genim has an implementation for phrases extraction, both with NPMI and the above data-driven approach of Mikolov et al. One can control the hyperparameters easily, like determining the minimum term count, threshold and scoring (‘default’ for data-driven approach and ‘npmi’ for NPMI). Note that values are different between the two approaches and one needs to take it into account.</p><pre>from gensim.models.phrases import Phrases, Phraser</pre><pre>def build_phrases(sentences):<br>    phrases = Phrases(sentences,<br>                      min_count=5,<br>                      threshold=7,<br>                      progress_per=1000)<br>    return Phraser(phrases)</pre><p>After we finish building the phrases model, we can save it easily and load it later:</p><pre>phrases_model.save(&#39;phrases_model.txt&#39;)</pre><pre>phrases_model= Phraser.load(&#39;phrases_model.txt&#39;)</pre><p>Now that we have a phrases model, we can use it to extract bi-grams for a given sentence:</p><pre>def sentence_to_bi_grams(phrases_model, sentence):<br>    return &#39; &#39;.join(phrases_model[sentence])</pre><p>We want to create, based on our corpus, a new corpus with meaningful bi-grams concatenated together for later use:</p><pre>def sentences_to_bi_grams(n_grams, input_file_name, output_file_name):<br>    with open(input_file_name, &#39;r&#39;) as input_file_pointer:<br>        with open(output_file_name, &#39;w+&#39;) as out_file:<br>            for sentence in get_sentences(input_file_pointer):<br>                cleaned_sentence = clean_sentence(sentence)<br>                tokenized_sentence = tokenize(cleaned_sentence)<br>                parsed_sentence = sentence_to_bi_grams(n_grams, tokenized_sentence)<br>                out_file.write(parsed_sentence + &#39;\n&#39;)</pre><h3><strong>Learning Similar Phrases</strong></h3><p>After this above phrase, our corpus contains phrases and we can use it as input into Word2Vec training (maybe changing hyper-parameters will be necessary) , same like before. The training phrase will consider “inflection_point” as one word and will learn a distributed d-dimensional vector that will be close to the vectors of terms like “tipping_point” or “inflection”, which is our goal!</p><p>On our 1.6 billion words corpus, it took us 1 hour to construct bi-grams and another 2 hours to train Word2Vec (with batch Skip-Gram, 300 dimension, 10 epochs, context of k=5 , negative sampling of 5, learning rate of 0.01 and minimum word count of 5) on a machine with 16 CPUs and 64 RAM using AWS Sagemaker service. A great Notebook example of how to use AWS Sagemaker service to train Word2Vec can be found <a href="https://github.com/awslabs/amazon-sagemaker-examples/blob/master/introduction_to_amazon_algorithms/blazingtext_word2vec_text8/blazingtext_word2vec_text8.ipynb">here</a>.</p><p>One can also use Gensim library to train Word2Vec model, for example <a href="http://kavita-ganesan.com/gensim-word2vec-tutorial-starter-code/#.XB6mR1wzZPY">here</a>.</p><p>For example, when giving the term “Inflection Point”, we get back the following related terms, ordered by their cosine-similarity score from their represented vector and the vector of “inflection_point”:</p><pre>&quot;terms&quot;: [<br>        {<br>          &quot;term&quot;: &quot;inflection&quot;,<br>          &quot;score&quot;: 0.741<br>        },<br>        {<br>          &quot;term&quot;: &quot;tipping_point&quot;,<br>          &quot;score&quot;: 0.667<br>        },<br>        {<br>          &quot;term&quot;: &quot;inflexion_point&quot;,<br>          &quot;score&quot;: 0.637<br>        },<br>        {<br>          &quot;term&quot;: &quot;hit_inflection&quot;,<br>          &quot;score&quot;: 0.624<br>        },<br>        {<br>          &quot;term&quot;: &quot;inflection_points&quot;,<br>          &quot;score&quot;: 0.606<br>        },<br>        {<br>          &quot;term&quot;: &quot;reached_inflection&quot;,<br>          &quot;score&quot;: 0.583<br>        },<br>        {<br>          &quot;term&quot;: &quot;cusp&quot;,<br>          &quot;score&quot;: 0.567<br>        },<br>        {<br>          &quot;term&quot;: &quot;reaching_inflection&quot;,<br>          &quot;score&quot;: 0.546<br>        },<br>        {<br>          &quot;term&quot;: &quot;reached_tipping&quot;,<br>          &quot;score&quot;: 0.518<br>        },<br>        {<br>          &quot;term&quot;: &quot;hitting_inflection&quot;,<br>          &quot;score&quot;: 0.501<br>        }<br>      ]</pre><p>Some of our clients wanted to see the affect of Black Friday on companies’ sales, so when giving the term “Black Friday” we get:</p><pre>&quot;terms&quot;: [<br>        {<br>          &quot;term&quot;: &quot;cyber_monday&quot;,<br>          &quot;score&quot;: 0.815<br>        },<br>        {<br>          &quot;term&quot;: &quot;thanksgiving_weekend&quot;,<br>          &quot;score&quot;: 0.679<br>        },<br>        {<br>          &quot;term&quot;: &quot;holiday_season&quot;,<br>          &quot;score&quot;: 0.645<br>        },<br>        {<br>          &quot;term&quot;: &quot;thanksgiving_holiday&quot;,<br>          &quot;score&quot;: 0.643<br>        },<br>        {<br>          &quot;term&quot;: &quot;valentine_day&quot;,<br>          &quot;score&quot;: 0.628<br>        },<br>        {<br>          &quot;term&quot;: &quot;mother_day&quot;,<br>          &quot;score&quot;: 0.628<br>        },<br>        {<br>          &quot;term&quot;: &quot;christmas&quot;,<br>          &quot;score&quot;: 0.627<br>        },<br>        {<br>          &quot;term&quot;: &quot;shopping_cyber&quot;,<br>          &quot;score&quot;: 0.612<br>        },<br>        {<br>          &quot;term&quot;: &quot;holiday_shopping&quot;,<br>          &quot;score&quot;: 0.608<br>        },<br>        {<br>          &quot;term&quot;: &quot;holiday&quot;,<br>          &quot;score&quot;: 0.605<br>        }<br>      ]</pre><p>Pretty cool, isn’t it?</p><h3>Conclusion</h3><p>In this post we covered different approaches for word representation in NLP tasks (BOW, TF-IDF and Word Embeddings), learnt how to learn word representation from its context using Word2Vec, saw how we can extract meaningful phrases from a given corpus (NPMI and data-driven approach) and how to transform a given corpus in order to learn similar terms/words for each one of extracted terms/words using Word2Vec algorithm. The results of this process can be used in a downstream task, like Query Expansion in Information Extraction tasks, Document Classification, Clustering, Question-Answering and many more.</p><p>Thanks for reading!</p><h3>References</h3><p>[1] Mikolov, T., Chen, K., Corrado, G.S., &amp; Dean, J. (2013). Efficient Estimation of Word Representations in Vector Space. <em>CoRR, abs/1301.3781</em>.</p><p>[2] Harris, Z. (1954). Distributional structure. <em>Word</em>, 10(23): 146–162.</p><p>[3] Bouma, G. (2009). Normalized ( Pointwise ) Mutual Information in Collocation Extraction.</p><p>[4] Mikolov, T., Sutskever, I., Chen, K., Corrado, G.S., &amp; Dean, J. (2013). Distributed Representations of Words and Phrases and their Compositionality. <em>NIPS</em>.</p><p>[5] Goldberg, Y., Hirst, G., Liu, Y., &amp; Zhang, M. (2017). Neural Network Methods for Natural Language Processing. <em>Computational Linguistics, 44</em>, 193–195.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=727b6cf723cf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/word2vec-for-phrases-learning-embeddings-for-more-than-one-word-727b6cf723cf">Word2Vec For Phrases — Learning Embeddings For More Than One Word</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>