def get_LatinWordStats(Latin_text, Latin_search_dict, i, j):

	l1_word = normalize_word(Latin_text[i])
	l2_word = normalize_word(Latin_text[i + j])

	L1 = word(l1_word, i, len(Latin_search_dict[l1_word]), "Latin")
	L2 = word(l2_word, i + j, len(Latin_search_dict[l2_word]), "Latin")

	return L1, L2
 def __init__(self, w):
     if type(w) == word:
         shortest = word(cyclicreduce(w.letters))
     else:
         shortest = word(cyclicreduce(word(w).letters))
     testword = shortest
     for i in range(len(shortest.letters)):
         firstletter = word(testword.letters[:1])
         testword = firstletter**-1 * testword * firstletter
         if testword < shortest:
             shortest = testword
         self.letters = shortest.letters
 def lifts(self, w):
     """
     Return a list of words in a free generating set for the
     subgroup.  These words represent curves whose union is the
     preimage of the curve represented by w in the initial group.
     """
     roots = self.graph.nodes()[:]
     tree = max_tree(self.graph)
     ## find the edges not in the spanning tree, and give them labels
     treeedges = set(tree.edges())
     alledges = set(self.graph.edges())
     newedges = list(alledges - treeedges)
     newgens = {}
     lab = 1
     for k in range(
             len(newedges)
     ):  # This loop populates the dictionary (edge not in spanning tree) -> (nonzero integer)
         if newedges[k][2] > 0:
             newgens[newedges[k]] = lab
             newgens[(newedges[k][1], newedges[k][0],
                      -newedges[k][2])] = -(lab)
             lab = lab + 1
     lifts_list = []
     while roots != []:  # We need to have a representative starting at an arbitrary vertex.
         root = roots.pop()
         current = root
         eat = w**1  # raise to power 1 to make sure we are copying and not disturbing w
         lift = word([])
         while len(eat) != 0 or current != root:
             if len(
                     eat
             ) == 0:  # in this case, we went through the word, but didn't end up back at root.
                 eat = w**1  # start eating another copy of w.
                 del roots[roots.index(
                     current)]  # If we were to start at current,
                 # we'd just end up with a conjugate word, so we don't do it.
             else:
                 letter = eat.pop()
                 # now look for edge in the edges from current
                 edges = self.graph.dict[current]
                 for edge in edges:
                     if edge[1] == letter:
                         # we've found the right edge, so we traverse it, adding a letter to lift if it's not in the tree.
                         tup = (current, edge[0], letter)
                         if tup in newedges:
                             lift = lift * word([newgens[tup]])
                         current = edge[0]
                         break
         lifts_list.append(lift)
     return lifts_list
	def __init__(self, search, language, lemmatize = True):
		self.text = []
		self.has_translation = []

		search= remove_punctuation(search)
		search = search.split(" ")

		for i in range(len(search)):
			search[i] = normalize_word(search[i])

		self.search_len = len(search)

		#create a word object for each word in the search phrase
		for i in range(self.search_len):

			if True == lemmatize and language:
				search[i] = get_lemma(search[i], language)

			self.text.append(word(search[i],None, None,language))
			self.has_translation.append(-1)
Example #5
0
    elif rc == '3':
        replit.clear()
        calculator()
        time.sleep(5)
        replit.clear()
    elif rc == '4':
        replit.clear()
        crosses()
        replit.clear()
    elif rc == '5':
        replit.clear()
        dessert()
        replit.clear()
    elif rc == '6':
        replit.clear()
        word()
        replit.clear()
    elif rc == '7':
        replit.clear()
        import signal
        import sys

        from board import BoardDrawer
        from game import Game

        def main():
            Game().run()

        def signal_handler(_signal, _frame):
            sys.exit(0)
def main():

    latin_cltk_importer = CorpusImporter("latin")
    latin_cltk_importer.import_corpus("latin_models_cltk")

    greek_cltk_importer = CorpusImporter("greek")
    greek_cltk_importer.import_corpus("greek_models_cltk")
    greek_cltk_importer.import_corpus("greek_word2vec_cltk")

    try:
        # Used for two of the translation functions

        transDict = None

        while True:
            function = input("Enter function to test: ")

            if function == "get_lemma":
                input_word = input("Enter word: ")
                lang = input("Enter langauge: ")

                start = time.time()
                output_word = get_lemma(input_word, lang)
                end = time.time()

                print("Output: " + output_word)
                print("Elapsed Time: " + str(end - start))

            elif function == "normalize_word":
                input_word = input("Enter word: ")

                output_word = normalize_word(input_word)

                print(output_word)

            elif function == "get_translation_pair":

                # Load in a transDict if one is not used already
                if not transDict:
                    transDict = {}
                    thesaurus_filename = input("Enter Thesaurus Filename: ")
                    transDict = ths.read_thesaurusFile(transDict, thesaurus_filename)

                input_word_1 = input("Enter first (Latin) word: ")
                input_word_2 = input("Enter second (Latin) word: ")

                L1 = word(input_word_1)
                L2 = word(input_word_2)

                start = time.time()
                get_translation_pair(L1, L2, transDict)
                end = time.time()

                print("First word translations" + str(L1.translations))
                print("Second word translations" + str(L2.translations))
                print("Elapsed Time: " + str(end - start))

            elif function == "get_translation":
                # Load in a transDict if one is not used already
                if not transDict:
                    transDict = {}
                    thesaurus_filename = input("Enter Thesaurus Filename: ")
                    transDict = ths.read_thesaurusFile(transDict, thesaurus_filename)

                input_word_1 = input("Enter a (Latin) word: ")

                L1 = word(input_word_1)

                start = time.time()
                get_translation(L1, transDict)
                end = time.time()

                print("Translations " + str(L1.translations))
                print("Elapsed Time: " + str(end - start))

                # glm = "get lemma massive" to test large calls to the lemmatizer
            elif function == "glm":

                lem_requests = int(input("Enter the number of lemmatization requests: "))

                input_words = ["veritas"] * lem_requests
                language = "Latin"

                start = time.time()
                get_lemma(input_words, language)
                end = time.time()

                print(
                    str(lem_requests)
                    + " requests complete.\n Time Elapsed:"
                    + str(end - start)
                    + "\n Time per request: "
                    + str((end - start) / lem_requests)
                )

            elif function == "clear_transDict":
                transDict = None
            else:
                print(
                    "Please enter a valid function name or command. \nValid Functions:\n\tget_lemma\n\tnormalize_word\n\tget_translation\n\tget_translation_pair\n\t\n\tglmclear_transDict"
                )

    except KeyboardInterrupt:
        return
def test_functions():

	print("Starting unit testing of simpleXLing.py")
	
	tests = []

	#====Build Search Dict====#
	
	#Attempts to build a search dictionary for a .txt file containing Latin words (Doesn't check lemmatized text file)
	curr_test = test("Build latin search dictionary (XLingFunctions.py)")
	
	latin_filename = "./test_files/small_latin.txt"
	
	words_in_file = 663

	language = "Latin"
	
	lemmatized_version = False
	
	tests.append(test_build_search_dict(curr_test,latin_filename, words_in_file, language, lemmatized_version ))
	

	#Attempts to build a search dictionary for a .txt file containing Greek wors (Doesn't check lemmatized text file)
	curr_test = test("Build greek search dictionary (XLingFunctions.py)")
	
	greek_filename = "./test_files/small_greek.txt"
	
	word_in_file = 789 
		
	language = "Greek"
	
	lemmatized_version = False
	
	tests.append(test_build_search_dict(curr_test,greek_filename, words_in_file, language, lemmatized_version)) 
	
	#Builds an arbitrary translation dictionary and attempts to find translation for a valid word pair
	curr_test = test("Get valid translations (translate.py)")

	LA = word("latina", 1, 3, None)
	LB = word("latinb", 2, 4, None)
	
	transDict = {}
	
	latin_a_translations = ["greek1", "greek2", "greek3"]
	latin_b_translations = ["greek4"]
 
	transDict["latina"] = latin_a_translations
	transDict["latinb"] = latin_b_translations

	result = trn.get_translation_pair(LA,LB,transDict)	
	
	if result == -1:
		curr_test.passed = False
		error_message = "Translations for two valid dictionary entries were not found"
		curr_test.errors.append(error_message)
	
	if not (LA.translations == latin_a_translations):
		curr_test.passed = False
		error_message = "Latina.translations doesn't match the actual translations"
		curr_test.errors.append(error_message)

	if not (LB.translations == latin_b_translations):
		curr_test.passed = False
		error_message = "Latinb.translations doesn't match the actual translations"
		curr_test.errors.append(error_message)
	
	tests.append(curr_test)

	#Attempts to find a translation for an invalid word pair
	curr_test = test("Get invalid translations (translate.py)") 

	LC = word("latinc", 2, 3, None)
	
	result = trn.get_translation_pair(LA,LC,transDict)	
	
	if not (result == -1):
		curr_test.passed = False
		error_message = "Translations for two valid dictionary entries were not found"
		curr_test.errors.append(error_message)
	
	if not (LA.translations == latin_a_translations):
		curr_test.passed = False
		error_message = "Latin_a.translations doesn't match the actual translations"
		curr_test.errors.append(error_message)

	if not (LC.translations == None):
		curr_test.passed = False
		error_message = "Latin_c. shouldn't have any translations"
		curr_test.errors.append(error_message)

	tests.append(curr_test)
	
	#Search for a translation of a pair of latin words in a greek search dictionary
	curr_test = test("Get Greek translation pair (XLingFunctions.py)") 

	L1_translation = "greek1"
	L2_translation = "greek2"

	G_search_dict = { 'greek1' : [1,3], 'greek2' : [2] } 
	
	G1, G2 = xls.get_GreekPair(L1_translation, L2_translation, G_search_dict) 
	
	if not (G1 == [1,3]): 
		curr_test.passed = False
		curr_test.errors.append("Translation array (array of indices in greek text where translation of latin word appear) is incorrect")

	if not (G2 == [2]): 
		curr_test.passed = False
		curr_test.errors.append("Translation array (array of indices in greek text where translation of latin word appear) is incorrect")
	
	tests.append(curr_test) 

	#Search for a translation of a pair of latin words in a greek search dictionary (when translations aren't in dictionary)
	curr_test = test("Get Greek translation pair with incomplete search dict (XLingFunctions.py)")
	
	L3_translation = "greek3"
	
	G1, G3 = xls.get_GreekPair(L1_translation, L3_translation, G_search_dict)
	
	if not (G1 == None): 
		curr_test.passed = False
		curr_test.errors.append("Translation array (array of indices in greek text where translation of latin word appear) is incorrect")

	if not (G3 == None): 
		curr_test.passed = False
		curr_test.errors.append("Translation array (array of indices in greek text where translation of latin word appear) is incorrect")
	
	tests.append(curr_test) 
	
	#Get latin word stats
	curr_test = test("Get Latin word stats (XLingFunctions.py)") 
 	
	latin_text = ['L1', 'L2', 'L1', 'L1', 'L2', 'L3','L4', 'L5', 'L5', 'L5']

	latin_search_dict = { 'L1' : [0,2,3], 'L2' : [1,4], 'L3' : [5], 'L4' : [6], 'L5': [7,8,9]}

	i = 3
	
	j = 4 

	L1, L2 = xls.get_LatinWordStats(latin_text,latin_search_dict, i, j)
	
	if not ( L1.word == "L1"):
		
		curr_test.passed = False
		curr_test.errors.append("Latin word object #1 corresponds to the wrong word (" + str(L1.word) + ")")
	
	if not (L1.pos == 3):
			
		curr_test.passed = False
		curr_test.errors.append("Latin word object #1 corresponds to the wrong position in text (" + str(L1.pos) + ")")
	
	if not (L1.occurences == 3):
		
		curr_test.passed = False
		curr_test.errors.append("Latin word object #1 should occur 3 times, only occurs" + str(L1.occurences) + " times")

	if not ( L2.word == "L5"):
		
		curr_test.passed = False
		curr_test.errors.append("Latin word object #2 corresponds to the wrong word (" + str(L2.word) + ")" )
	
	if not (L2.pos == 7):
	
		curr_test.passed = False
		curr_test.errors.append("Latin word object #2 corresponds to the wrong position in text (" + str(L2.pos) + ")" )
	
	if not (L2.occurences == 3):
		
		curr_test.passed = False
		curr_test.errors.append("Latin word object #2 should occur 3 times, only occurs" + str(L2.occurences) + " times")

	tests.append(curr_test)
	
	#Find match pair test (three possible matches in greek test, should take the match with the two words side by side) Uses same L1,L2, Latin text, and latin text as above
	curr_test = test("Find match pair given three matches in the greek corpus (XLingFunctions.py") 
	
	L1_translation = "g1"
	L2_translation = "g2"

	Greek_text = [ "g1", "x", "x", "g2", "g1", "x", "g2", "x", "x", "x", "g1" ] 

	Greek_search_dict = {"g1": [0,4,10], "g2": [ 3,6], "x": [1,2,5,7,8,9]}
		
	bestMatch = xls.find_match_pair(L1, L2, L1_translation, L2_translation, Greek_search_dict, latin_text, len(latin_text), Greek_text, len(Greek_text), None)

	if not bestMatch:
		
		curr_test.passed = False
		curr_test.errors.append("No match was found") 
		
	elif not (bestMatch.G1_pos == 4) or not(bestMatch.G2_pos == 3):
		
		curr_test.passed = False
		curr_test.errors.append("The best match did not occur in the expected position ( G1 = " +str(bestMatch.G1_pos) + "   G2 = " + str(bestMatch.G2_pos) )
	
	tests.append(curr_test) 

	#Ensures that try_all_search_combos produces all combos of position indicies
	curr_test = test("Try all search combos (finds best match in a greek text given a search prhase)")
	
	search = search_phrase("L1 L2 L3", "Latin")
	search.has_translation = [1, 1, 1] 

	search.text[0].translations = ["g1", "g4", "g3" ] 
	
	search.text[1].translations = ["g6"]

	search.text[2].translations = ["g5", "g2"] 

	score = scoreboard(1) 
	
	xls.try_all_search_combos( search, score, len(Greek_text), Greek_search_dict,  Greek_text)
	
	if not ( score.matches[0].G1_pos == 4 and score.matches[0].G2_pos == 3):

		curr_test.passed = False
		curr_test.errors.append("The wrong top  match was found for the search") 
	 
	tests.append(curr_test) 

	return tests