def reconstructTree(self,treeString): wordLst = TreeSentenceFunctions.splitPhrases(treeString) for phrase in wordLst: if TreeSentenceFunctions.isLeafLevel(phrase): # base case decomposedTuple = TreeSentenceFunctions.decomposePhrase(phrase) tag = decomposedTuple[0] word = decomposedTuple[1] #if word[-1] in TreeNode.punctuations: #word = word[:-1] childNode = TreeNode(tag,word,len(self.children),(self.vertical + 1)) self.children.append(childNode) else: topLevelTag = TreeSentenceFunctions.getTopLevelTag(phrase) childNode = TreeNode(topLevelTag, None, len(self.children), (self.vertical + 1)) self.children.append(childNode) childNode.reconstructTree(TreeSentenceFunctions.stripOneLevel(phrase))
def treeStringToTree(treeString): topLevelTag = TreeSentenceFunctions.getTopLevelTag(treeString) root = TreeNode(tag=topLevelTag,hori=0,vert=0) root.reconstructTree(TreeSentenceFunctions.stripOneLevel(treeString)) return root
while treeString != '': treeRoot = TreeNode.TreeNode.treeStringToTree(treeString) # treeStringToTree is a @staticmethod #storeIntoDatabase(treeRoot,sentenceSequence,mysqlCursor) sentenceBody = treeRoot.toSentence() # sql commands: sentence_tbl mysqlCursor.execute("INSERT INTO sentence_tbl(chapter_id,sentence_body,sentence_sequence) VALUES(%s,%s,%s)", (chapterID,sentenceBody,sentenceSequence)) mysqlCursor.execute("SELECT sentence_id FROM sentence_tbl WHERE sentence_body = %s", sentenceBody) sentenceID = int(mysqlCursor.fetchone()[0]) mysqlConnection.commit() wordAndTag = treeRoot.toTagWordTuple() # a list of (tag word) tuples, ignoring non leaf(word) level nodes # sql commands for word_tbl and word_POS_tbl for tagWordTuple in TreeSentenceFunctions.splitPhrases(wordAndTag): decomposedTuple = TreeSentenceFunctions.decomposePhrase(tagWordTuple) tag = decomposedTuple[0] word = decomposedTuple[1] # take out the punctuation(s) in the word if there's any word = cleanUpWord(word) if word != '': # sql commands: word_tbl mysqlCursor.execute("SELECT word_id from word_tbl where word_name = %s", word) wordID = mysqlCursor.fetchone() if not wordID: # ensures that entries in word_tbl are unique mysqlCursor.execute("INSERT INTO word_tbl(word_name) VALUES(%s)", word) mysqlCursor.execute("SELECT word_id FROM word_tbl WHERE word_name = %s", word) wordID = mysqlCursor.fetchone()