

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>utils.language_utils &mdash; LEAF 0.1 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/customheader.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 

  
  <script src="../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../index.html" class="icon icon-home"> LEAF
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Getting Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../install/get_leaf.html">Installation and Contribution</a></li>
</ul>
<p class="caption"><span class="caption-text">User Guide</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../tutorials/index.html">General Examples</a></li>
</ul>
<p class="caption"><span class="caption-text">API Documentation</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../autodoc/modules.html">models</a></li>
</ul>
<p class="caption"><span class="caption-text">Additional Information</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../contact.html">Contact</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../citations.html">Citing LEAF</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">LEAF</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>utils.language_utils</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for utils.language_utils</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Utils for language models.&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">re</span>


<span class="c1"># ------------------------</span>
<span class="c1"># utils for shakespeare dataset</span>

<span class="n">ALL_LETTERS</span> <span class="o">=</span> <span class="s2">&quot; ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&quot;</span>
<span class="n">NUM_LETTERS</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ALL_LETTERS</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_one_hot</span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">size</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns one-hot vector with given size and value 1 at given index.&quot;&quot;&quot;</span>
    <span class="n">vec</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span><span class="p">)]</span>
    <span class="n">vec</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">index</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">vec</span>


<div class="viewcode-block" id="letter_to_vec"><a class="viewcode-back" href="../../autodoc/utils.html#utils.language_utils.letter_to_vec">[docs]</a><span class="k">def</span> <span class="nf">letter_to_vec</span><span class="p">(</span><span class="n">letter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns one-hot representation of given letter.&quot;&quot;&quot;</span>
    <span class="n">index</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">ALL_LETTERS</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">letter</span><span class="p">))</span> <span class="c1"># treating &#39; &#39; as unknown character</span>
    <span class="k">return</span> <span class="n">_one_hot</span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">NUM_LETTERS</span><span class="p">)</span></div>


<div class="viewcode-block" id="word_to_indices"><a class="viewcode-back" href="../../autodoc/utils.html#utils.language_utils.word_to_indices">[docs]</a><span class="k">def</span> <span class="nf">word_to_indices</span><span class="p">(</span><span class="n">word</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;returns a list of character indices</span>

<span class="sd">    Args:</span>
<span class="sd">        word: string</span>
<span class="sd">    </span>
<span class="sd">    Return:</span>
<span class="sd">        indices: int list with length len(word)</span>
<span class="sd">    &#39;&#39;&#39;</span>
    <span class="n">indices</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">word</span><span class="p">:</span>
        <span class="n">indices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">ALL_LETTERS</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">c</span><span class="p">)))</span> <span class="c1"># added max to account for -1</span>
    <span class="k">return</span> <span class="n">indices</span></div>


<span class="c1"># ------------------------</span>
<span class="c1"># utils for sent140 dataset</span>


<div class="viewcode-block" id="split_line"><a class="viewcode-back" href="../../autodoc/utils.html#utils.language_utils.split_line">[docs]</a><span class="k">def</span> <span class="nf">split_line</span><span class="p">(</span><span class="n">line</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Split given line/phrase into list of words</span>

<span class="sd">    Args:</span>
<span class="sd">        line: string representing phrase to be split</span>
<span class="sd">    </span>
<span class="sd">    Return:</span>
<span class="sd">        list of strings, with each string representing a word</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;[\w&#39;]+|[.,!?;]&quot;</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span></div>


<span class="k">def</span> <span class="nf">_word_to_index</span><span class="p">(</span><span class="n">word</span><span class="p">,</span> <span class="n">indd</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns index of given word based on given lookup dictionary</span>

<span class="sd">    returns the length of the lookup dictionary if word not found</span>

<span class="sd">    Args:</span>
<span class="sd">        word: string</span>
<span class="sd">        indd: dictionary with string words as keys and int indices as values</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">indd</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">indd</span><span class="p">[</span><span class="n">word</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">indd</span><span class="p">)</span>


<div class="viewcode-block" id="line_to_indices"><a class="viewcode-back" href="../../autodoc/utils.html#utils.language_utils.line_to_indices">[docs]</a><span class="k">def</span> <span class="nf">line_to_indices</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">indd</span><span class="p">,</span> <span class="n">max_words</span><span class="o">=</span><span class="mi">25</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Converts given phrase into list of word indices</span>
<span class="sd">    </span>
<span class="sd">    if the phrase has more than max_words words, returns a list containing</span>
<span class="sd">    indices of the first max_words words</span>
<span class="sd">    if the phrase has less than max_words words, repeatedly appends integer </span>
<span class="sd">    representing unknown index to returned list until the list&#39;s length is </span>
<span class="sd">    max_words</span>

<span class="sd">    Args:</span>
<span class="sd">        line: string representing phrase/sequence of words</span>
<span class="sd">        indd: dictionary with string words as keys and int indices as values</span>
<span class="sd">        max_words: maximum number of word indices in returned list</span>

<span class="sd">    Return:</span>
<span class="sd">        indl: list of word indices, one index for each word in phrase</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">line_list</span> <span class="o">=</span> <span class="n">split_line</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="c1"># split phrase in words</span>
    <span class="n">indl</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">line_list</span><span class="p">:</span>
        <span class="n">cind</span> <span class="o">=</span> <span class="n">_word_to_index</span><span class="p">(</span><span class="n">word</span><span class="p">,</span> <span class="n">indd</span><span class="p">)</span>
        <span class="n">indl</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cind</span><span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">indl</span><span class="p">)</span> <span class="o">==</span> <span class="n">max_words</span><span class="p">):</span>
            <span class="k">break</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">max_words</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">indl</span><span class="p">)):</span>
        <span class="n">indl</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">indd</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">indl</span></div>


<div class="viewcode-block" id="bag_of_words"><a class="viewcode-back" href="../../autodoc/utils.html#utils.language_utils.bag_of_words">[docs]</a><span class="k">def</span> <span class="nf">bag_of_words</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">vocab</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns bag of words representation of given phrase using given vocab.</span>

<span class="sd">    Args:</span>
<span class="sd">        line: string representing phrase to be parsed</span>
<span class="sd">        vocab: dictionary with words as keys and indices as values</span>

<span class="sd">    Return:</span>
<span class="sd">        integer list</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">bag</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">vocab</span><span class="p">)</span>
    <span class="n">words</span> <span class="o">=</span> <span class="n">split_line</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">words</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">vocab</span><span class="p">:</span>
            <span class="n">bag</span><span class="p">[</span><span class="n">vocab</span><span class="p">[</span><span class="n">w</span><span class="p">]]</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">bag</span></div>


<div class="viewcode-block" id="get_word_emb_arr"><a class="viewcode-back" href="../../autodoc/utils.html#utils.language_utils.get_word_emb_arr">[docs]</a><span class="k">def</span> <span class="nf">get_word_emb_arr</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">inf</span><span class="p">:</span>
        <span class="n">embs</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">inf</span><span class="p">)</span>
    <span class="n">vocab</span> <span class="o">=</span> <span class="n">embs</span><span class="p">[</span><span class="s1">&#39;vocab&#39;</span><span class="p">]</span>
    <span class="n">word_emb_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">embs</span><span class="p">[</span><span class="s1">&#39;emba&#39;</span><span class="p">])</span>
    <span class="n">indd</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">vocab</span><span class="p">)):</span>
        <span class="n">indd</span><span class="p">[</span><span class="n">vocab</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="n">i</span>
    <span class="n">vocab</span> <span class="o">=</span> <span class="p">{</span><span class="n">w</span><span class="p">:</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">w</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">embs</span><span class="p">[</span><span class="s1">&#39;vocab&#39;</span><span class="p">])}</span>
    <span class="k">return</span> <span class="n">word_emb_arr</span><span class="p">,</span> <span class="n">indd</span><span class="p">,</span> <span class="n">vocab</span></div>


<div class="viewcode-block" id="val_to_vec"><a class="viewcode-back" href="../../autodoc/utils.html#utils.language_utils.val_to_vec">[docs]</a><span class="k">def</span> <span class="nf">val_to_vec</span><span class="p">(</span><span class="n">size</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Converts target into one-hot.</span>

<span class="sd">    Args:</span>
<span class="sd">        size: Size of vector.</span>
<span class="sd">        val: Integer in range [0, size].</span>
<span class="sd">    Returns:</span>
<span class="sd">         vec: one-hot vector with a 1 in the val element.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">assert</span> <span class="mi">0</span> <span class="o">&lt;=</span> <span class="n">val</span> <span class="o">&lt;</span> <span class="n">size</span>
    <span class="n">vec</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span><span class="p">)]</span>
    <span class="n">vec</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">val</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">vec</span></div>

</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Talwalkar Lab

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
    

  

  <script type="text/javascript" src="../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>