Package nltk :: Package wordnet :: Module browse
[hide private]
[frames] | no frames]

Source Code for Module nltk.wordnet.browse

  1  # Natural Language Toolkit: Wordnet Interface: Wordnet Text Mode Browser 
  2  # 
  3  # Copyright (C) 2001-2008 NLTK Project 
  4  # Author: Steven Bird <[email protected]> 
  5  #         Jussi Salmela <[email protected]> (modifications) 
  6  # URL: <http://nltk.org> 
  7  # For license information, see LICENSE.TXT 
  8   
  9  """Natural Language Toolkit: Wordnet Interface: Wordnet Text Mode Browser 
 10  See also the NLTK Wordnet Graphical Browser in nltk_contrib.wordnet 
 11  """ 
 12   
 13  from textwrap import TextWrapper 
 14  from random import randint 
 15   
 16  from util import * 
 17  from dictionary import * 
 18   
 19  tw = TextWrapper(subsequent_indent="    ") 
 20   
21 -def show(synsets, index):
22 return "%d %s;" % (index, synsets[index][0])
23 26 30 35 50
51 -def new_word(word):
52 D = None 53 for pos,sec in ((N,"N"), (V,"V"), (ADJ,"J"), (ADV,"R")): 54 if word in pos: 55 if not D: D = pos 56 print sec, 57 print_all(pos[word]) 58 if D: synsets = D[word] 59 else: 60 print "Word '%s' not found! Choosing a random word." % word 61 D = N 62 synsets = random_synset(D) 63 print "N", 64 print_all(N[synsets[0][0]]) 65 return D, synsets
66
67 -def random_synset(D):
68 return D[randint(0,len(D)-1)]
69 70
71 -def browse(word=" ", index=0):
72 """ 73 Browse WordNet interactively, starting from the specified word, and 74 navigating the WordNet hierarchy to synonyms, hypernyms, hyponyms, and so on. 75 76 @type word: C{string} 77 @param word: the word to look up in WordNet 78 @type index: C{int} 79 @param index: the sense number of this word to use (optional) 80 """ 81 print "Wordnet browser (type 'h' for help)" 82 D, synsets = new_word(word) 83 84 while True: 85 if index >= len(synsets): 86 index = 0 87 input = '' 88 while input == '': 89 if synsets: 90 prompt = "%s_%d/%d>" % (synsets[index][0], index, len(synsets)) 91 input = raw_input(prompt) 92 else: 93 input = raw_input("> ") # safety net 94 95 # word lookup 96 if len(input) > 1 and not input.isdigit(): 97 if input[0] == "*": 98 word = input[1:] 99 else: 100 word = input.lower() 101 D, synsets = new_word(word) 102 index = 0 103 104 # sense selection 105 elif input.isdigit(): 106 if int(input) < len(synsets): 107 index = int(input) 108 print_gloss(synsets, index) 109 else: 110 print "There are %d synsets" % len(synsets) 111 112 # more info 113 elif input == "a": 114 print_all(synsets) 115 elif input == "g": 116 print_gloss(synsets, index) 117 elif input == "v": 118 print_all_glosses(synsets) 119 elif input == "s": 120 print "Synonyms:", ' '.join(word for word in synsets[index]) 121 122 # choose part-of-speech 123 elif input in "NVJR": 124 ind = "NVJR".index(input) 125 pos = [N, V, ADJ, ADV][ind] 126 s = ["noun", "verb", "adjective", "adverb"][ind] 127 if word in pos: 128 D = pos 129 synsets = D[word] 130 else: 131 print "No " + s + " sense found" 132 133 # navigation 134 elif input == "r": 135 synsets = random_synset(D) 136 elif input == "u": 137 try: 138 hypernyms = synsets[index][HYPERNYM] 139 hypernyms[0] 140 synsets = hypernyms 141 print_all(synsets) 142 index = 0 143 except IndexError: 144 print "Cannot go up" 145 elif input == "d": 146 try: 147 hyponyms = synsets[index][HYPONYM] 148 hyponyms[0] 149 synsets = hyponyms 150 print_all(synsets) 151 index = 0 152 except IndexError: 153 print "Cannot go down" 154 155 # miscellany 156 elif input == "h" or input == "?": 157 print_help() 158 elif input == "q": 159 print "Goodbye" 160 break 161 162 else: 163 print "Unrecognised command: %s" % input 164 print "Type 'h' for help"
165
166 -def demo():
167 print_help() 168 print 169 browse()
170 171 if __name__ == '__main__': 172 demo() 173 174 __all__ = ["demo"] 175