<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Maxme's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 03:49:38 GMT</pubDate>
    <description>DZone Snippets: Maxme's Code Snippets</description>
    <item>
      <title>Spelling correction using the Python Natural Language Toolkit (nltk)</title>
      <link>http://snippets.dzone.com/posts/show/3395</link>
      <description>Google "Did you mean"-like. More here:&lt;br /&gt;&lt;a href="http://www.biais.org/blog/index.php/2007/01/31/25-spelling-correction-using-the-python-natural-language-toolkit-nltk"&gt;http://www.biais.org/blog/index.php/2007/01/31/25-spelling-correction-using-the-python-natural-language-toolkit-nltk&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Outputs:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;birdd - Did you mean "birds" ? (or "bird")&lt;br /&gt;oklaoma - Did you mean "oklahoma" ?&lt;br /&gt;emphasise - Did you mean "emphasize" ? (or "emphasizes", "emphasizing")&lt;br /&gt;bird - This word seems OK&lt;br /&gt;carot - I can't found it in my learned db&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here is the class:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from nltk_lite.stem.porter import Porter&lt;br /&gt;from nltk_lite.corpora import brown&lt;br /&gt;from nltk_lite import tokenize&lt;br /&gt; &lt;br /&gt;import sys&lt;br /&gt;from collections import defaultdict&lt;br /&gt;import operator&lt;br /&gt; &lt;br /&gt;def sortby(nlist ,n, reverse=0):&lt;br /&gt;    nlist.sort(key=operator.itemgetter(n), reverse=reverse)&lt;br /&gt; &lt;br /&gt;class mydict(dict):&lt;br /&gt;    def __missing__(self, key):&lt;br /&gt;        return 0&lt;br /&gt; &lt;br /&gt;class DidYouMean:&lt;br /&gt;    def __init__(self):&lt;br /&gt;        self.stemmer = Porter()&lt;br /&gt; &lt;br /&gt;    def specialhash(self, s):&lt;br /&gt;        s = s.lower()&lt;br /&gt;        s = s.replace("z", "s")&lt;br /&gt;        s = s.replace("h", "")&lt;br /&gt;        for i in [chr(ord("a") + i) for i in range(26)]:&lt;br /&gt;            s = s.replace(i+i, i)&lt;br /&gt;        s = self.stemmer.stem(s)&lt;br /&gt;        return s&lt;br /&gt; &lt;br /&gt;    def test(self, token):&lt;br /&gt;        hashed = self.specialhash(token)&lt;br /&gt;        if hashed in self.learned:&lt;br /&gt;            words = self.learned[hashed].items()&lt;br /&gt;            sortby(words, 1, reverse=1)&lt;br /&gt;            if token in [i[0] for i in words]:&lt;br /&gt;                return 'This word seems OK'&lt;br /&gt;            else:&lt;br /&gt;                if len(words) == 1:&lt;br /&gt;                    return 'Did you mean "%s" ?' % words[0][0]&lt;br /&gt;                else:&lt;br /&gt;                    return 'Did you mean "%s" ? (or %s)' \&lt;br /&gt;                           % (words[0][0], ", ".join(['"'+i[0]+'"' \&lt;br /&gt;                                                      for i in words[1:]]))&lt;br /&gt;        return "I can't found similar word in my learned db"&lt;br /&gt; &lt;br /&gt;    def learn(self, listofsentences=[], n=2000):&lt;br /&gt;        self.learned = defaultdict(mydict)&lt;br /&gt;        if listofsentences == []:&lt;br /&gt;            listofsentences = brown.raw()&lt;br /&gt;        for i, sent in enumerate(listofsentences):&lt;br /&gt;            if i &gt;= n: # Limit to the first nth sentences of the corpus&lt;br /&gt;                break&lt;br /&gt;            for word in sent:&lt;br /&gt;                self.learned[self.specialhash(word)][word.lower()] += 1&lt;br /&gt; &lt;br /&gt;def demo():&lt;br /&gt;    d = DidYouMean()&lt;br /&gt;    d.learn()&lt;br /&gt;    # choice of words to be relevant related to the brown corpus&lt;br /&gt;    for i in "birdd, oklaoma, emphasise, bird, carot".split(", "):&lt;br /&gt;        print i, "-", d.test(i)&lt;br /&gt; &lt;br /&gt;if __name__ == "__main__":&lt;br /&gt;    demo()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 31 Jan 2007 17:51:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3395</guid>
      <author>maxme (Maxime Biais)</author>
    </item>
    <item>
      <title>Visit Python Abstract Syntax Tree</title>
      <link>http://snippets.dzone.com/posts/show/3360</link>
      <description>Simplest AST visitor. More on this blog post :&lt;br /&gt;&lt;a href="http://www.biais.org/blog/index.php/2007/01/10/9-visit-python-abstract-syntax-tree"&gt;http://www.biais.org/blog/index.php/2007/01/10/9-visit-python-abstract-syntax-tree&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import compiler&lt;br /&gt; &lt;br /&gt;class CodePrinter:&lt;br /&gt;    def __init__(self):&lt;br /&gt;        self.src = ''&lt;br /&gt; &lt;br /&gt;    def visitName(self,t):&lt;br /&gt;        self.src += t.name&lt;br /&gt; &lt;br /&gt;    def visitConst(self,t):&lt;br /&gt;        self.src += str(t.value)&lt;br /&gt; &lt;br /&gt;    def visitStmt(self, t):&lt;br /&gt;        for i in t:&lt;br /&gt;            a = pretty_print(i)&lt;br /&gt;            self.src += a + "\n"&lt;br /&gt; &lt;br /&gt;    def visitAssName(self, t):&lt;br /&gt;        self.src += t.name + " = "&lt;br /&gt; &lt;br /&gt;def pretty_print(node):&lt;br /&gt;    myvisitor = CodePrinter()&lt;br /&gt;    # compiler.walk return the visitor instance : 2nd arg&lt;br /&gt;    return compiler.walk(node, myvisitor).src&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 24 Jan 2007 13:03:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3360</guid>
      <author>maxme (Maxime Biais)</author>
    </item>
    <item>
      <title>Python profile decorator</title>
      <link>http://snippets.dzone.com/posts/show/3346</link>
      <description>Python profile decorator. More info on this blog post: &lt;a href="http://www.biais.org/blog/index.php/2007/01/20/18-python-profiling-decorator"&gt;http://www.biais.org/blog/index.php/2007/01/20/18-python-profiling-decorator&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Maxime Biais &lt;http://www.biais.org/blog&gt;&lt;br /&gt;&lt;br /&gt;import hotshot, hotshot.stats&lt;br /&gt; &lt;br /&gt;def profileit(printlines=1):&lt;br /&gt;    def _my(func):&lt;br /&gt;        def _func(*args, **kargs):&lt;br /&gt;            prof = hotshot.Profile("profiling.data")&lt;br /&gt;            res = prof.runcall(func, *args, **kargs)&lt;br /&gt;            prof.close()&lt;br /&gt;            stats = hotshot.stats.load("profiling.data")&lt;br /&gt;            stats.strip_dirs()&lt;br /&gt;            stats.sort_stats('time', 'calls')&lt;br /&gt;            print "&gt;&gt;&gt;---- Begin profiling print"&lt;br /&gt;            stats.print_stats(printlines)&lt;br /&gt;            print "&gt;&gt;&gt;---- End profiling print"&lt;br /&gt;            return res&lt;br /&gt;        return _func&lt;br /&gt;    return _my&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Usage:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;@profileit(20)&lt;br /&gt;def mop():&lt;br /&gt;    a = 0&lt;br /&gt;    for i in range(100):&lt;br /&gt;        a += mip()&lt;br /&gt;    return a&lt;br /&gt;print mop()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 22 Jan 2007 11:03:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3346</guid>
      <author>maxme (Maxime Biais)</author>
    </item>
    <item>
      <title>Run emacs or emacsclient</title>
      <link>http://snippets.dzone.com/posts/show/3343</link>
      <description>Automatically run emacs or emacsclient if emacs is already launched.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;&lt;br /&gt;EMACS="/usr/bin/emacs"&lt;br /&gt;EMACSCLIENT="/usr/bin/emacsclient"&lt;br /&gt;&lt;br /&gt;$EMACSCLIENT $@ 2&gt; /dev/null&lt;br /&gt;if [ $? -ne 0 ]; then&lt;br /&gt;   exec $EMACS $@&lt;br /&gt;fi&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Put this in your ~/.emacs.el :&lt;br /&gt;&lt;code&gt;&lt;br /&gt;;; start emacs server&lt;br /&gt;(server-start)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 21 Jan 2007 23:34:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3343</guid>
      <author>maxme (Maxime Biais)</author>
    </item>
  </channel>
</rss>
