def computeSimilarity(self, synSet1: SynSet, synSet2: SynSet) -> float: pathToRootOfSynSet1 = self.wordNet.findPathToRoot(synSet1) pathToRootOfSynSet2 = self.wordNet.findPathToRoot(synSet2) LCSid = self.wordNet.findLCSid(pathToRootOfSynSet1, pathToRootOfSynSet2) return 1 / (self.informationContents[synSet1.getId()] + self.informationContents[synSet2.getId()] - 2 * self.informationContents[LCSid])
def removeSynSet(self, synSet: SynSet): """ Removes specified SynSet from the SynSet list. PARAMETERS ---------- synSet : SynSet SynSet to be removed """ self.__synSetList.pop(synSet.getId())
def addSynSet(self, synSet: SynSet): """ Adds specified SynSet to the SynSet list. PARAMETERS ---------- synSet : SynSet SynSet to be added """ self.__synSetList[synSet.getId()] = synSet
def findPathToRoot(self, synSet: SynSet) -> list: """ Finds the path to the root node of a SynSets. PARAMETERS ---------- synSet : SynSet SynSet whose root path will be found RETURNS ------- list List of String corresponding to nodes in the path """ pathToRoot = [] while synSet is not None: if synSet.getId() in pathToRoot: break pathToRoot.append(synSet.getId()) synSet = self.percolateUp(synSet) return pathToRoot
def changeSynSetId(self, synSet: SynSet, newId: str): """ Changes ID of a specified SynSet with the specified new ID. PARAMETERS ---------- synSet : SynSet SynSet whose ID will be updated newId : str new ID """ self.__synSetList.pop(synSet.getId()) synSet.setId(newId) self.__synSetList[newId] = synSet
def removeReverseRelation(self, synSet: SynSet, semanticRelation: SemanticRelation): """ Removes the reverse relations from the SynSet. PARAMETERS ---------- synSet : SynSet SynSet to remove the reverse relation semanticRelation : SemanticRelation relation whose reverse will be removed """ otherSynSet = self.getSynSetWithId(semanticRelation.getName()) if otherSynSet is not None and SemanticRelation.reverse( semanticRelation.getRelationType()) is not None: otherRelation = SemanticRelation( synSet.getId(), SemanticRelation.reverse(semanticRelation.getRelationType())) if otherSynSet.containsRelation(otherRelation): otherSynSet.removeRelation(otherRelation)
def addReverseRelation(self, synSet: SynSet, semanticRelation: SemanticRelation): """ Adds the reverse relations to the SynSet. PARAMETERS ---------- synSet : SynSet SynSet to add the reverse relations semanticRelation : SemanticRelation relation whose reverse will be added """ otherSynSet = self.getSynSetWithId(semanticRelation.getName()) if otherSynSet is not None and SemanticRelation.reverse( semanticRelation.getRelationType()) is not None: otherRelation = SemanticRelation( synSet.getId(), SemanticRelation.reverse(semanticRelation.getRelationType())) if not otherSynSet.containsRelation(otherRelation): otherSynSet.addRelation(otherRelation)
def __init__(self, fileName: str = None, exceptionFileName: str = None): """ Constructor that initializes the SynSet list, literal list, reads exception. PARAMETERS ---------- fileName : str Resource to be read for the WordNet. """ self.__exceptionList = {} if fileName is None: fileName = pkg_resources.resource_filename( __name__, 'data/turkish_wordnet.xml') elif exceptionFileName is not None: self.readExceptionFile(exceptionFileName) self.__interlingualList = {} self.__synSetList = OrderedDict() self.__literalList = OrderedDict() root = xml.etree.ElementTree.parse(fileName).getroot() currentSynSet = None for synSetNode in root: for partNode in synSetNode: if partNode.tag == "ID": currentSynSet = SynSet(partNode.text) self.addSynSet(currentSynSet) elif partNode.tag == "DEF": currentSynSet.setDefinition(partNode.text) elif partNode.tag == "EXAMPLE": currentSynSet.setExample(partNode.text) elif partNode.tag == "WIKI": currentSynSet.setWikiPage(partNode.text) elif partNode.tag == "BCS": currentSynSet.setBcs(int(partNode.text)) elif partNode.tag == "POS": if partNode.text == "a": currentSynSet.setPos(Pos.ADJECTIVE) elif partNode.text == "v": currentSynSet.setPos(Pos.VERB) elif partNode.text == "b": currentSynSet.setPos(Pos.ADVERB) elif partNode.text == "n": currentSynSet.setPos(Pos.NOUN) elif partNode.text == "i": currentSynSet.setPos(Pos.INTERJECTION) elif partNode.text == "c": currentSynSet.setPos(Pos.CONJUNCTION) elif partNode.text == "p": currentSynSet.setPos(Pos.PREPOSITION) elif partNode.text == "r": currentSynSet.setPos(Pos.PRONOUN) elif partNode.tag == "SR": if len(partNode) > 0 and partNode[0].tag == "TYPE": typeNode = partNode[0] if len(partNode) > 1 and partNode[1].tag == "TO": toNode = partNode[1] currentSynSet.addRelation( SemanticRelation(partNode.text, typeNode.text, int(toNode.text))) else: currentSynSet.addRelation( SemanticRelation(partNode.text, typeNode.text)) elif partNode.tag == "ILR": if len(partNode) > 0 and partNode[0].tag == "TYPE": typeNode = partNode[0] interlingualId = partNode.text if interlingualId in self.__interlingualList: synSetList = self.__interlingualList[ interlingualId] else: synSetList = [] synSetList.append(currentSynSet) self.__interlingualList[interlingualId] = synSetList currentSynSet.addRelation( InterlingualRelation(interlingualId, typeNode.text)) elif partNode.tag == "SYNONYM": for literalNode in partNode: currentLiteral = None for childNode in literalNode: if childNode.tag == "SENSE": currentLiteral = Literal( literalNode.text, int(childNode.text), currentSynSet.getId()) currentSynSet.addLiteral(currentLiteral) self.addLiteralToLiteralList(currentLiteral) elif childNode.tag == "SR": typeNode = childNode[0] if len(childNode ) > 1 and childNode[1].tag == "TO": toNode = childNode[1] currentLiteral.addRelation( SemanticRelation( childNode.text, typeNode.text, int(toNode.text))) else: currentLiteral.addRelation( SemanticRelation( childNode.text, typeNode.text))