<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:10pt"><div><span><font><div style="font-size: small;">Hi Mohsen,</div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><br></div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;">code sample  that I use in my work is</div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><br></div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;">//
 gloable variables</div><div style="background-color: transparent;"><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px; background-color: transparent;">Vocab*</span><span class="Apple-tab-span" style="font-size: 12.727272033691406px; background-color: transparent; white-space: pre;">                                                                </span><span style="font-size: 12.727272033691406px; background-color: transparent;">g_srilm_vocab;</span><br></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;">Ngram*<span class="Apple-tab-span" style="white-space: pre;">                                                          </span>g_model;</span></div></div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><br></div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color:
 transparent; font-style: normal;">double GetLogWordProb(const std::string& strCurrWord,const vector<std::string>& vstrHistory)</div><div style="font-size: small;">{</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">             </span>float<span class="Apple-tab-span" style="white-space:pre">       </span>fResult;</div><div style="font-size: small;"><br></div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">            </span>size_t len = 1 + vstrHistory.size()+1;</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">           </span>VocabIndex* WordIDs=new VocabIndex[len];</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">         </span>WordIDs[0] = g_srilm_vocab->getIndex((char*)(strCurrWord.c_str()), g_srilm_vocab->unkIndex());</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">     
        </span>for(size_t i=0;i<vstrHistory.size();i++)</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">                      </span>WordIDs[i+1] = vstrHistory[i]!="<s>"?</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">                            </span>g_srilm_vocab->getIndex((char*)(vstrHistory[i].c_str()), g_srilm_vocab->unkIndex()):Vocab_None;</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">            </span>WordIDs[vstrHistory.size()+1] = Vocab_None;</div><div style="font-size: small;"><br></div><div style="font-size: small;"><br></div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">            </span>for(size_t k=0; k<len;k++)</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">            </span>{</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">                        </span>DEBUG("k=%d
 wordindex:%d wordstring:%s",k,WordIDs[k],g_srilm_vocab->getWord(WordIDs[k]));</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">                </span>fResult=g_model->wordProb(WordIDs[0], &WordIDs[1]);</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">               </span>if(fResult==LogP_Zero)fResult=0.0;</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">               </span>DEBUG("prob=%f",fResult);<span class="Apple-tab-span" style="white-space:pre">         </span></div><div style="font-size: small;"><br></div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">            </span>//g_model->sentenceProb(words, stats);</div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">                </span>delete[] WordIDs;</div><div
 style="font-size: small;"><br></div><div style="font-size: small;"><span class="Apple-tab-span" style="white-space:pre">            </span>return fResult;</div><div style="font-size: small;"><br></div><div style="font-size: small;">}</div><div style="font-size: 13px;"><br></div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><br></div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;">//in the  main function</div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><br></div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times,
 serif; background-color: transparent; font-style: normal;"><br></div><div style="background-color: transparent;"><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space: pre;">      </span>//--------------------------------</span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">      </span>// Load LM</span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">      </span>//--------------------------------</span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">      </span>g_srilm_vocab= new Vocab;</span></div><div style="background-color: transparent;"><span style="font-size:
 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre"> </span>g_model=new Ngram(*g_srilm_vocab,nOrder);</span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">       </span>File file(strLangModelFile.c_str(), "r");</span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">     </span>if(!file)</span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">       </span>{</span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">               </span>ERROR("Could not open file %s",strLangModelFile.c_str());</span></div><div style="background-color: transparent;"><span
 style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">  </span>}</span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">       </span>g_model->read(file, 0);<span class="Apple-tab-span" style="white-space:pre">  </span></span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">        </span>for(size_t i=0;i<nOrder;i++)</span></div><div style="background-color: transparent;"><span style="font-size: 12.727272033691406px;"><span class="Apple-tab-span" style="white-space:pre">         </span>TRACE("Num of ngram in model order %d:%d",i+1,g_model->numNgrams(i+1));</span></div><div style="color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; font-size: 12.727272033691406px; font-style:
 normal;"><br></div></div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;"><br></div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;">Best regards,</div><div style="font-size: 12.727272033691406px; color: rgb(0, 0, 0); font-family: 'times new roman', 'new york', times, serif; background-color: transparent; font-style: normal;">Yasser</div></font></span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 10pt;"><br></div>  <div style="font-family: 'times new roman', 'new york', times, serif; font-size: 10pt;"> <div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"> <div dir="ltr"> <font size="2" face="Arial"> <hr size="1">  <b><span
 style="font-weight:bold;">From:</span></b> Andreas Stolcke <stolcke@icsi.berkeley.edu><br> <b><span style="font-weight: bold;">To:</span></b> Yi Yang <yangyiycc@gmail.com> <br><b><span style="font-weight: bold;">Cc:</span></b> SRILM-User@speech.sri.com <br> <b><span style="font-weight: bold;">Sent:</span></b> Friday, March 8, 2013 10:37 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [SRILM User List] c++ sample for building language model<br> </font> </div> <br>
<div id="yiv1637668101">
  

    
  
  <div>
    <div class="yiv1637668101moz-cite-prefix">On 3/8/2013 8:06 AM, Yi Yang wrote:<br>
    </div>
    <blockquote type="cite">
      <div dir="ltr">Hi Mohsen,
        <div><br>
        </div>
        <div>Hope the following codes can be helpful:</div>
      </div>
    </blockquote>
    you forgot to create the Vocab object.  In your case you could
    create it globally to your code works otherwise:<br>
    <br>
    Vocab vocab;<br>
    <br>
    Andreas<br>
    <br>
    <blockquote type="cite">
      <div dir="ltr">
        <div><br>
        </div>
        <div>
          <div>void SrilmTest::srilm_init(const char* fname, int order)
            {</div>
          <div>  File file(fname, "r", 0);</div>
          <div>  assert(file);</div>
        </div>
      </div>
    </blockquote>
    <br>
    <blockquote type="cite">
      <div dir="ltr">
        <div>
          <div>  ngram = new Ngram(vocab, order);</div>
          <div>  ngram->read(file, false);</div>
          <div>  cerr << "Done\n";</div>
          <div>}</div>
          <div><br>
          </div>
          <div>int SrilmTest::srilm_getvoc(const char* word) {</div>
          <div>  return vocab.getIndex((VocabString)word);</div>
          <div>}</div>
          <div><br>
          </div>
          <div>float SrilmTest::srilm_wordprob(int w, int* context) {</div>
          <div>  return (float)ngram->wordProb(w,
            (VocabIndex*)context);</div>
          <div>
            }</div>
        </div>
      </div>
      <div class="yiv1637668101gmail_extra"><br>
        <br>
        <div class="yiv1637668101gmail_quote">On Thu, Mar 7, 2013 at 3:23 PM, mohsen
          jadidi <span dir="ltr"><<a rel="nofollow" ymailto="mailto:mohsen.jadidi@gmail.com" target="_blank" href="mailto:mohsen.jadidi@gmail.com">mohsen.jadidi@gmail.com</a>></span>
          wrote:<br>
          <blockquote class="yiv1637668101gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
            <div dir="ltr">Hey, 
              <div><br>
              </div>
              <div>I need to use srilm in my c++ code to build ML. all
                the examples and slides in the internet explained it
                using ngram-count command not code. I know should use
                <Ngram.h> <Vocab.h>. can you point me to a
                starting point?</div>
              <div><br>
              </div>
              <div>cheers,</div>
              <span class="yiv1637668101HOEnZb"><font color="#888888">
                  <div>
                    <div><br>
                    </div>
                    <div><br>
                    </div>
                    -- <br>
                    Mohsen<br>
                  </div>
                </font></span></div>
            <br>
            _______________________________________________<br>
            SRILM-User site list<br>
            <a rel="nofollow" ymailto="mailto:SRILM-User@speech.sri.com" target="_blank" href="mailto:SRILM-User@speech.sri.com">SRILM-User@speech.sri.com</a><br>
            http://www.speech.sri.com/mailman/listinfo/srilm-user<br>
          </blockquote>
        </div>
        <br>
        <br clear="all">
        <div><br>
        </div>
        -- <br>
        Sincerely,<br>
        Yi Yang<br>
        http://www.cc.gatech.edu/%7Eyyang319/<br>
      </div>
      <br>
      <fieldset class="yiv1637668101mimeAttachmentHeader"></fieldset>
      <br>
      <pre>_______________________________________________
SRILM-User site list
<a rel="nofollow" class="yiv1637668101moz-txt-link-abbreviated" ymailto="mailto:SRILM-User@speech.sri.com" target="_blank" href="mailto:SRILM-User@speech.sri.com">SRILM-User@speech.sri.com</a>
<a rel="nofollow" class="yiv1637668101moz-txt-link-freetext" target="_blank" href="http://www.speech.sri.com/mailman/listinfo/srilm-user">http://www.speech.sri.com/mailman/listinfo/srilm-user</a></pre>
    </blockquote>
    <br>
  </div>

</div><br>_______________________________________________<br>SRILM-User site list<br><a ymailto="mailto:SRILM-User@speech.sri.com" href="mailto:SRILM-User@speech.sri.com">SRILM-User@speech.sri.com</a><br><a href="http://www.speech.sri.com/mailman/listinfo/srilm-user" target="_blank">http://www.speech.sri.com/mailman/listinfo/srilm-user</a><br><br> </div> </div>  </div></body></html>