Exemple #1
0
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     if super().satisfies(parseNode):
         data = parseNode.getLayerData(ViewLayerType.TURKISH_WORD)
         parentData = parseNode.getParent().getData().getName()
         return data is not None and "*" not in data and (
             not (data == "0" and parentData == "-NONE-"))
     return False
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     if super().satisfies(parseNode):
         data = parseNode.getLayerData(ViewLayerType.ENGLISH_WORD)
         parentData = parseNode.getParent().getData().getName()
         return parentData == "CD" and re.fullmatch("[0-9,.]+",
                                                    data) is not None
     return False
 def convertToString(self, parseNode: ParseNodeDrawable):
     if parseNode.isLeaf():
         return self.__converter.leafConverter(parseNode)
     else:
         st = ""
         for i in range(parseNode.numberOfChildren()):
             st += self.convertToString(parseNode.getChild(i))
         return st
Exemple #4
0
 def readFromFile(self, fileName: str):
     inputFile = open(fileName, encoding="utf8")
     line = inputFile.readline().strip()
     if "(" in line and ")" in line:
         line = line[line.index("(") + 1:line.rindex(")")].strip()
         self.root = ParseNodeDrawable(None, line, False, 0)
     else:
         self.root = None
     inputFile.close()
Exemple #5
0
 def leafConverter(self, leafNode: ParseNodeDrawable) -> str:
     layerData = leafNode.getLayerData(self.viewLayerType)
     parentLayerData = leafNode.getParent().getLayerData(self.viewLayerType)
     if layerData is not None:
         if "*" in layerData or (layerData == "0"
                                 and parentLayerData == "-NONE-"):
             return ""
         else:
             return " " + layerData.replace("-LRB-", "(").replace("-RRB-", ")").replace("-LSB-", "[").\
                 replace("-RSB-", "]").replace("-LCB-", "{").replace("-RCB-", "}").replace("-lrb-", "(").\
                 replace("-rrb-", ")").replace("-lsb-", "[").replace("-rsb-", "]").replace("-lcb", "{").\
                 replace("-rcb-", "}")
     else:
         return ""
Exemple #6
0
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     if super().satisfies(parseNode):
         layerInfo = parseNode.getLayerInfo()
         for i in range(layerInfo.getNumberOfMeanings()):
             synSetId = layerInfo.getSemanticAt(i)
             if synSetId == self.__id:
                 return True
     return False
Exemple #7
0
 def leafConverter(self, parseNodeDrawable: ParseNodeDrawable) -> str:
     layerInfo = parseNodeDrawable.getLayerInfo()
     rootWords = " "
     for i in range(layerInfo.getNumberOfWords()):
         root = layerInfo.getMorphologicalParseAt(i).getWord().getName()
         if root is not None and len(root) != 0:
             rootWords += " " + root
     return rootWords
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     layerInfo = parseNode.getLayerInfo()
     if super().satisfies(parseNode) and layerInfo is not None and \
         layerInfo.getLayerData(ViewLayerType.SEMANTICS) is not None:
         for i in range(layerInfo.getNumberOfMeanings()):
             synSetId = layerInfo.getSemanticAt(i)
             if self.__wordNet.getSynSetWithId(synSetId) is not None and \
                     self.__wordNet.getSynSetWithId(synSetId).getPos() == Pos.VERB:
                 return True
     return False
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     if super().satisfies(parseNode):
         data = parseNode.getLayerData(self.__secondLanguage)
         return data is not None and data == "*NONE*"
     return False
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     return parseNode.numberOfChildren() > 0 and parseNode.getData().isVP()
Exemple #11
0
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     if super().satisfies(parseNode):
         parentData = parseNode.getParent().getData().getName()
         return parentData == "NNP" or parentData == "NNPS"
     return False
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     if parseNode.numberOfChildren() > 0:
         return parseNode.getData().__str__() == self.__symbol
     else:
         return False
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     if super().satisfies(parseNode):
         data = parseNode.getLayerData(ViewLayerType.ENGLISH_WORD)
         parentData = parseNode.getParent().getData().getName()
         return "*" in data or (data == "0" and parentData == "-NONE-")
     return False
 def modifier(self, parseNode: ParseNodeDrawable):
     if parseNode.isLeaf():
         name = parseNode.getData().getName()
         parseNode.clearLayers()
         parseNode.getLayerInfo().setLayerData(ViewLayerType.ENGLISH_WORD, name)
         parseNode.clearData()
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     layerInfo = parseNode.getLayerInfo()
     return super().satisfies(parseNode) and layerInfo is not None and \
            layerInfo.getLayerData(ViewLayerType.PROPBANK) is not None and \
            layerInfo.getLayerData(ViewLayerType.PROPBANK) == "PREDICATE"
 def nodeModify(self, parseNode: ParseNodeDrawable):
     self.__nodeModifier.modifier(parseNode)
     for i in range(parseNode.numberOfChildren()):
         self.nodeModify(parseNode.getChild(i))
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     return parseNode.numberOfChildren() == 0
 def collectNodes(self, parseNode: ParseNodeDrawable, collected: list):
     if self.__condition is None or self.__condition.satisfies(parseNode):
         collected.append(parseNode)
     for i in range(parseNode.numberOfChildren()):
         self.collectNodes(parseNode.getChild(i), collected)
Exemple #19
0
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     layerInfo = parseNode.getLayerInfo()
     return super().satisfies(parseNode) and layerInfo is not None and layerInfo.getArgument() is not None \
            and layerInfo.getArgument().getArgumentType() == "PREDICATE"
Exemple #20
0
 def satisfies(self, parseNode: ParseNodeDrawable) -> bool:
     if super().satisfies(parseNode):
         data = parseNode.getLayerData(ViewLayerType.ENGLISH_WORD)
         return Word.isPunctuationSymbol(data) and data != "$"
     return False
Exemple #21
0
class ParseTreeDrawable(ParseTree):

    __fileDescription: FileDescription
    __name: str

    def __init__(self, fileDescription, path: str = None):
        if path is None:
            if isinstance(fileDescription, FileDescription):
                self.__fileDescription = fileDescription
                self.readFromFile(
                    self.__fileDescription.getFileName(
                        fileDescription.getPath()))
            elif isinstance(fileDescription, str):
                self.readFromFile(fileDescription)
        else:
            self.__fileDescription = FileDescription(
                path, fileDescription.getExtension(),
                fileDescription.getIndex())
            self.readFromFile(
                self.__fileDescription.getFileName(fileDescription.getPath()))

    def setFileDescription(self, fileDescription: FileDescription):
        self.__fileDescription = fileDescription

    def getFileDescription(self) -> FileDescription:
        return self.__fileDescription

    def reload(self):
        self.readFromFile(
            self.__fileDescription.getFileName(
                self.__fileDescription.getPath()))

    def readFromFile(self, fileName: str):
        inputFile = open(fileName, encoding="utf8")
        line = inputFile.readline().strip()
        if "(" in line and ")" in line:
            line = line[line.index("(") + 1:line.rindex(")")].strip()
            self.root = ParseNodeDrawable(None, line, False, 0)
        else:
            self.root = None
        inputFile.close()

    def setName(self, name: str):
        self.__name = name

    def getName(self) -> str:
        return self.__name

    def nextTree(self, count: int):
        if self.__fileDescription.nextFileExists(count):
            self.__fileDescription.addToIndex(count)
            self.reload()

    def previousTree(self, count: int):
        if self.__fileDescription.previousFileExists(count):
            self.__fileDescription.addToIndex(-count)
            self.reload()

    def save(self):
        outputFile = open(self.__fileDescription.getFileName(),
                          mode='w',
                          encoding="utf8")
        outputFile.write("( " + self.__str__() + " )\n")
        outputFile.close()

    def saveWithPath(self, newPath: str):
        outputFile = open(self.__fileDescription.getFileName(newPath),
                          mode='w',
                          encoding="utf8")
        outputFile.write("( " + self.__str__() + " )\n")
        outputFile.close()

    def maxDepth(self) -> int:
        if isinstance(self.root, ParseNodeDrawable):
            return self.root.maxDepth()

    def moveLeft(self, node: ParseNode):
        if self.root != node:
            self.root.moveLeft(node)

    def moveRight(self, node: ParseNode):
        if self.root != node:
            self.root.moveRight(node)

    def layerExists(self, viewLayerType: ViewLayerType) -> bool:
        if self.root is not None and isinstance(self.root, ParseNodeDrawable):
            return self.root.layerExists(viewLayerType)
        else:
            return False

    def layerAll(self, viewLayerType: ViewLayerType) -> bool:
        if self.root is not None and isinstance(self.root, ParseNodeDrawable):
            return self.root.layerAll(viewLayerType)
        else:
            return False

    def clearLayer(self, viewLayerType: ViewLayerType):
        if self.root is not None and isinstance(self.root, ParseNodeDrawable):
            self.root.clearLayer(viewLayerType)

    def generateAnnotatedSentence(self,
                                  language: str = None) -> AnnotatedSentence:
        sentence = AnnotatedSentence()
        if language is None:
            nodeDrawableCollector = NodeDrawableCollector(
                self.root, IsTurkishLeafNode())
            leafList = nodeDrawableCollector.collect()
            for parseNode in leafList:
                if isinstance(parseNode, ParseNodeDrawable):
                    layers = parseNode.getLayerInfo()
                    for i in range(layers.getNumberOfWords()):
                        sentence.addWord(layers.toAnnotatedWord(i))
        else:
            nodeDrawableCollector = NodeDrawableCollector(
                self.root, IsEnglishLeafNode())
            leafList = nodeDrawableCollector.collect()
            for parseNode in leafList:
                if isinstance(parseNode, ParseNodeDrawable):
                    newWord = AnnotatedWord(
                        "{" + language + "=" + parseNode.getData().getName() +
                        "}{posTag=" +
                        parseNode.getParent().getData().getName() + "}")
                    sentence.addWord(newWord)
        return sentence

    def extractNodesWithVerbs(self, wordNet: WordNet) -> list:
        nodeDrawableCollector = NodeDrawableCollector(self.root,
                                                      IsVerbNode(wordNet))
        return nodeDrawableCollector.collect()

    def extractNodesWithPredicateVerbs(self, wordNet: WordNet) -> list:
        nodeDrawableCollector = NodeDrawableCollector(
            self.root, IsPredicateVerbNode(wordNet))
        return nodeDrawableCollector.collect()