<div><br></div><div><div class="gmail_quote">On Mon, Aug 2, 2010 at 10:46 AM, Majdi Sawalha <span dir="ltr"><<a href="mailto:maj_sawalha@yahoo.com">maj_sawalha@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div style="font-family:arial, helvetica, sans-serif;font-size:14pt"><div></div>
<div>Dear List members,</div>
<div> </div>
<div>I am developing a morphological analyzer that depends on a large lexicon. i construct a Lexicon class that reades a text file and construct a dictionary of the lexicon entries. </div>
<div>the other class will use the lexicon class to chech if the word is found in the lexicon. the problem that this takes long time as each time an object of that class created, then it needs to call the lexicon many times. then when the lexicon is called it re-construct the lexicon again. is there any way to construct the lexicon one time during the execution of the program? and then the other modules will search the already constructed lexicon.</div>

<div> </div>
<div>best regards</div>
<div>Majdi<br> </div><br>Faculty of Engineering<br>School of Computing<br>University of Leeds<br>Leeds, LS2 9JT<br>UK<br><a href="http://www.comp.leeds.ac.uk/sawalha" target="_blank">http://www.comp.leeds.ac.uk/sawalha</a> 
<div></div></div><br>







      </div><br>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
<br></blockquote></div><br></div><div>You want an object with a global lifetime.  I often wish Python had a nice equivalent to static variables in C.<div><br></div><div>Sometimes people suggest doing this with a function or method argument that takes a default value that looks like it would be different each time the function is called, but in reality, it's evaluated once when the function/method is created.</div>
<div><br></div><div>EG:</div><div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><div>$ cat t</div></div></div><div><div><div>#!/usr/local/cpython-2.7/bin/python</div>
</div></div><div><div><div><br></div></div></div><div><div><div>import time</div></div></div><div><div><div><br></div></div></div><div><div><div>def fn(x = time.time()):</div></div></div><div><div><div>        print x</div>
</div></div><div><div><div><br></div></div></div><div><div><div>for i in xrange(5):</div></div></div><div><div><div>        fn()</div></div></div><div><div><div>        time.sleep(1)</div></div></div><div><div><div><br></div>
</div></div><div><div><div>benchbox-dstromberg:~/src/global-lifetime i486-pc-linux-gnu 13955 - above cmd done 2010 Mon Aug 02 07:16 PM</div></div></div><div><div><div><br></div></div></div><div><div><div>$ ./t</div></div>
</div><div><div><div>1280801789.33</div></div></div><div><div><div>1280801789.33</div></div></div><div><div><div>1280801789.33</div></div></div><div><div><div>1280801789.33</div></div></div><div><div><div>1280801789.33</div>
</div></div><div><div><div>benchbox-dstromberg:~/src/global-lifetime i486-pc-linux-gnu 13955 - above cmd done 2010 Mon Aug 02 07:16 PM</div></div></div><div><div><br></div></div></blockquote>IOW, it's printing the same time each iteration, despite having a 10 millisecond precision and a 1 second sleep between each call.<div>
<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"></blockquote><br><div><div>BTW, for storing a truly large lexicon, you might be better off with a trie than a dictionary (AKA hash table), though I'm sure some lexicons aren't huge enough to require it.  One simple way of doing a trie would be to mkdir an empty directory, and then for each ith subdirectory depth beneath that, have it correspond to the ith character of your word in your lexicon.  That's probably not a terribly efficient trie, but I think it may get the point across - you're not storing common prefixes over and over.</div>
</div><div><br></div><div><br></div></div>