示例#1
0
 def get_classes_for_lexkey(self, lexkey, this_wn_version=None):
     if this_wn_version is None:
         #Then it's assumed to be using the proper wnversion for the selected semantic class (wn30 for BLC and wn21 for WND)
         pass
     elif this_wn_version != self.wn_version:
         #We need to map the synset from this_wn_Version to self.wn_version.
         if self.wn_mapper is None:
             from libs.WordNetMapper import WordNetMapper
             self.wn_mapper = WordNetMapper()
         o = lexkey
         lexkey = self.wn_mapper.map_lexkey_to_lexkey(
             lexkey, this_wn_version, self.wn_version)
     ######
     #We get the pos from the lexkey: rock_hopper%1:05:00:
     p = lexkey.find('%') + 1
     pos = self.normalise_pos(lexkey[p])
     this_class = None
     if pos is None:
         print >> sys.stderr, 'Pos %s not recognized' % this_pos
     else:
         if pos in self.map_synset_pos_to_class:
             synset = self.synset_for_lexkey.get(lexkey)
             if synset is not None:
                 this_class = self.map_synset_pos_to_class[pos].get(synset)
     return this_class
示例#2
0
    def get_classes_for_synset_pos(self,
                                   synset,
                                   this_pos,
                                   this_wn_version=None):
        if this_wn_version is None:
            #Then it's assumed to be using the proper wnversion for the selected semantic class (wn30 for BLC and wn21 for WND)
            pass
        elif this_wn_version != self.wn_version:
            #We need to map the synset from this_wn_Version to self.wn_version.
            if self.wn_mapper is None:
                from libs.WordNetMapper import WordNetMapper
                self.wn_mapper = WordNetMapper()
            synset, another_pos = self.wn_mapper.map_offset_to_offset(
                synset, this_wn_version, self.wn_version)
            #print 'Mapped synset wn30', synset

        pos = self.normalise_pos(this_pos)
        if pos is None:
            print >> sys.stderr, 'Pos %s not recognized' % this_pos
            return None
        else:
            if pos in self.map_synset_pos_to_class:
                return self.map_synset_pos_to_class[pos].get(synset)
 def get_classes_for_synset_pos(self,synset,this_pos, this_wn_version=None):
     if this_wn_version is None:
         #Then it's assumed to be using the proper wnversion for the selected semantic class (wn30 for BLC and wn21 for WND)
         pass
     elif this_wn_version != self.wn_version:
         #We need to map the synset from this_wn_Version to self.wn_version.
         if self.wn_mapper is None:
             from libs.WordNetMapper import WordNetMapper
             self.wn_mapper = WordNetMapper()
         synset, another_pos = self.wn_mapper.map_offset_to_offset(synset,this_wn_version,self.wn_version)
             #print 'Mapped synset wn30', synset
          
     pos = self.normalise_pos(this_pos)
     if pos is None:
         print>>sys.stderr,'Pos %s not recognized' % this_pos
         return None
     else:
         if pos in self.map_synset_pos_to_class:
             return self.map_synset_pos_to_class[pos].get(synset)
 def get_classes_for_lexkey(self,lexkey,this_wn_version=None):
     if this_wn_version is None:
         #Then it's assumed to be using the proper wnversion for the selected semantic class (wn30 for BLC and wn21 for WND)
         pass
     elif this_wn_version != self.wn_version:
         #We need to map the synset from this_wn_Version to self.wn_version.
         if self.wn_mapper is None:
             from libs.WordNetMapper import WordNetMapper
             self.wn_mapper = WordNetMapper()
         o = lexkey
         lexkey = self.wn_mapper.map_lexkey_to_lexkey(lexkey,this_wn_version,self.wn_version)
     ######
     #We get the pos from the lexkey: rock_hopper%1:05:00:
     p = lexkey.find('%')+1
     pos = self.normalise_pos(lexkey[p])
     this_class = None
     if pos is None:
         print>>sys.stderr,'Pos %s not recognized' % this_pos
     else:
         if pos in self.map_synset_pos_to_class:
             synset = self.synset_for_lexkey.get(lexkey)
             if synset is not None:
                 this_class = self.map_synset_pos_to_class[pos].get(synset)
     return this_class
class SemanticClassManager(object):
    def __init__(self):
        self.map_synset_pos_to_class = {}
        self.NOUN = 'n'
        self.VERB = 'v'
        self.ADJ = 'a'
        self.ADV = 'r'
        self.resource = 'SemanticClassAbstract'
        self.wn_version = None
        self.wn_mapper = None   #By default we will not use it
        self.synset_for_lexkey = None   # To be read by the subclasses    {lexkey} --> offset        
        self.sense_info_list_for_lemma_pos = {}
        
    def get_resource(self):
        return self.resource
    
    def normalise_pos(self,this_pos):
        pos = None
        if this_pos.lower() in ['noun','n','1']:
            pos = self.NOUN
        elif this_pos.lower() in ['verb','v','2']:
            pos = self.VERB
        elif this_pos.lower() in ['adj','j','3','5','s','a']:
            pos = self.ADJ
        elif this_pos.lower() in ['adv','r','4']:
            pos = self.ADV
        return pos
    
    def sense_info_iterator(self, lemma, pos):
        for sense_info in self.sense_info_list_for_lemma_pos.get((lemma,pos),[]):
            yield sense_info
            
            
    def get_classes_for_synset_pos(self,synset,this_pos, this_wn_version=None):
        if this_wn_version is None:
            #Then it's assumed to be using the proper wnversion for the selected semantic class (wn30 for BLC and wn21 for WND)
            pass
        elif this_wn_version != self.wn_version:
            #We need to map the synset from this_wn_Version to self.wn_version.
            if self.wn_mapper is None:
                from libs.WordNetMapper import WordNetMapper
                self.wn_mapper = WordNetMapper()
            synset, another_pos = self.wn_mapper.map_offset_to_offset(synset,this_wn_version,self.wn_version)
                #print 'Mapped synset wn30', synset
             
        pos = self.normalise_pos(this_pos)
        if pos is None:
            print>>sys.stderr,'Pos %s not recognized' % this_pos
            return None
        else:
            if pos in self.map_synset_pos_to_class:
                return self.map_synset_pos_to_class[pos].get(synset)
            
            
    def get_classes_for_lexkey(self,lexkey,this_wn_version=None):
        if this_wn_version is None:
            #Then it's assumed to be using the proper wnversion for the selected semantic class (wn30 for BLC and wn21 for WND)
            pass
        elif this_wn_version != self.wn_version:
            #We need to map the synset from this_wn_Version to self.wn_version.
            if self.wn_mapper is None:
                from libs.WordNetMapper import WordNetMapper
                self.wn_mapper = WordNetMapper()
            o = lexkey
            lexkey = self.wn_mapper.map_lexkey_to_lexkey(lexkey,this_wn_version,self.wn_version)
        ######
        #We get the pos from the lexkey: rock_hopper%1:05:00:
        p = lexkey.find('%')+1
        pos = self.normalise_pos(lexkey[p])
        this_class = None
        if pos is None:
            print>>sys.stderr,'Pos %s not recognized' % this_pos
        else:
            if pos in self.map_synset_pos_to_class:
                synset = self.synset_for_lexkey.get(lexkey)
                if synset is not None:
                    this_class = self.map_synset_pos_to_class[pos].get(synset)
        return this_class
    
    def get_classes_for_lemma_pos(self,lemma,pos):
        classes = []
        pos = self.normalise_pos(pos)
        for sense_info in self.sense_info_list_for_lemma_pos[(lemma,pos)]:
            these_classes = self.get_classes_for_synset_pos(sense_info.synset, pos)
            if these_classes is not None:
                classes.extend(these_classes)
        # In case we dont use the hierarchy option for WND, the list of classes will be
        # a plain list of strings, with different classes for the different synsets. What
        # we do here is remove the duplicated in these cases. Else, we return the whole object
        if len(classes) != 0:
            if not isinstance(classes[0],list):
                classes = list(set(classes))
        return classes
            
            
    def are_compatible(self,class1,class2):
        pass
        
    
    def get_most_frequent_classes(self,lemma,pos):
        classes = None
        lexkey_for_first_sense = None
        for sense_info in self.sense_info_iterator(lemma, pos):
            if sense_info.sense_number == '1':
                lexkey_for_first_sense = sense_info.lexkey
                break
        if lexkey_for_first_sense is not None:
            classes = self.get_classes_for_lexkey(lexkey_for_first_sense)
        return classes
示例#6
0
class SemanticClassManager(object):
    def __init__(self):
        self.map_synset_pos_to_class = {}
        self.NOUN = 'n'
        self.VERB = 'v'
        self.ADJ = 'a'
        self.ADV = 'r'
        self.resource = 'SemanticClassAbstract'
        self.wn_version = None
        self.wn_mapper = None  #By default we will not use it
        self.synset_for_lexkey = None  # To be read by the subclasses    {lexkey} --> offset
        self.sense_info_list_for_lemma_pos = {}

    def get_resource(self):
        return self.resource

    def normalise_pos(self, this_pos):
        pos = None
        if this_pos.lower() in ['noun', 'n', '1']:
            pos = self.NOUN
        elif this_pos.lower() in ['verb', 'v', '2']:
            pos = self.VERB
        elif this_pos.lower() in ['adj', 'j', '3', '5', 's', 'a']:
            pos = self.ADJ
        elif this_pos.lower() in ['adv', 'r', '4']:
            pos = self.ADV
        return pos

    def sense_info_iterator(self, lemma, pos):
        for sense_info in self.sense_info_list_for_lemma_pos.get((lemma, pos),
                                                                 []):
            yield sense_info

    def get_classes_for_synset_pos(self,
                                   synset,
                                   this_pos,
                                   this_wn_version=None):
        if this_wn_version is None:
            #Then it's assumed to be using the proper wnversion for the selected semantic class (wn30 for BLC and wn21 for WND)
            pass
        elif this_wn_version != self.wn_version:
            #We need to map the synset from this_wn_Version to self.wn_version.
            if self.wn_mapper is None:
                from libs.WordNetMapper import WordNetMapper
                self.wn_mapper = WordNetMapper()
            synset, another_pos = self.wn_mapper.map_offset_to_offset(
                synset, this_wn_version, self.wn_version)
            #print 'Mapped synset wn30', synset

        pos = self.normalise_pos(this_pos)
        if pos is None:
            print >> sys.stderr, 'Pos %s not recognized' % this_pos
            return None
        else:
            if pos in self.map_synset_pos_to_class:
                return self.map_synset_pos_to_class[pos].get(synset)

    def get_classes_for_lexkey(self, lexkey, this_wn_version=None):
        if this_wn_version is None:
            #Then it's assumed to be using the proper wnversion for the selected semantic class (wn30 for BLC and wn21 for WND)
            pass
        elif this_wn_version != self.wn_version:
            #We need to map the synset from this_wn_Version to self.wn_version.
            if self.wn_mapper is None:
                from libs.WordNetMapper import WordNetMapper
                self.wn_mapper = WordNetMapper()
            o = lexkey
            lexkey = self.wn_mapper.map_lexkey_to_lexkey(
                lexkey, this_wn_version, self.wn_version)
        ######
        #We get the pos from the lexkey: rock_hopper%1:05:00:
        p = lexkey.find('%') + 1
        pos = self.normalise_pos(lexkey[p])
        this_class = None
        if pos is None:
            print >> sys.stderr, 'Pos %s not recognized' % this_pos
        else:
            if pos in self.map_synset_pos_to_class:
                synset = self.synset_for_lexkey.get(lexkey)
                if synset is not None:
                    this_class = self.map_synset_pos_to_class[pos].get(synset)
        return this_class

    def get_classes_for_lemma_pos(self, lemma, pos):
        classes = []
        pos = self.normalise_pos(pos)
        for sense_info in self.sense_info_list_for_lemma_pos[(lemma, pos)]:
            these_classes = self.get_classes_for_synset_pos(
                sense_info.synset, pos)
            if these_classes is not None:
                classes.extend(these_classes)
        # In case we dont use the hierarchy option for WND, the list of classes will be
        # a plain list of strings, with different classes for the different synsets. What
        # we do here is remove the duplicated in these cases. Else, we return the whole object
        if len(classes) != 0:
            if not isinstance(classes[0], list):
                classes = list(set(classes))
        return classes

    def are_compatible(self, class1, class2):
        pass

    def get_most_frequent_classes(self, lemma, pos):
        classes = None
        lexkey_for_first_sense = None
        for sense_info in self.sense_info_iterator(lemma, pos):
            if sense_info.sense_number == '1':
                lexkey_for_first_sense = sense_info.lexkey
                break
        if lexkey_for_first_sense is not None:
            classes = self.get_classes_for_lexkey(lexkey_for_first_sense)
        return classes