title: Wordnet synsets using perl
body: <p>I installed <a href="http://search.cpan.org/~tpederse/WordNet-Similarity-2.05/lib/WordNet/Similarity.pm" rel="nofollow">Wordnet::Similarity</a> and <a href="http://search.cpan.org/dist/WordNet-QueryData/QueryData.pm" rel="nofollow">Wordnet::QueryData</a> as an easy way to calculate information content score and probability that comes with these modules. But I'm stuck at this basic problem: given a word, print n words similar to it - which should not be difficult that iterating through the synsets and doing <code>join</code>.</p>
  
  <p>using the <code>wn</code> command and piping it with a whole lot of <code>tr</code>, <code>sort | uniq</code> I can get all the words:</p>
  
  <pre><code> wn cat -synsn | grep -v Sense | tr '=' ' ' | tr '&gt;' ' ' | tr '\t' ' ' | tr ',' '\n' | sort | uniq
  </code></pre>
  
  <p><strong>OUTPUT</strong></p>
  
  <pre><code>8 senses of cat                                                         
  adult female
  adult male
  African tea
  Arabian tea
  big cat
  bozo
  cat
  cat
  CAT
  Caterpillar
  cat-o'-nine-tails
   computed axial tomography
  computed tomography
  computerized axial tomography
  computerized tomography
  CT
  excitant
  felid
        feline
        gossip
  gossiper
  gossipmonger
  guy
  hombre
  kat
  khat
        man
  newsmonger
  qat
  quat
  rumormonger
  rumourmonger
        stimulant
  stimulant drug
  Synonyms/Hypernyms (Ordered by Estimated Frequency) of noun cat
        tracked vehicle
  true cat
        whip
        woman
  X-radiation
        X-raying
  </code></pre>
  
  <p>but its kinda nasty,and needs further clean up. </p>
  
  <p>What my script looks like is below, and what I want to get is all the words in cat#n1...8. </p>
  
  <p><strong>SCRIPT</strong></p>
  
  <pre><code>use WordNet::QueryData;
  
  my $wn = WordNet::QueryData-&gt;new( noload =&gt; 1);
  
  print "Senses: ", join(", ", $wn-&gt;querySense("cat#n")), "\n";
  print "Synset: ", join(", ", $wn-&gt;querySense("cat", "syns")), "\n";
  print "Hyponyms: ", join(", ", $wn-&gt;querySense("cat#n#1", "hypo")), "\n";
  </code></pre>
  
  <p><strong>OUTPUT:</strong> </p>
  
  <pre><code>Senses: cat#n#1, cat#n#2, cat#n#3, cat#n#4, cat#n#5, cat#n#6, cat#n#7, cat#n#8
  Synset: cat#n, cat#v
  Hyponyms: domestic_cat#n#1, wildcat#n#3
  </code></pre>
  
  <hr>
  
  <p><strong>SCRIPT</strong></p>
  
  <pre><code>use WordNet::QueryData;
  my $wn = WordNet::QueryData-&gt;new;
  
  foreach $word (qw/cat#n/) {
  
      @senses = $wn-&gt;querySense($word);
  
      foreach $wps (@senses) {
              @gloss = $wn -&gt; querySense($wps, "syns");
              print "$wps : @gloss\n";
      }
  
  }
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>cat#n#1 : cat#n#1 true_cat#n#1
  cat#n#2 : guy#n#1 cat#n#2 hombre#n#1 bozo#n#2
  cat#n#3 : cat#n#3
  cat#n#4 : kat#n#1 khat#n#1 qat#n#1 quat#n#1 cat#n#4 Arabian_tea#n#1 African_tea#n#1
  cat#n#5 : cat-o'-nine-tails#n#1 cat#n#5
  cat#n#6 : Caterpillar#n#2 cat#n#6
  cat#n#7 : big_cat#n#1 cat#n#7
  cat#n#8 : computerized_tomography#n#1 computed_tomography#n#1 CT#n#2 computerized_axial_tomography#n#1 computed_axial_tomography#n#1 CAT#n#8
  </code></pre>
  
  <hr>
  
  <p>P.S.
  I have never written perl before, but have been looking into perl scripts since morning - and can now understand the basic stuff. Just need to know if there is cleaner way to do this using the api docs - couldn't figure out from the api or usergroup archives.</p>
  
  <p>Update:</p>
  
  <p>I think I'll settle with:</p>
  
  <pre><code> wn cat -synsn | sed '1,6d' |sed 's/Sense [[:digit:]]//g' | sed 's/[[:space:]]*=&gt; //' | sed '/^$/d'
  </code></pre>
  
  <p>sed rocks!</p>