Пример #1
0
    def testDelWord(self):
        """ Test deleting a word """
        # This parent I've chosen has only one child
        db = self.getDB()
        num_trees, matched_words = cf.searchDB(db, "varch")
        test_parent = matched_words[0][1]
        num_trees, matched_words = cf.searchDB(db, "ferkel")
        test_word = matched_words[0][1]

        #        # Test deleting a word that's not in the tree?
        #        word_dets = {'lang': 'English', 'text': ['banana', 'pineapple'],
        #                'morpheme': 'bannana', 'def': 'A fruity thing'}
        #        orphan_word = cf.createWord(word_dets)
        #        self.assertRaises(cf.EtymExceptWord, cf.deleteWord, orphan_word)

        # Test deleting a word
        cf.deleteWord(test_word)
        self.assertEqual(cf.loadWordChildren(test_parent), [])
        num_trees, matched_words = cf.searchDB(db, "ferkel")
        self.assertEqual(num_trees, 0)

        # Reload the db, delete the parent and ensure the child moves up
        db = self.getDB()
        num_trees, matched_words = cf.searchDB(db, "varch")
        test_parent = matched_words[0][1]
        num_trees, matched_words = cf.searchDB(db, "ferkel")
        test_word = matched_words[0][1]
        test_grandparent = cf.loadWordParents(test_parent)
        cf.deleteWord(test_parent)
        self.assertEqual(cf.loadWordParents(test_word), test_grandparent)
Пример #2
0
    def testIsDescendant(self):
        """ Test checking IsDescendant() """
        # "farho" is a great grand-parent of "farrow"
        # "porcus" is a sibling of "farho"
        db = self.getDB()
        num_trees, matched_words = cf.searchDB(db, "farho")
        test_top = matched_words[0][1]
        num_trees, matched_words = cf.searchDB(db, "farrow")
        test_bottom = matched_words[0][1]
        num_trees, matched_words = cf.searchDB(db, "porcus")
        test_side = matched_words[0][1]

        self.assertEqual(cf.isDescendant(test_top, test_bottom), True)
        self.assertEqual(cf.isDescendant(test_top, test_side), False)
        self.assertEqual(cf.isDescendant(test_bottom, test_top), False)
        self.assertEqual(cf.isDescendant(test_bottom, test_side), False)
Пример #3
0
 def testFindRoot(self):
     """ Test finding the tree root of a word """
     # Search for biology, since it's in two trees
     db = self.getDB()
     # First get references to the words
     num_trees, matched_words = cf.searchDB(db, "biology")
     test_source = matched_words[0][1]
     test_root = matched_words[0][0]
     self.assertEqual(num_trees, 2)
     # Find the root
     tree_root = cf.findRoot(test_source)
     tree_root_details = cf.loadWordDetails(tree_root)
     # Confirm it's right
     self.assertEqual(tree_root_details["lang"], "PIE")
     self.assertEqual(tree_root_details["morpheme"], "gweie")
     self.assertEqual(test_root, tree_root)
     # Now try the other biology tree
     test_source = matched_words[1][1]
     test_root = matched_words[1][0]
     # Find the root
     tree_root = cf.findRoot(test_source)
     tree_root_details = cf.loadWordDetails(tree_root)
     # Confirm it's right
     self.assertEqual(tree_root_details["lang"], "PIE")
     self.assertEqual(tree_root_details["morpheme"], "leg")
     self.assertEqual(test_root, tree_root)
Пример #4
0
 def getWord(self, search_word):
     """ Helper function to load in a word """
     db = self.getDB()
     num_trees, matched_words = cf.searchDB(db, search_word)
     # Return the first match
     # chosen_root = matched_words[0][0]
     chosen_word = matched_words[0][1]
     return chosen_word
Пример #5
0
def main():
    """ The main routine """
    ###
    # Parse arguments
    parser = argparse.ArgumentParser(description='Run etymdendron')
    parser.add_argument('word', help='Word to search for (will be asked for '
        'if not specified)', nargs='?', default=None)
    args = parser.parse_args()

    ###
    # First let's load the XML
    words_tree = loadDB(WORDS_FILE)
    if type(words_tree) is str:
        print(words_tree) # This holds the error message
        sys.exit(1)

    ###
    # Display a header
    print('=========================')
    print('====   Etymdendron   ====')
    print('=========================')
    print('{0} loaded, {1} trees found'.format(WORDS_FILE,
        len(words_tree.getroot())))

    ###
    # Search for a word
    if args.word is None:
        search_word = cli_funcs.get_search_word()
    else:
        search_word = args.word

    num_trees, matched_words = searchDB(words_tree, search_word)

    ###
    # Display the tree if we have matches
    # No match
    if num_trees == 0:
        print('{0} is not found in {1}'.format(search_word, WORDS_FILE))
        sys.exit(0)
    # Multiple matches
    elif num_trees > 1:
        print('{0} is found in {1} trees'.format(search_word, num_trees))
        chosen_root, chosen_word = cli_funcs.choose_word_from_many(
                matched_words)
    # One match
    elif num_trees == 1:
        print('{0} is found in one tree'.format(search_word))
        # Extract the tree and all matched words separately
        # Just pick the first entry (m_w[...][0] are the same in this case)
        chosen_root = matched_words[0][0]
        chosen_word = [match[1] for match in matched_words]

    # And now display the tree
    cli_funcs.display_tree(chosen_root, chosen_word, search_word)

    ###
    # That's all!
    sys.exit(0)
Пример #6
0
    def testMoveWord(self):
        """ Test moving a word to a different location in the tree """
        db = self.getDB()
        # Here we move ross to be a child of hross
        # First get references to the words
        num_trees, matched_words = cf.searchDB(db, "ross")
        test_source = matched_words[0][1]
        test_source_parent = cf.loadWordParents(test_source)
        num_trees, matched_words = cf.searchDB(db, "hross")
        test_dest_parent = matched_words[0][1]
        self.assertEqual(cf.countWordChildren(test_dest_parent), 0)
        # Perform the move
        cf.moveWord(test_source, test_dest_parent)
        # Make sure it really happened
        self.assertEqual(cf.countWordChildren(test_dest_parent), 1)
        self.assertEqual(cf.countWordChildren(test_source_parent), 0)
        self.assertEqual(cf.loadWordParents(test_source), test_dest_parent)

        # Let's test for some bad inputs
        self.assertRaises(cf.EtymExceptWord, cf.moveWord, None, None)
        self.assertRaises(cf.EtymExceptWord, cf.moveWord, test_source, None)
        self.assertRaises(cf.EtymExceptWord, cf.moveWord, None, test_source)

        # Another test, this time make sure the children are maintained
        # We move fearh (which has a child and a grandchild) to be a child
        # of porcus (which has two children and a few further descendants)
        db = self.getDB()
        # First get references to the words
        num_trees, matched_words = cf.searchDB(db, "fearh")
        test_source = matched_words[0][1]
        test_source_parent = cf.loadWordParents(test_source)
        num_trees, matched_words = cf.searchDB(db, "porcus")
        test_dest_parent = matched_words[0][1]
        self.assertEqual(cf.countWordChildren(test_dest_parent), 2)
        self.assertEqual(cf.countWordChildren(test_source), 1)
        # Perform the move
        cf.moveWord(test_source, test_dest_parent)
        # Make sure it really happened
        self.assertEqual(cf.countWordChildren(test_dest_parent), 3)
        self.assertEqual(cf.countWordChildren(test_source_parent), 2)
        self.assertEqual(cf.loadWordParents(test_source), test_dest_parent)
        self.assertEqual(cf.countWordChildren(test_source), 1)
        num_trees, matched_words = cf.searchDB(db, "far")
        test_dest_child = matched_words[0][1]
        self.assertEqual(cf.loadWordParents(test_dest_child), test_source)
Пример #7
0
 def testCheckNode(self):
     """ Test checking the node type """
     db = self.getDB()
     num_trees, matched_words = cf.searchDB(db, "fearh")
     test_source = matched_words[0][1]
     test_root = matched_words[0][0]
     self.assertEqual(cf.checkNode(test_source), "word")
     self.assertRaises(cf.EtymExceptWord, cf.checkNode, None)
     self.assertEqual(cf.checkNode(test_root), "tree")
Пример #8
0
 def testDelTree(self):
     """ Test deleting a tree """
     # Search for biology, since it's in two trees
     db = self.getDB()
     # First get references to the words
     num_trees, matched_words = cf.searchDB(db, "biology")
     test_source = matched_words[0][1]
     self.assertEqual(num_trees, 2)
     tree_root = cf.findRoot(test_source)
     # Delete a tree
     cf.deleteTree(tree_root)
     # Confirm it happened
     num_trees, matched_words = cf.searchDB(db, "biology")
     test_source = matched_words[0][1]
     self.assertEqual(num_trees, 1)
     tree_root = cf.findRoot(test_source)
     # Try again
     cf.deleteTree(tree_root)
     # Confirm it happened (again)
     num_trees, matched_words = cf.searchDB(db, "biology")
     self.assertEqual(num_trees, 0)
Пример #9
0
 def testDispRos(self):
     """ Tests the CLI display of 'ros' """
     num_trees, matched_words = cf.searchDB(self.db, "ros")
     chosen_root = matched_words[0][0]
     chosen_word = [match[1] for match in matched_words]
     # Redirect stdout to a string
     tree_output = StringIO.StringIO()
     sys.stdout = tree_output
     cli.display_tree(chosen_root, chosen_word, "ros")
     tree_output_string = tree_output.getvalue()
     sys.stdout = sys.__stdout__
     self.maxDiff = None
     self.assertEqual(tree_output_string.strip(), self.dispRos)
Пример #10
0
    def testAddTree(self):
        """ Test adding a tree """
        # Create a test word to attach to the new tree
        word_dets = {
            "lang": "English",
            "text": ["banana", "pineapple"],
            "morpheme": "banana",
            "def": "A fruity thing",
            "tag": "word",
        }
        new_word = cf.createWord(word_dets)
        new_word_details = cf.loadWordDetails(new_word)
        self.assertEqual(word_dets, new_word_details)
        # Create the new tree
        db = self.getDB()
        tree_dets = {"lang": "PIE", "text": ["bane"], "morpheme": "bane", "def": "Some fruit", "tag": "tree"}
        cf.addTree(db, tree_dets, [new_word])
        # Test that it was created
        num_trees, matched_words = cf.searchDB(db, "banana")
        search_word = matched_words[0][1]
        search_root = matched_words[0][0]
        search_root_dets = cf.loadWordDetails(search_root)
        self.assertEqual(search_word, new_word)
        self.assertEqual(search_root_dets["lang"], "PIE")
        self.assertEqual(search_root_dets["def"], "Some fruit")
        self.assertEqual(search_root_dets["morpheme"], "bane")
        self.assertEqual(search_root_dets["text"], ["bane"])

        # Okay, now test some bad input
        # second arg not a list
        self.assertRaises(cf.EtymExceptWord, cf.addTree, db, tree_dets, new_word)
        # No word given
        self.assertRaises(cf.EtymExceptWord, cf.addTree, db, tree_dets, [None])
        self.assertRaises(cf.EtymExceptWord, cf.addTree, db, tree_dets, [])
        # Invalid db
        self.assertRaises(cf.EtymExceptDB, cf.addTree, None, tree_dets, [new_word])
        # Bad tree details
        tree_dets = {"lang": "PIE", "text": "bane", "morpheme": "bane", "tag": "tree"}
        self.assertRaises(cf.EtymExceptWord, cf.addTree, db, tree_dets, [new_word])
Пример #11
0
    def OnSearch(self, event):
        """ Searches for a word, displays results """
        self.search_word = self.searchbox.GetValue()
        if self.search_word is not '': # Simple validation for now
            num_trees, matched_words = cf.searchDB(self.words_tree,
                    self.search_word)
            self.treebox.DeleteAllItems() # Clear the tree control out
            self.searchchoice.Clear() # Clear out the choice box
            self.searchchoice.Disable()
            wx.xrc.XRCCTRL(self.frame, 'et_txtLang').SetEditable(self.edit_mode)
            wx.xrc.XRCCTRL(self.frame, 'et_txtDef').SetEditable(self.edit_mode)
            wx.xrc.XRCCTRL(self.frame, 'et_txtAlt').SetEditable(self.edit_mode)
            if num_trees == 0:
                chosen_root = chosen_word = None
            elif num_trees > 1:
                # Populate the tree with the first match
                chosen_root = matched_words[0][0]
                chosen_word = [match[1] for match in matched_words]
                # Populate the choice button
                for item in matched_words:
                    word = item[1]
                    word_details = cf.loadWordDetails(word)
                    choice_text = '{0} ({1})'.format(
                            word_details['morpheme'], word_details['lang'])
                    self.searchchoice.Append(choice_text, item)

                self.searchchoice.Enable()
                self.searchchoice.SetSelection(0)
            else:
                # Extract the tree and all matched words separately
                # Just pick the first entry (m_w[...][0] are the 
                # same in this case)
                chosen_root = matched_words[0][0]
                chosen_word = [match[1] for match in matched_words]

            self.search_root = chosen_root
            self.search_words = chosen_word
            self.DisplayTree(self.search_root, self.search_words)
Пример #12
0
 def testSearchNonexistant(self):
     """ Tests how many trees an unknown word is found in """
     num_trees, matched_words = cf.searchDB(self.db, "kumquat")
     self.assertEqual(num_trees, 0)
Пример #13
0
 def testSearchBiology(self):
     """ Tests how many trees 'biology' is found in """
     num_trees, matched_words = cf.searchDB(self.db, "biology")
     self.assertEqual(num_trees, 2)
Пример #14
0
 def testSearchHorse(self):
     """ Tests how many trees 'horse' is found in """
     num_trees, matched_words = cf.searchDB(self.db, "horse")
     self.assertEqual(num_trees, 1)