Exemplo n.º 1
0
    def test_calculate_one_way_likelihood_4gram_and_target_comes_after(self):
        # not practical but just try
        mock_context_item_0 = mock()
        mock_context_item_1 = mock()
        mock_context_item_2 = mock()
        context = [mock_context_item_0, mock_context_item_1, mock_context_item_2]

        target = mock()

        wrapped_calculator = mock()

        calculation_context = None

        when(wrapped_calculator).calculate_oneway_likelihood(target, [mock_context_item_2], True, calculation_context).thenReturn(0.5)
        when(wrapped_calculator).calculate_oneway_likelihood(target, [mock_context_item_1, mock_context_item_2], True, calculation_context).thenReturn(0.2)
        when(wrapped_calculator).calculate_oneway_likelihood(target, [mock_context_item_0, mock_context_item_1, mock_context_item_2], True,
            calculation_context).thenReturn(0.05)

        interpolating_calculator = InterpolatingLikelihoodCalculator(wrapped_calculator)

        interpolated_likelihood = interpolating_calculator.calculate_oneway_likelihood(target, context, True, calculation_context)

        assert_that(interpolated_likelihood, equal_to(0.5 * 1 / 111 + 0.2 * 10 / 111 + 0.05 * 100 / 111))

        verify(wrapped_calculator).calculate_oneway_likelihood(target, [mock_context_item_2], True, calculation_context)
        verify(wrapped_calculator).calculate_oneway_likelihood(target, [mock_context_item_1, mock_context_item_2], True, calculation_context)
        verify(wrapped_calculator).calculate_oneway_likelihood(target, [mock_context_item_0, mock_context_item_1, mock_context_item_2], True,
            calculation_context)
        verifyNoMoreInteractions(wrapped_calculator)
Exemplo n.º 2
0
    def test_calculate_one_way_likelihood_bigram_and_target_comes_before(self):
        mock_context_item_0 = mock()
        context = [mock_context_item_0]

        target = mock()

        wrapped_calculator = mock()

        calculation_context = None

        when(wrapped_calculator).calculate_oneway_likelihood(
            target, [mock_context_item_0], False,
            calculation_context).thenReturn(0.5)

        interpolating_calculator = InterpolatingLikelihoodCalculator(
            wrapped_calculator)

        interpolated_likelihood = interpolating_calculator.calculate_oneway_likelihood(
            target, context, False, calculation_context)

        assert_that(interpolated_likelihood, equal_to(0.5))

        verify(wrapped_calculator).calculate_oneway_likelihood(
            target, [mock_context_item_0], False, calculation_context)
        verifyNoMoreInteractions(wrapped_calculator)
Exemplo n.º 3
0
    def test_calculate_one_way_likelihood_trigram_and_target_comes_after(self):
        mock_context_item_0 = mock()
        mock_context_item_1 = mock()
        context = [mock_context_item_0, mock_context_item_1]

        target = mock()

        wrapped_calculator = mock()

        calculation_context = None

        when(wrapped_calculator).calculate_oneway_likelihood(
            target, [mock_context_item_1], True,
            calculation_context).thenReturn(0.5)
        when(wrapped_calculator).calculate_oneway_likelihood(
            target, [mock_context_item_0, mock_context_item_1], True,
            calculation_context).thenReturn(0.2)

        interpolating_calculator = InterpolatingLikelihoodCalculator(
            wrapped_calculator)

        interpolated_likelihood = interpolating_calculator.calculate_oneway_likelihood(
            target, context, True, calculation_context)

        assert_that(interpolated_likelihood,
                    equal_to(0.5 * 1 / 11 + 0.2 * 10 / 11))

        verify(wrapped_calculator).calculate_oneway_likelihood(
            target, [mock_context_item_1], True, calculation_context)
        verify(wrapped_calculator).calculate_oneway_likelihood(
            target, [mock_context_item_0, mock_context_item_1], True,
            calculation_context)
        verifyNoMoreInteractions(wrapped_calculator)
Exemplo n.º 4
0
    def setUpClass(cls):
        super(InterpolatingLikelihoodCalculatorCalculationContextTest,
              cls).setUpClass()
        all_roots = []

        lexemes = LexiconLoader.load_from_file(
            os.path.join(os.path.dirname(__file__),
                         '../../../../../resources/master_dictionary.txt'))
        for di in lexemes:
            all_roots.extend(RootGenerator.generate(di))

        root_map_generator = RootMapGenerator()
        cls.root_map = root_map_generator.generate(all_roots)

        suffix_graph = CopulaSuffixGraph(
            NumeralSuffixGraph(ProperNounSuffixGraph(BasicSuffixGraph())))
        suffix_graph.initialize()

        predefined_paths = PredefinedPaths(cls.root_map, suffix_graph)
        predefined_paths.create_predefined_paths()

        word_root_finder = WordRootFinder(cls.root_map)
        digit_numeral_root_finder = DigitNumeralRootFinder()
        text_numeral_root_finder = TextNumeralRootFinder(cls.root_map)
        proper_noun_from_apostrophe_root_finder = ProperNounFromApostropheRootFinder(
        )
        proper_noun_without_apostrophe_root_finder = ProperNounWithoutApostropheRootFinder(
        )

        cls.contextless_parser = UpperCaseSupportingContextlessMorphologicalParser(
            suffix_graph, predefined_paths, [
                word_root_finder, digit_numeral_root_finder,
                text_numeral_root_finder,
                proper_noun_from_apostrophe_root_finder,
                proper_noun_without_apostrophe_root_finder
            ])

        mongodb_connection = pymongo.Connection(host='127.0.0.1')
        cls.collection_map = {
            1: mongodb_connection['trnltk']['wordUnigrams999'],
            2: mongodb_connection['trnltk']['wordBigrams999'],
            3: mongodb_connection['trnltk']['wordTrigrams999']
        }

        database_index_builder = DatabaseIndexBuilder(cls.collection_map)
        target_form_given_context_counter = InMemoryCachingTargetFormGivenContextCounter(
            cls.collection_map)
        ngram_frequency_smoother = CachedSimpleGoodTuringNGramFrequencySmoother(
        )
        sequence_likelihood_calculator = UniformSequenceLikelihoodCalculator()

        wrapped_generator = ContextParsingLikelihoodCalculator(
            database_index_builder, target_form_given_context_counter,
            ngram_frequency_smoother, sequence_likelihood_calculator)

        cls.generator = InterpolatingLikelihoodCalculator(wrapped_generator)
Exemplo n.º 5
0
    def test_calculate_one_way_likelihood_bigram_and_target_comes_before(self):
        mock_context_item_0 = mock()
        context = [mock_context_item_0]

        target = mock()

        wrapped_calculator = mock()

        calculation_context = None

        when(wrapped_calculator).calculate_oneway_likelihood(target, [mock_context_item_0], False, calculation_context).thenReturn(0.5)

        interpolating_calculator = InterpolatingLikelihoodCalculator(wrapped_calculator)

        interpolated_likelihood = interpolating_calculator.calculate_oneway_likelihood(target, context, False, calculation_context)

        assert_that(interpolated_likelihood, equal_to(0.5))

        verify(wrapped_calculator).calculate_oneway_likelihood(target, [mock_context_item_0], False, calculation_context)
        verifyNoMoreInteractions(wrapped_calculator)