Example #1
0
class TransitionMatchedWordConcordanceIndex(ConcordanceIndex):
    def __init__(self, word_list):
        self._offsets = HierarchicalIndex(3)

        for index, word in enumerate(word_list):
            if isinstance(word, UnparsableWordBinding):
                continue

            syntactic_category = word.root.syntactic_category
            secondary_syntactic_category = word.root.secondary_syntactic_category
            for suffix in word.suffixes:
                syntactic_category = suffix.to_syntactic_category
                if isinstance(suffix, DerivationalSuffixBinding):
                    secondary_syntactic_category = None

                self._offsets.insert(index, suffix.matched_word,
                                     syntactic_category,
                                     secondary_syntactic_category)

    def offsets(self,
                word_str,
                syntactic_category=None,
                secondary_syntactic_category=None):
        assert word_str is not None
        if secondary_syntactic_category:
            assert syntactic_category is not None

        args = [word_str, syntactic_category, secondary_syntactic_category]
        args = filter(lambda x: x is not None, args)

        return self._offsets.get(*args)
Example #2
0
class CompleteWordConcordanceIndex(ConcordanceIndex):
    def __init__(self, word_list):
        self._offsets = HierarchicalIndex(3)

        for index, word in enumerate(word_list):
            if isinstance(word, UnparsableWordBinding):
                continue

            self._offsets.insert(index, word.str, word.syntactic_category,
                                 word.secondary_syntactic_category)

    def offsets(self,
                word_str,
                syntactic_category=None,
                secondary_syntactic_category=None):
        assert word_str is not None
        if secondary_syntactic_category:
            assert syntactic_category is not None

        args = [word_str, syntactic_category, secondary_syntactic_category]
        args = filter(lambda x: x is not None, args)

        return self._offsets.get(*args)
Example #3
0
    def test_index_should_validate_depth_in_operations(self):
        # depth=2
        idx = HierarchicalIndex(2)

        assert_that(idx.get('A'), has_length(0))
        assert_that(idx.get('A', 'X'), has_length(0))
        self.assertRaises(AssertionError, lambda: idx.get('A', 'X', 'i'))

        self.assertRaises(AssertionError, lambda: idx.insert(1))
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A'))
        idx.insert(1, 'A', 'X')  # should not raise anything
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A', 'X', 'i'))

        assert_that(idx.get('A', 'X'), equal_to([1]))

        # depth=3
        idx = HierarchicalIndex(3)

        assert_that(idx.get('A'), has_length(0))
        assert_that(idx.get('A', 'X'), has_length(0))
        assert_that(idx.get('A', 'X', 'i'), has_length(0))
        self.assertRaises(AssertionError, lambda: idx.get('A', 'X', 'i', '1'))

        self.assertRaises(AssertionError, lambda: idx.insert(1))
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A'))
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A', 'X'))
        idx.insert(1, 'A', 'X', 'i')  # should not raise anything
        self.assertRaises(AssertionError,
                          lambda: idx.insert(1, 'A', 'X', 'i', 'a'))

        assert_that(idx.get('A', 'X', 'i'), equal_to([1]))

        # depth=N
        N = 100
        idx = HierarchicalIndex(N)
        for i in range(1, N + 1):
            assert_that(
                idx.get(*range(0, i)), has_length(0)
            )  # try calling idx.get(0,0,0,0.....0) with N items at last iteration
        self.assertRaises(AssertionError, lambda: idx.get(*range(0, N + 1)))

        for i in range(1, N):
            self.assertRaises(AssertionError,
                              lambda: idx.insert(1, *range(0, i)))
        idx.insert(1, *range(0, N))  # should not raise anything

        assert_that(idx.get(*range(0, N)), equal_to([1]))
Example #4
0
    def test_index_with_sample_case_for_depth_3(self):
        idx = HierarchicalIndex(3)

        # A
        idx.insert(1, 'A', 'X', 'i')
        idx.insert(2, 'A', 'X', 'ii')
        idx.insert(3, 'A', 'X', 'ii')

        idx.insert(4, 'A', 'Y', 'i')
        idx.insert(5, 'A', 'Y', 'ii')

        idx.insert(10, 'A', 'Z', 'ii')
        idx.insert(11, 'A', 'Z', 'iii')

        #B
        idx.insert(6, 'B', 'X', 'ii')
        idx.insert(7, 'B', 'X', 'iii')

        idx.insert(8, 'B', 'Y', 'i')

        #same queries with case for depth 2
        assert_that(idx.get('A'), equal_to([1, 2, 3, 4, 5, 10, 11]))
        assert_that(idx.get('A', 'Y'), equal_to([4, 5]))
        assert_that(idx.get('B'), equal_to([6, 7, 8]))
        assert_that(idx.get('B', 'X'), equal_to([6, 7]))

        #new queries
        assert_that(idx.get('B', 'X', 'ii'), equal_to([6]))
        assert_that(idx.get('A', 'X', 'ii'), equal_to([2, 3]))
Example #5
0
    def test_index_with_sample_case_for_depth_2(self):
        idx = HierarchicalIndex(2)

        # A
        idx.insert(1, 'A', 'X')
        idx.insert(2, 'A', 'X')
        idx.insert(3, 'A', 'X')

        idx.insert(4, 'A', 'Y')
        idx.insert(5, 'A', 'Y')

        idx.insert(10, 'A', 'Z')
        idx.insert(11, 'A', 'Z')

        #B
        idx.insert(6, 'B', 'X')
        idx.insert(7, 'B', 'X')

        idx.insert(8, 'B', 'Y')

        assert_that(idx.get('A'), equal_to([1, 2, 3, 4, 5, 10, 11]))
        assert_that(idx.get('A', 'Y'), equal_to([4, 5]))
        assert_that(idx.get('B'), equal_to([6, 7, 8]))
        assert_that(idx.get('B', 'X'), equal_to([6, 7]))
Example #6
0
    def test_index_should_validate_depth_in_operations(self):
        # depth=2
        idx = HierarchicalIndex(2)

        assert_that(idx.get('A'), has_length(0))
        assert_that(idx.get('A', 'X'), has_length(0))
        self.assertRaises(AssertionError, lambda: idx.get('A', 'X', 'i'))

        self.assertRaises(AssertionError, lambda: idx.insert(1))
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A'))
        idx.insert(1, 'A', 'X')             # should not raise anything
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A', 'X', 'i'))

        assert_that(idx.get('A', 'X'), equal_to([1]))

        # depth=3
        idx = HierarchicalIndex(3)

        assert_that(idx.get('A'), has_length(0))
        assert_that(idx.get('A', 'X'), has_length(0))
        assert_that(idx.get('A', 'X', 'i'), has_length(0))
        self.assertRaises(AssertionError, lambda: idx.get('A', 'X', 'i', '1'))

        self.assertRaises(AssertionError, lambda: idx.insert(1))
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A'))
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A', 'X'))
        idx.insert(1, 'A', 'X', 'i')        # should not raise anything
        self.assertRaises(AssertionError, lambda: idx.insert(1, 'A', 'X', 'i', 'a'))

        assert_that(idx.get('A', 'X', 'i'), equal_to([1]))

        # depth=N
        N = 100
        idx = HierarchicalIndex(N)
        for i in range(1, N + 1):
            assert_that(idx.get(*range(0, i)), has_length(0))   # try calling idx.get(0,0,0,0.....0) with N items at last iteration
        self.assertRaises(AssertionError, lambda: idx.get(*range(0, N + 1)))

        for i in range(1, N):
            self.assertRaises(AssertionError, lambda: idx.insert(1, *range(0, i)))
        idx.insert(1, *range(0, N))             # should not raise anything

        assert_that(idx.get(*range(0, N)), equal_to([1]))
Example #7
0
    def test_index_with_sample_case_for_depth_3(self):
        idx = HierarchicalIndex(3)

        # A
        idx.insert(1, 'A', 'X', 'i')
        idx.insert(2, 'A', 'X', 'ii')
        idx.insert(3, 'A', 'X', 'ii')

        idx.insert(4, 'A', 'Y', 'i')
        idx.insert(5, 'A', 'Y', 'ii')

        idx.insert(10, 'A', 'Z', 'ii')
        idx.insert(11, 'A', 'Z', 'iii')

        #B
        idx.insert(6, 'B', 'X', 'ii')
        idx.insert(7, 'B', 'X', 'iii')

        idx.insert(8, 'B', 'Y', 'i')

        #same queries with case for depth 2
        assert_that(idx.get('A'), equal_to([1, 2, 3, 4, 5, 10, 11]))
        assert_that(idx.get('A', 'Y'), equal_to([4, 5]))
        assert_that(idx.get('B'), equal_to([6, 7, 8]))
        assert_that(idx.get('B', 'X'), equal_to([6, 7]))

        #new queries
        assert_that(idx.get('B', 'X', 'ii'), equal_to([6]))
        assert_that(idx.get('A', 'X', 'ii'), equal_to([2, 3]))
Example #8
0
    def test_index_with_sample_case_for_depth_2(self):
        idx = HierarchicalIndex(2)

        # A
        idx.insert(1, 'A', 'X')
        idx.insert(2, 'A', 'X')
        idx.insert(3, 'A', 'X')

        idx.insert(4, 'A', 'Y')
        idx.insert(5, 'A', 'Y')

        idx.insert(10, 'A', 'Z')
        idx.insert(11, 'A', 'Z')

        #B
        idx.insert(6, 'B', 'X')
        idx.insert(7, 'B', 'X')

        idx.insert(8, 'B', 'Y')

        assert_that(idx.get('A'), equal_to([1, 2, 3, 4, 5, 10, 11]))
        assert_that(idx.get('A', 'Y'), equal_to([4, 5]))
        assert_that(idx.get('B'), equal_to([6, 7, 8]))
        assert_that(idx.get('B', 'X'), equal_to([6, 7]))