def trySubstitutions(
         trySubstitutions,  # workaround for lack of nested closures in Python < 2.1
         form,  # reduced form
         substitutions,  # remaining substitutions
         lookup=1,
         dictionary=_dictionaryFor(pos),
         excfile=excfile,
         collect=collect,
         collection=[]):
     import string
     exceptions = binarySearchFile(excfile, form)
     if exceptions:
         form = exceptions[string.find(exceptions, ' ') + 1:-1]
     if lookup and dictionary.has_key(form):
         if collect:
             collection.append(form)
         else:
             return form
     elif substitutions:
         old, new = substitutions[0]
         substitutions = substitutions[1:]
         substitute = None
         if endsWith(form, old):
             substitute = form[:-len(old)] + new
             #if dictionary.has_key(substitute):
             #   return substitute
         form =              trySubstitutions(trySubstitutions, form, substitutions) or \
             (substitute and trySubstitutions(trySubstitutions, substitute, substitutions))
         return (collect and collection) or form
     elif collect:
         return collection
    def entries(self, form, pos_tag=None):
        # inherit docs from DictionaryI
        # find the WordNet part of speech
        poss = [NOUN, VERB, ADJECTIVE, ADVERB]
        if pos_tag:
            pos = self._tag_dict.get(pos_tag)
            if pos:
                poss = [pos]
            else:
                poss = []

        # find all the synsets
        synsets = []
        for pos in poss:
            try:
                word = _dictionaryFor(pos)[form]
                synsets.extend([
                    '%s/%s' % (pos, sense.synset.offset)
                    for sense in word.senses()
                ])
            except KeyError:
                # it's not in WordNet
                pass

        return synsets
示例#3
0
 def trySubstitutions(trySubstitutions,	# workaround for lack of nested closures in Python < 2.1
                      form,		  	# reduced form
                      substitutions,		# remaining substitutions
                      lookup=1,
                      dictionary=_dictionaryFor(pos),
                      excfile=excfile,
                      collect=collect,
                      collection=[]):
     import string
     exceptions = binarySearchFile(excfile, form)
     if exceptions:
         form = exceptions[string.find(exceptions, ' ')+1:-1]
     if lookup and dictionary.has_key(form):
         if collect:
             collection.append(form)
         else:
             return form
     elif substitutions:
         old, new = substitutions[0]
         substitutions = substitutions[1:]
         substitute = None
         if endsWith(form, old):
             substitute = form[:-len(old)] + new
             #if dictionary.has_key(substitute):
             #   return substitute
         form =              trySubstitutions(trySubstitutions, form, substitutions) or \
             (substitute and trySubstitutions(trySubstitutions, substitute, substitutions))
         return (collect and collection) or form
     elif collect:
         return collection
示例#4
0
 def trySubstitutions(trySubstitutions, form, substitutions, lookup=1, dictionary=_dictionaryFor(pos)):
     if lookup and dictionary.has_key(form):
         return dictionary[form]
     elif substitutions:
         (old, new) = substitutions[0]
         substitute = string.replace(form, old, new) #and substitute != form
         if substitute and dictionary.has_key(substitute):
             return dictionary[substitute]
         return              trySubstitutions(trySubstitutions, form, substitutions[1:], lookup=0) or \
             (substitute and trySubstitutions(trySubstitutions, substitute, substitutions[1:]))
 def trySubstitutions(trySubstitutions,
                      form,
                      substitutions,
                      lookup=1,
                      dictionary=_dictionaryFor(pos)):
     if lookup and dictionary.has_key(form):
         return dictionary[form]
     elif substitutions:
         (old, new) = substitutions[0]
         substitute = string.replace(form, old,
                                     new)  #and substitute != form
         if substitute and dictionary.has_key(substitute):
             return dictionary[substitute]
         return              trySubstitutions(trySubstitutions, form, substitutions[1:], lookup=0) or \
             (substitute and trySubstitutions(trySubstitutions, substitute, substitutions[1:]))
    def entries(self, form, pos_tag=None):
        # inherit docs from DictionaryI
        # find the WordNet part of speech
        poss = [NOUN, VERB, ADJECTIVE, ADVERB]
        if pos_tag:
            pos = self._tag_dict.get(pos_tag)
            if pos:
                poss = [pos]
            else:
                poss = []

        # find all the synsets
        synsets = []
        for pos in poss:
            try:
                word = _dictionaryFor(pos)[form]
                synsets.extend(['%s/%s' % (pos, sense.synset.offset)
                                for sense in word.senses()])
            except KeyError:
                # it's not in WordNet
                pass 

        return synsets