Пример #1
0
    def test_prob_t_a_given_s(self):
        # arrange
        src_sentence = ["ich", "esse", "ja", "gern", "räucherschinken"]
        trg_sentence = ["i", "love", "to", "eat", "smoked", "ham"]
        corpus = [AlignedSent(trg_sentence, src_sentence)]
        alignment_info = AlignmentInfo(
            (0, 1, 4, 0, 2, 5, 5),
            [None] + src_sentence,
            ["UNUSED"] + trg_sentence,
            [[3], [1], [4], [], [2], [5, 6]],
        )

        distortion_table = defaultdict(
            lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(float)))
        )
        distortion_table[1][1][5][6] = 0.97  # i -> ich
        distortion_table[2][4][5][6] = 0.97  # love -> gern
        distortion_table[3][0][5][6] = 0.97  # to -> NULL
        distortion_table[4][2][5][6] = 0.97  # eat -> esse
        distortion_table[5][5][5][6] = 0.97  # smoked -> räucherschinken
        distortion_table[6][5][5][6] = 0.97  # ham -> räucherschinken

        translation_table = defaultdict(lambda: defaultdict(float))
        translation_table["i"]["ich"] = 0.98
        translation_table["love"]["gern"] = 0.98
        translation_table["to"][None] = 0.98
        translation_table["eat"]["esse"] = 0.98
        translation_table["smoked"]["räucherschinken"] = 0.98
        translation_table["ham"]["räucherschinken"] = 0.98

        fertility_table = defaultdict(lambda: defaultdict(float))
        fertility_table[1]["ich"] = 0.99
        fertility_table[1]["esse"] = 0.99
        fertility_table[0]["ja"] = 0.99
        fertility_table[1]["gern"] = 0.99
        fertility_table[2]["räucherschinken"] = 0.999
        fertility_table[1][None] = 0.99

        probabilities = {
            "p1": 0.167,
            "translation_table": translation_table,
            "distortion_table": distortion_table,
            "fertility_table": fertility_table,
            "alignment_table": None,
        }

        model3 = IBMModel3(corpus, 0, probabilities)

        # act
        probability = model3.prob_t_a_given_s(alignment_info)

        # assert
        null_generation = 5 * pow(0.167, 1) * pow(0.833, 4)
        fertility = 1 * 0.99 * 1 * 0.99 * 1 * 0.99 * 1 * 0.99 * 2 * 0.999
        lexical_translation = 0.98 * 0.98 * 0.98 * 0.98 * 0.98 * 0.98
        distortion = 0.97 * 0.97 * 0.97 * 0.97 * 0.97 * 0.97
        expected_probability = (
            null_generation * fertility * lexical_translation * distortion
        )
        self.assertEqual(round(probability, 4), round(expected_probability, 4))
Пример #2
0
    def test_prob_t_a_given_s(self):
        # arrange
        src_sentence = ["ich", 'esse', 'ja', 'gern', 'räucherschinken']
        trg_sentence = ['i', 'love', 'to', 'eat', 'smoked', 'ham']
        corpus = [AlignedSent(trg_sentence, src_sentence)]
        alignment_info = AlignmentInfo(
            (0, 1, 4, 0, 2, 5, 5),
            [None] + src_sentence,
            ['UNUSED'] + trg_sentence,
            [[3], [1], [4], [], [2], [5, 6]],
        )

        distortion_table = defaultdict(lambda: defaultdict(lambda: defaultdict(
            lambda: defaultdict(float))))
        distortion_table[1][1][5][6] = 0.97  # i -> ich
        distortion_table[2][4][5][6] = 0.97  # love -> gern
        distortion_table[3][0][5][6] = 0.97  # to -> NULL
        distortion_table[4][2][5][6] = 0.97  # eat -> esse
        distortion_table[5][5][5][6] = 0.97  # smoked -> räucherschinken
        distortion_table[6][5][5][6] = 0.97  # ham -> räucherschinken

        translation_table = defaultdict(lambda: defaultdict(float))
        translation_table['i']['ich'] = 0.98
        translation_table['love']['gern'] = 0.98
        translation_table['to'][None] = 0.98
        translation_table['eat']['esse'] = 0.98
        translation_table['smoked']['räucherschinken'] = 0.98
        translation_table['ham']['räucherschinken'] = 0.98

        fertility_table = defaultdict(lambda: defaultdict(float))
        fertility_table[1]['ich'] = 0.99
        fertility_table[1]['esse'] = 0.99
        fertility_table[0]['ja'] = 0.99
        fertility_table[1]['gern'] = 0.99
        fertility_table[2]['räucherschinken'] = 0.999
        fertility_table[1][None] = 0.99

        probabilities = {
            'p1': 0.167,
            'translation_table': translation_table,
            'distortion_table': distortion_table,
            'fertility_table': fertility_table,
            'alignment_table': None,
        }

        model3 = IBMModel3(corpus, 0, probabilities)

        # act
        probability = model3.prob_t_a_given_s(alignment_info)

        # assert
        null_generation = 5 * pow(0.167, 1) * pow(0.833, 4)
        fertility = 1 * 0.99 * 1 * 0.99 * 1 * 0.99 * 1 * 0.99 * 2 * 0.999
        lexical_translation = 0.98 * 0.98 * 0.98 * 0.98 * 0.98 * 0.98
        distortion = 0.97 * 0.97 * 0.97 * 0.97 * 0.97 * 0.97
        expected_probability = (null_generation * fertility *
                                lexical_translation * distortion)
        self.assertEqual(round(probability, 4), round(expected_probability, 4))
Пример #3
0
    def test_set_uniform_distortion_probabilities(self):
        # arrange
        corpus = [
            AlignedSent(['ham', 'eggs'], ['schinken', 'schinken', 'eier']),
            AlignedSent(['spam', 'spam', 'spam', 'spam'], ['spam', 'spam']),
        ]
        model3 = IBMModel3(corpus, 0)

        # act
        model3.set_uniform_probabilities(corpus)

        # assert
        # expected_prob = 1.0 / length of target sentence
        self.assertEqual(model3.distortion_table[1][0][3][2], 1.0 / 2)
        self.assertEqual(model3.distortion_table[4][2][2][4], 1.0 / 4)
Пример #4
0
    def test_set_uniform_distortion_probabilities_of_non_domain_values(self):
        # arrange
        corpus = [
            AlignedSent(['ham', 'eggs'], ['schinken', 'schinken', 'eier']),
            AlignedSent(['spam', 'spam', 'spam', 'spam'], ['spam', 'spam']),
        ]
        model3 = IBMModel3(corpus, 0)

        # act
        model3.set_uniform_probabilities(corpus)

        # assert
        # examine i and j values that are not in the training data domain
        self.assertEqual(model3.distortion_table[0][0][3][2], IBMModel.MIN_PROB)
        self.assertEqual(model3.distortion_table[9][2][2][4], IBMModel.MIN_PROB)
        self.assertEqual(model3.distortion_table[2][9][2][4], IBMModel.MIN_PROB)
Пример #5
0
    def __init__(
        self,
        sentence_aligned_corpus,
        iterations,
        source_word_classes,
        target_word_classes,
        probability_tables=None,
    ):
        """
        Train on ``sentence_aligned_corpus`` and create a lexical
        translation model, distortion models, a fertility model, and a
        model for generating NULL-aligned words.

        Translation direction is from ``AlignedSent.mots`` to
        ``AlignedSent.words``.

        :param sentence_aligned_corpus: Sentence-aligned parallel corpus
        :type sentence_aligned_corpus: list(AlignedSent)

        :param iterations: Number of iterations to run training algorithm
        :type iterations: int

        :param source_word_classes: Lookup table that maps a source word
            to its word class, the latter represented by an integer id
        :type source_word_classes: dict[str]: int

        :param target_word_classes: Lookup table that maps a target word
            to its word class, the latter represented by an integer id
        :type target_word_classes: dict[str]: int

        :param probability_tables: Optional. Use this to pass in custom
            probability values. If not specified, probabilities will be
            set to a uniform distribution, or some other sensible value.
            If specified, all the following entries must be present:
            ``translation_table``, ``alignment_table``,
            ``fertility_table``, ``p1``, ``head_distortion_table``,
            ``non_head_distortion_table``. See ``IBMModel`` and
            ``IBMModel4`` for the type and purpose of these tables.
        :type probability_tables: dict[str]: object
        """
        super(IBMModel4, self).__init__(sentence_aligned_corpus)
        self.reset_probabilities()
        self.src_classes = source_word_classes
        self.trg_classes = target_word_classes

        if probability_tables is None:
            # Get probabilities from IBM model 3
            ibm3 = IBMModel3(sentence_aligned_corpus, iterations)
            self.translation_table = ibm3.translation_table
            self.alignment_table = ibm3.alignment_table
            self.fertility_table = ibm3.fertility_table
            self.p1 = ibm3.p1
            self.set_uniform_probabilities(sentence_aligned_corpus)
        else:
            # Set user-defined probabilities
            self.translation_table = probability_tables["translation_table"]
            self.alignment_table = probability_tables["alignment_table"]
            self.fertility_table = probability_tables["fertility_table"]
            self.p1 = probability_tables["p1"]
            self.head_distortion_table = probability_tables[
                "head_distortion_table"]
            self.non_head_distortion_table = probability_tables[
                "non_head_distortion_table"]

        for n in range(0, iterations):
            self.train(sentence_aligned_corpus)
Пример #6
0
    # Structures: 1-paragraph alignment only, 2-sentence alignment based on paragraphs, 3-direct sentence alignment
    structures = {1: "para", 2: "psent", 3: "sent"}
    struct_num = 1

    for i in range(1, 44):
        en_path = 'translation-dashboard/data/en-ba-' + structures[
            struct_num] + '-align/en-chapter-' + str(i) + '.txt'
        ba_path = 'translation-dashboard/data/en-ba-' + structures[
            struct_num] + '-align/ba-chapter-' + str(i) + '.txt'
        aligned_paras.extend(para_as_sent(en_path, ba_path))
        wc += word_count(en_path)
    # print (wc.freq("i"))

    num_iterations = 20
    start = timer()
    model = IBMModel3(aligned_paras, num_iterations)
    end = timer()
    timeelapsed = end - start  # timer will only evaluate time taken to run IBM Models

    with open('align_models/ibm-model-runtimes.csv', 'a',
              encoding='utf-8') as output_file:
        output_writer = csv.writer(output_file, delimiter='\t')
        output_writer.writerow([
            "3",
            str(num_iterations), timeelapsed,
            socket.gethostname(), 'struct' + str(struct_num)
        ])
    output_file.close()

    # Save model and word count
    with open('align_models/ibm3.model', 'wb') as m_file:
def generateModels(qtype):
	# dictionary, pwC, pdf = prepare_corpus("data/linkSO",recompute=False)
	datadir = "data/linkSO"
	all_questions = pd.read_csv(join(datadir, "linkso/topublish/" + qtype + "/" + qtype + "_qid2all.txt"), sep='\t', \
								names=['qID', 'qHeader', 'qDescription', 'topVotedAnswer', 'type'])
	similar_docs_file = pd.read_csv(join(datadir, "linkso/topublish/" + qtype + "/" + qtype + "_cosidf.txt"), sep='\t', \
									names=['qID1', 'qID2', 'score', 'label'], skiprows=1)
	filtered_rows = similar_docs_file[similar_docs_file[label] == 1]
	filtered_columns = filtered_rows.filter(items=[question_id1, question_id2])
	bitext_qH_qH = []
	bitext_qD_qD = []
	bitext_qHqD_qHqD = []
	loop_counter = 0
	for each_row in filtered_columns.itertuples():
		q1ID = each_row[1]
		q2ID = each_row[2]
		q1_row = all_questions.loc[all_questions[question_id] == q1ID]
		q1header = str(q1_row[question_header].values[0]).split()
		q1desc = str(q1_row[question_description].values[0]).split()
		q1ans = str(q1_row[top_answer].values[0]).split()
		q2_row = all_questions.loc[all_questions[question_id] == q2ID]
		q2header = str(q2_row[question_header].values[0]).split()
		q2desc = str(q2_row[question_description].values[0]).split()
		q2ans = str(q2_row[top_answer].values[0]).split()
		# print("\nQ1 Header:", q1header)
		# print("Q1 Desc:", q1desc)
		# print("Q1 Answer:", q1ans)
		# print("Q2:", q2header)
		# print("Q2 Desc:", q2desc)
		# print("Q2 Answer:", q2ans)
		bitext_qH_qH.append(AlignedSent(q1header, q2header))
		bitext_qD_qD.append(AlignedSent(q1desc, q2desc))
		bitext_qHqD_qHqD.append(AlignedSent(q1header + q1desc, q2header + q2desc))
		loop_counter += 1

	# Model 1
	print("Training Model1 QH QH..")
	start = time.time()
	ibmQH = IBMModel1(bitext_qH_qH, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQH_Model1_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQH, fout)

	print("Training Model1 QD QD..")
	start = time.time()
	ibmQD = IBMModel1(bitext_qD_qD, 50)
	print("Model QD QD trained.. In", time.time() - start, " seconds..")
	with open('modelQDQD_Model1_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQD, fout)

	print("Training Model1 QHQD QHQD..")
	start = time.time()
	ibmQHQD = IBMModel1(bitext_qHqD_qHqD, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQD_Model1_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQHQD, fout)

	print(round(ibmQH.translation_table['html']['web'], 10))
	print(round(ibmQD.translation_table['html']['web'], 10))
	print(round(ibmQHQD.translation_table['html']['web'], 10))

	# Model 2
	print("Training Model2 QH QH..")
	start = time.time()
	ibmQH = IBMModel2(bitext_qH_qH, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQH_Model2_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQH, fout)

	print("Training Model2 QD QD..")
	start = time.time()
	ibmQD = IBMModel2(bitext_qD_qD, 50)
	print("Model QD QD trained.. In", time.time() - start, " seconds..")
	with open('modelQDQD_Model2_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQD, fout)

	print("Training Model2 QHQD QHQD..")
	start = time.time()
	ibmQHQD = IBMModel2(bitext_qHqD_qHqD, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQD_Model2_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQHQD, fout)

	print(round(ibmQH.translation_table['html']['web'], 10))
	print(round(ibmQD.translation_table['html']['web'], 10))
	print(round(ibmQHQD.translation_table['html']['web'], 10))

	# Model 3
	print("Training Model3 QH QH..")
	start = time.time()
	ibmQH = IBMModel3(bitext_qH_qH, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQH_Model3_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQH, fout)

	print("Training Model3 QD QD..")
	start = time.time()
	ibmQD = IBMModel3(bitext_qD_qD, 50)
	print("Model QD QD trained.. In", time.time() - start, " seconds..")
	with open('modelQDQD_Model3_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQD, fout)

	print("Training Model3 QHQD QHQD..")
	start = time.time()
	ibmQHQD = IBMModel3(bitext_qHqD_qHqD, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQD_Model3_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQHQD, fout)

	print(round(ibmQH.translation_table['html']['web'], 10))
	print(round(ibmQD.translation_table['html']['web'], 10))
	print(round(ibmQHQD.translation_table['html']['web'], 10))

	# Model 4
	print("Training Model4 QH QH..")
	start = time.time()
	ibmQH = IBMModel4(bitext_qH_qH, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQH_Model4_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQH, fout)

	print("Training Model4 QD QD..")
	start = time.time()
	ibmQD = IBMModel4(bitext_qD_qD, 50)
	print("Model QD QD trained.. In", time.time() - start, " seconds..")
	with open('modelQDQD_Model4_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQD, fout)

	print("Training Model4 QHQD QHQD..")
	start = time.time()
	ibmQHQD = IBMModel4(bitext_qHqD_qHqD, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQD_Model4_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQHQD, fout)

	print(round(ibmQH.translation_table['html']['web'], 10))
	print(round(ibmQD.translation_table['html']['web'], 10))
	print(round(ibmQHQD.translation_table['html']['web'], 10))

	# Model5
	print("Training Model5 QH QH..")
	start = time.time()
	ibmQH = IBMModel5(bitext_qH_qH, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQH_Model5_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQH, fout)

	print("Training Model5 QD QD..")
	start = time.time()
	ibmQD = IBMModel5(bitext_qD_qD, 50)
	print("Model QD QD trained.. In", time.time() - start, " seconds..")
	with open('modelQDQD_Model5_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQD, fout)

	print("Training Model5 QHQD QHQD..")
	start = time.time()
	ibmQHQD = IBMModel5(bitext_qHqD_qHqD, 50)
	print("Model QH QH trained.. In", time.time() - start, " seconds..")
	with open('modelQHQD_Model5_' + qtype + '.pk', 'wb') as fout:
		pickle.dump(ibmQHQD, fout)

	print(round(ibmQH.translation_table['html']['web'], 10))
	print(round(ibmQD.translation_table['html']['web'], 10))
	print(round(ibmQHQD.translation_table['html']['web'], 10))