コード例 #1
0
 def translate(dictionary, input, output):
     """
     (File, File, File) -> None
     Reads all the word pairs from the dictionary file
     and then reads words from the input file,
     translates the words (if possible),
     and writes them to the output file.
     """
     searchTree = AVLTree()
     for line in dictionary.readlines():
         words = line.split()
         assert len(words) == 2
         searchTree.insert(Association(words[0], words[1]))
     for line in input.readlines():
         for word in line.split():
             assoc = searchTree.find(Association(word))
             if assoc is None:
                 output.write(word + " ")
             else:
                 output.write(assoc.value + " ")
         output.write("\n")
コード例 #2
0
ファイル: algorithms.py プロジェクト: RyoDream/PythonLearning
    def translate(dictionary, input, output):
        """
        (File, File, File) -> None
        Reads all the word pairs from the dictionary file
        and then reads words from the input file,
        translates the words (if possible),
        and writes them to the output file.
        """
        searchTree = AVLTree()
        for line in dictionary.readlines():
            words = line.split()
	    assert len(words) == 2
            searchTree.insert(Association(words[0], words[1]))
        for line in input.readlines():
            for word in line.split():
                assoc = searchTree.find(Association(word))
                if assoc is None:
                    output.write(word + " ")
                else:
                    output.write(assoc.value + " ")
            output.write("\n")