1
2
3
4
5
6
7
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):
23
26
30
35
37 print "="*60
38 print "Lookup a word by typing it and finishing with Enter."
39 print "Reserved words -- letters and numbers used as browser commands --"
40 print "can be searched by preceeding them with an asterisk *."
41 print
42 print "Words have numbered senses, pick a sense by typing a number."
43 print
44 print "Commands are a letter followed by Enter:"
45 print " d=down, u=up, g=gloss, s=synonyms, a=all-senses"
46 print " v=verbose, r=random, q=quit"
47 print
48 print "Choose POS with: N=nouns, V=verbs, J=adjectives, R=adverbs"
49 print "="*60
50
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
68 return D[randint(0,len(D)-1)]
69
70
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("> ")
94
95
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
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
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
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
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
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
170
171 if __name__ == '__main__':
172 demo()
173
174 __all__ = ["demo"]
175