test_ibm2.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for IBM Model 2 training methods
  4. """
  5. import unittest
  6. from collections import defaultdict
  7. from nltk.translate import AlignedSent
  8. from nltk.translate import IBMModel
  9. from nltk.translate import IBMModel2
  10. from nltk.translate.ibm_model import AlignmentInfo
  11. class TestIBMModel2(unittest.TestCase):
  12. def test_set_uniform_alignment_probabilities(self):
  13. # arrange
  14. corpus = [
  15. AlignedSent(['ham', 'eggs'], ['schinken', 'schinken', 'eier']),
  16. AlignedSent(['spam', 'spam', 'spam', 'spam'], ['spam', 'spam']),
  17. ]
  18. model2 = IBMModel2(corpus, 0)
  19. # act
  20. model2.set_uniform_probabilities(corpus)
  21. # assert
  22. # expected_prob = 1.0 / (length of source sentence + 1)
  23. self.assertEqual(model2.alignment_table[0][1][3][2], 1.0 / 4)
  24. self.assertEqual(model2.alignment_table[2][4][2][4], 1.0 / 3)
  25. def test_set_uniform_alignment_probabilities_of_non_domain_values(self):
  26. # arrange
  27. corpus = [
  28. AlignedSent(['ham', 'eggs'], ['schinken', 'schinken', 'eier']),
  29. AlignedSent(['spam', 'spam', 'spam', 'spam'], ['spam', 'spam']),
  30. ]
  31. model2 = IBMModel2(corpus, 0)
  32. # act
  33. model2.set_uniform_probabilities(corpus)
  34. # assert
  35. # examine i and j values that are not in the training data domain
  36. self.assertEqual(model2.alignment_table[99][1][3][2], IBMModel.MIN_PROB)
  37. self.assertEqual(model2.alignment_table[2][99][2][4], IBMModel.MIN_PROB)
  38. def test_prob_t_a_given_s(self):
  39. # arrange
  40. src_sentence = ["ich", 'esse', 'ja', 'gern', 'räucherschinken']
  41. trg_sentence = ['i', 'love', 'to', 'eat', 'smoked', 'ham']
  42. corpus = [AlignedSent(trg_sentence, src_sentence)]
  43. alignment_info = AlignmentInfo(
  44. (0, 1, 4, 0, 2, 5, 5),
  45. [None] + src_sentence,
  46. ['UNUSED'] + trg_sentence,
  47. None,
  48. )
  49. translation_table = defaultdict(lambda: defaultdict(float))
  50. translation_table['i']['ich'] = 0.98
  51. translation_table['love']['gern'] = 0.98
  52. translation_table['to'][None] = 0.98
  53. translation_table['eat']['esse'] = 0.98
  54. translation_table['smoked']['räucherschinken'] = 0.98
  55. translation_table['ham']['räucherschinken'] = 0.98
  56. alignment_table = defaultdict(
  57. lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(float)))
  58. )
  59. alignment_table[0][3][5][6] = 0.97 # None -> to
  60. alignment_table[1][1][5][6] = 0.97 # ich -> i
  61. alignment_table[2][4][5][6] = 0.97 # esse -> eat
  62. alignment_table[4][2][5][6] = 0.97 # gern -> love
  63. alignment_table[5][5][5][6] = 0.96 # räucherschinken -> smoked
  64. alignment_table[5][6][5][6] = 0.96 # räucherschinken -> ham
  65. model2 = IBMModel2(corpus, 0)
  66. model2.translation_table = translation_table
  67. model2.alignment_table = alignment_table
  68. # act
  69. probability = model2.prob_t_a_given_s(alignment_info)
  70. # assert
  71. lexical_translation = 0.98 * 0.98 * 0.98 * 0.98 * 0.98 * 0.98
  72. alignment = 0.97 * 0.97 * 0.97 * 0.97 * 0.96 * 0.96
  73. expected_probability = lexical_translation * alignment
  74. self.assertEqual(round(probability, 4), round(expected_probability, 4))