Пример #1
0
def _match(query, value, matchkind):
    if query.startswith('..'):
        query = query[1:]
        sq = query[1:]
        internal_match_ok = True
    else:
        internal_match_ok = False
    for t in value:
        try:     ### ignore regexp exceptions, required because search-ahead tries before typing is finished
            t = icu_lower(t)
            if (matchkind == EQUALS_MATCH):
                if internal_match_ok:
                    if query == t:
                        return True
                    comps = [c.strip() for c in t.split('.') if c.strip()]
                    for comp in comps:
                        if sq == comp:
                            return True
                elif query[0] == '.':
                    if t.startswith(query[1:]):
                        ql = len(query) - 1
                        if (len(t) == ql) or (t[ql:ql+1] == '.'):
                            return True
                elif query == t:
                    return True
            elif matchkind == REGEXP_MATCH:
                return re.search(query, t, re.I|re.UNICODE)
            elif matchkind == CONTAINS_MATCH:
                if pref_use_primary_find_in_search:
                    return primary_find(query, t)[0] != -1
                else:
                    return query in t
        except re.error:
            pass
    return False
Пример #2
0
def process_item(ctx, haystack, needle):
    # non-recursive implementation using a stack
    stack = [(0, 0, 0, 0, [-1]*len(needle))]
    final_score, final_positions = stack[0][-2:]
    push, pop = stack.append, stack.pop
    while stack:
        hidx, nidx, last_idx, score, positions = pop()
        key = (hidx, nidx, last_idx)
        mem = ctx.memory.get(key, None)
        if mem is None:
            for i in xrange(nidx, len(needle)):
                n = needle[i]
                if (len(haystack) - hidx < len(needle) - i):
                    score = 0
                    break
                pos = primary_find(n, haystack[hidx:])[0]
                if pos == -1:
                    score = 0
                    break
                pos += hidx

                distance = pos - last_idx
                score_for_char = ctx.max_score_per_char if distance <= 1 else calc_score_for_char(ctx, haystack[pos-1], haystack[pos], distance)
                hidx = pos + 1
                push((hidx, i, last_idx, score, list(positions)))
                last_idx = positions[i] = pos
                score += score_for_char
            ctx.memory[key] = (score, positions)
        else:
            score, positions = mem
        if score > final_score:
            final_score = score
            final_positions = positions
    return final_score, final_positions
Пример #3
0
 def test_find(self):
     ' Test searching for substrings '
     self.ae((1, 1), icu.find(b'a', b'1ab'))
     self.ae((1, 1 if sys.maxunicode >= 0x10ffff else 2), icu.find('\U0001f431', 'x\U0001f431x'))
     self.ae((1 if sys.maxunicode >= 0x10ffff else 2, 1), icu.find('y', '\U0001f431y'))
     self.ae((0, 4), icu.primary_find('pena', 'peña'))
     for k, v in {u'pèché': u'peche', u'flüße':u'Flusse', u'Štepánek':u'ŠtepaneK'}.iteritems():
         self.ae((1, len(k)), icu.primary_find(v, ' ' + k), 'Failed to find %s in %s' % (v, k))
     self.assertTrue(icu.startswith(b'abc', b'ab'))
     self.assertTrue(icu.startswith('abc', 'abc'))
     self.assertFalse(icu.startswith('xyz', 'a'))
     self.assertTrue(icu.startswith('xxx', ''))
     self.assertTrue(icu.primary_startswith('pena', 'peña'))
     self.assertTrue(icu.contains('\U0001f431', '\U0001f431'))
     self.assertTrue(icu.contains('something', 'some other something else'))
     self.assertTrue(icu.contains('', 'a'))
     self.assertTrue(icu.contains('', ''))
     self.assertFalse(icu.contains('xxx', 'xx'))
     self.assertTrue(icu.primary_contains('pena', 'peña'))
Пример #4
0
 def test_find(self):
     " Test searching for substrings "
     self.ae((1, 1), icu.find(b"a", b"1ab"))
     self.ae((1, 1 if sys.maxunicode >= 0x10FFFF else 2), icu.find("\U0001f431", "x\U0001f431x"))
     self.ae((1 if sys.maxunicode >= 0x10FFFF else 2, 1), icu.find("y", "\U0001f431y"))
     self.ae((0, 4), icu.primary_find("pena", "peña"))
     for k, v in {"pèché": "peche", "flüße": "Flusse", "Štepánek": "ŠtepaneK"}.iteritems():
         self.ae((1, len(k)), icu.primary_find(v, " " + k), "Failed to find %s in %s" % (v, k))
     self.assertTrue(icu.startswith(b"abc", b"ab"))
     self.assertTrue(icu.startswith("abc", "abc"))
     self.assertFalse(icu.startswith("xyz", "a"))
     self.assertTrue(icu.startswith("xxx", ""))
     self.assertTrue(icu.primary_startswith("pena", "peña"))
     self.assertTrue(icu.contains("\U0001f431", "\U0001f431"))
     self.assertTrue(icu.contains("something", "some other something else"))
     self.assertTrue(icu.contains("", "a"))
     self.assertTrue(icu.contains("", ""))
     self.assertFalse(icu.contains("xxx", "xx"))
     self.assertTrue(icu.primary_contains("pena", "peña"))
Пример #5
0
 def filter_tags(self, filter_value, which='available_tags'):
     collection = getattr(self, which)
     q = icu_lower(unicode(filter_value))
     for i in xrange(collection.count()):  # on every available tag
         item = collection.item(i)
         item.setHidden(bool(q and primary_find(q, unicode(item.text()))[0] == -1))