<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Random Hacks: Visualizing WordNet relationships as graphs</title>
    <link>http://www.randomhacks.net/articles/2009/12/29/visualizing-wordnet-relationships-as-graphs</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Technology and Other Fun Stuff</description>
    <item>
      <title>"Visualizing WordNet relationships as graphs" by Eric</title>
      <description>&lt;p&gt;Google finds &lt;a href="http://www.cs.utah.edu/~hal/HWordNet/index.html"&gt;HWordNet&lt;/a&gt;, which looks pretty reasonable. For maximum enjoyment, you&amp;#8217;ll also want some kind of &lt;span class="caps"&gt;DAG&lt;/span&gt; or graph library, and visualization tools.&lt;/p&gt;


	&lt;p&gt;I have to say, I&amp;#8217;m delighted about WordNet, because it is both terrifyingly comprehensive and remarkably robust&amp;#8212;you can actually get away with writing software that reasons robustly over WordNet relationships. I find this remarkable, and may post some examples soon.&lt;/p&gt;</description>
      <pubDate>Fri, 01 Jan 2010 18:06:37 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:25ccc178-dc03-4bb1-8a8b-acefe5f937c4</guid>
      <link>http://www.randomhacks.net/articles/2009/12/29/visualizing-wordnet-relationships-as-graphs#comment-742</link>
    </item>
    <item>
      <title>"Visualizing WordNet relationships as graphs" by Matthew</title>
      <description>&lt;p&gt;Do you know of any similar modules in Haskell that will let me play around with this sort of thing?&lt;/p&gt;</description>
      <pubDate>Fri, 01 Jan 2010 10:07:36 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:4883b4e9-3603-4385-8149-27810a38306b</guid>
      <link>http://www.randomhacks.net/articles/2009/12/29/visualizing-wordnet-relationships-as-graphs#comment-741</link>
    </item>
    <item>
      <title>Visualizing WordNet relationships as graphs</title>
      <description>&lt;p&gt;The &lt;a href="http://wordnet.princeton.edu/"&gt;WordNet&lt;/a&gt; database contains all sorts of interesting relationships between words: it can categorize words into hierarchies, find the parts of an object, and answer many other interesting questions.&lt;/p&gt;

&lt;p&gt;The code below relies on the &lt;a href="http://www.nltk.org/"&gt;NLTK&lt;/a&gt; and &lt;a href="http://networkx.lanl.gov/"&gt;NetworkX&lt;/a&gt; libraries for Python.&lt;/p&gt;

&lt;h3&gt;Categorizing words&lt;/h3&gt;

&lt;p&gt;What, exactly, is a dog? It&amp;#8217;s a domestic animal and a carnivore, not to mention a physical entity (as opposed to an abstract entity, such as an idea). WordNet knows all these facts:&lt;/p&gt;

&lt;p&gt;&lt;a href="/files/dog.png"&gt;&lt;img src="/files/dog.png" width="406" height="306" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How do we generate this image? First, we look up the first entry for &amp;#8220;dog&amp;#8221; in WordNet. This returns a &amp;#8220;synset&amp;#8221;, or a set of words with equivalent meanings.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_python "&gt;dog = wn.synset('dog.n.01')&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next, we compute the &lt;a href="http://en.wikipedia.org/wiki/Transitive_closure"&gt;transitive closure&lt;/a&gt; of the &lt;a href="http://en.wikipedia.org/wiki/Hyponymy"&gt;hypernym&lt;/a&gt; relationship, or (in English) we look for all the categories to which &amp;#8220;dog&amp;#8221; belongs, and all the categories to which &lt;em&gt;those&lt;/em&gt; categories belong, recursively:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_python "&gt;graph = closure_graph(dog,
                      lambda s: s.hypernyms())&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After that, we just pass the resulting graph to &lt;a href="http://networkx.lanl.gov/"&gt;NetworkX&lt;/a&gt; for display:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_python "&gt;nx.draw_graphviz(graph)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;The implementation&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;closure_graph&lt;/code&gt; function repeatedly calls &lt;code&gt;fn&lt;/code&gt; on the supplied symset, and uses the result to build a &lt;a href="http://networkx.lanl.gov/"&gt;NetworkX&lt;/a&gt; graph. This code goes at the top of the file, so you can use &lt;code&gt;wn&lt;/code&gt; and &lt;code&gt;nx&lt;/code&gt; in your own code.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_python "&gt;from nltk.corpus import wordnet as wn
import networkx as nx

def closure_graph(synset, fn):
    seen = set()
    graph = nx.DiGraph()

    def recurse(s):
        if not s in seen:
            seen.add(s)
            graph.add_node(s.name)
            for s1 in fn(s):
                graph.add_node(s1.name)
                graph.add_edge(s.name, s1.name)
                recurse(s1)

    recurse(synset)
    return graph&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By using a high-quality graph library, we make it much easier to merge, analyze and display our graphs.&lt;/p&gt;

&lt;h3&gt;More graphs&lt;/h3&gt;

&lt;p&gt;Parts of the finger, generated with &lt;code&gt;synset('finger.n.01')&lt;/code&gt; and &lt;code&gt;part_meronyms&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="/files/wn_finger.png"&gt;&lt;img src="/files/wn_finger.png" width="406" height="306" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Types of running, generated with &lt;code&gt;synset('run.v.01')&lt;/code&gt; and &lt;code&gt;hyponyms&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="/files/wn_run.png"&gt;&lt;img src="/files/wn_run.png" width="406" height="306" /&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 29 Dec 2009 20:38:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bf20469d-bdce-4636-a7f1-33579f49b54c</guid>
      <author>Eric Kidd</author>
      <link>http://www.randomhacks.net/articles/2009/12/29/visualizing-wordnet-relationships-as-graphs</link>
      <category>Python</category>
      <category>NLP</category>
      <trackback:ping>http://www.randomhacks.net/articles/trackback/739</trackback:ping>
    </item>
  </channel>
</rss>

