Пример #1
0
 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])
Пример #2
0
 def __containsSameLiteral(self, synSet1: SynSet, synSet2: SynSet) -> bool:
     for i in range(synSet1.getSynonym().literalSize()):
         literal1 = synSet1.getSynonym().getLiteral(i)
         for j in range(i + 1, synSet2.getSynonym().literalSize()):
             literal2 = synSet2.getSynonym().getLiteral(j)
             if literal1.getName() == literal2.getName() and synSet1.getPos(
             ) is not None:
                 return True
     return False
 def intersection(self, synSet: SynSet, sentence: AnnotatedSentence) -> int:
     if synSet.getExample() is not None:
         words1 = (synSet.getLongDefinition() + " " +
                   synSet.getExample()).split(" ")
     else:
         words1 = synSet.getLongDefinition().split(" ")
     words2 = sentence.toString().split(" ")
     count = 0
     for word1 in words1:
         for word2 in words2:
             if word1.lower() == word2.lower():
                 count = count + 1
     return count
Пример #4
0
    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 intersection(self, synSet: SynSet, leafList: list) -> int:
     if synSet.getExample() is not None:
         words1 = (synSet.getLongDefinition() + " " +
                   synSet.getExample()).split(" ")
     else:
         words1 = synSet.getLongDefinition().split(" ")
     words2 = []
     for i in range(len(leafList)):
         words2.append(leafList[i].getLayerData(ViewLayerType.TURKISH_WORD))
     count = 0
     for word1 in words1:
         for word2 in words2:
             if word1.lower() == word2.lower():
                 count = count + 1
     return count
Пример #6
0
    def removeSynSet(self, synSet: SynSet):
        """
        Removes specified SynSet from the SynSet list.

        PARAMETERS
        ----------
        synSet : SynSet
            SynSet to be removed
        """
        self.__synSetList.pop(synSet.getId())
Пример #7
0
    def addSynSet(self, synSet: SynSet):
        """
        Adds specified SynSet to the SynSet list.

        PARAMETERS
        ----------
        synSet : SynSet
            SynSet to be added
        """
        self.__synSetList[synSet.getId()] = synSet
Пример #8
0
    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
Пример #9
0
    def percolateUp(self, root: SynSet) -> SynSet:
        """
        Finds the parent of a node. It does not move until the root, instead it goes one level up.

        PARAMETERS
        ----------
        root : SynSet
            SynSet whose parent will be find

        RETURNS
        -------
        SynSet
            Parent SynSet
        """
        for i in range(root.relationSize()):
            r = root.getRelation(i)
            if isinstance(r, SemanticRelation):
                if r.getRelationType() == SemanticRelationType.HYPERNYM \
                        or r.getRelationType() == SemanticRelationType.INSTANCE_HYPERNYM:
                    root = self.getSynSetWithId(r.getName())
                    return root
        return None
Пример #10
0
    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)
Пример #11
0
    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)
Пример #12
0
    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))