importing greclusters into git
[greclusters.git] / dict.py
blob626ffd62c11cd5f0b20e4d2694caef7f8dd6e3a8
1 import re
2 from nltk.wordnet import N,V,ADJ,ADV
4 from cache import cached
6 parts = [N, V, ADJ, ADV]
8 @cached
9 def find_definition(word):
10 # TODO: do fuzzy matching for misspelled words and its variations
11 s = ""
12 for part in parts:
13 if word in part:
14 s += "[%s]\n" % part.pos
15 for sense in part[word]:
16 s += sense.gloss + "\n"
17 s += "\n"
18 return s
20 pat_quoted = re.compile('("[^"]*")')
21 @cached
22 def find_definition_html(word):
23 """This is much like `find_definition_html' except the returned
24 string is formatted as pretty HTML
25 """
26 s = ""
27 for part in parts:
28 if word in part:
29 s += "<p><b>[%s]</b><ol>" % part.pos
30 for sense in part[word]:
31 s += "<li>" + pat_quoted.sub(r'<i>\1</i>', sense.gloss)
32 s += "</li>"
33 s += "</ol></p>"
34 return s
36 if __name__ == '__main__':
37 print "dog:\n", find_definition("dog")
38 print "mitigate:\n", find_definition("mitigate")