~mapperr/trl

trl/trullo/normalizer.py -rw-r--r-- 848 bytes
edfaf2b5 — mapperr Add line break to the readme 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from typing import List

import attr

from trullo.shortcuttable import Shortcuttable


@attr.s(auto_attribs=True)
class Normalizer:
    @staticmethod
    def normalize(string: str) -> str:
        result = ''
        for char in string:
            if char.lower() not in 'qwertyuiopasdfghjklzxcvbnm1234567890':
                continue
            result += char.lower()
        return result

    @staticmethod
    def get_matches(
            shortcut: str,
            shortcuttables: List[Shortcuttable]) -> List[any]:
        return [
            shortcuttable for shortcuttable in shortcuttables
            if Normalizer.is_a_match(
                shortcut,
                shortcuttable.get_normalized_name())]

    @staticmethod
    def is_a_match(shortcut: str, normalized_name: str) -> bool:
        return shortcut in normalized_name