Skip to content

Public API

Classes

Corrector

Base class for Corrector, which is the component being tested.

Child classes should overwrite auto_correct(), auto_complete(), resolve_swipe(), and predict_next_word().

By default, the implementation for these methods is dummy : just return an empty list of candidates.

Source code in kebbie/correctors.py
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class Corrector:
    """Base class for Corrector, which is the component being tested.

    Child classes should overwrite `auto_correct()`, `auto_complete()`,
    `resolve_swipe()`, and `predict_next_word()`.

    By default, the implementation for these methods is dummy : just return an
    empty list of candidates.
    """

    def auto_correct(
        self,
        context: str,
        keystrokes: List[Optional[Tuple[float, float]]],
        word: str,
    ) -> List[str]:
        """Method used for auto-correction.
        Given a context and a typed word, this method should return a list of
        possible candidates for correction.

        Note that the typed word is given both as a plain string, and as a list
        of keystrokes. The child class overwriting this method can use either
        of them.

        Args:
            context (str): String representing the previously typed characters
                (the beginning of the sentence basically).
            keystrokes (List[Optional[Tuple[float, float]]]): List of positions
                (x and y coordinates) for each keystroke of the word being
                typed.
            word (str): Word being typed (corresponding to the keystrokes).

        Returns:
            The list of correction candidates.
        """
        return []

    def auto_complete(
        self,
        context: str,
        keystrokes: List[Optional[Tuple[float, float]]],
        partial_word: str,
    ) -> List[str]:
        """Method used for auto-completion.
        Given a context and a partially typed word, this method should return
        a list of possible candidates for completion.

        Note that the typed word is given both as a plain string, and as a list
        of keystrokes. The child class overwriting this method can use either
        of them.

        Args:
            context (str): String representing the previously typed characters
                (the beginning of the sentence basically).
            keystrokes (List[Optional[Tuple[float, float]]]): List of positions
                (x and y coordinates) for each keystroke of the word being
                typed.
            partial_word (str): Partial word being typed (corresponding to the
                keystrokes).

        Returns:
            The list of completion candidates.
        """
        return []

    def resolve_swipe(self, context: str, swipe_gesture: List[Tuple[float, float]]) -> List[str]:
        """Method used for resolving a swipe gesture. Given a context and a
        swipe gesture, this method should return a list of possible candidates
        corresponding to this swipe gesture.

        Args:
            context (str): String representing the previously typed characters
                (the beginning of the sentence basically).
            swipe_gesture (List[Tuple[float, float]]): List of positions (x and
                y coordinates) along the keyboard, representing the swipe
                gesture.

        Returns:
            The list of swiped word candidates.
        """
        return []

    def predict_next_word(self, context: str) -> List[str]:
        """Method used for next-word prediction task. Given a context, this
        method should return a list of possible candidates for next-word.

        Args:
            context (str): String representing the previously typed characters
                (the beginning of the sentence basically).

        Returns:
            The list of next-word candidates.
        """
        return []

    def profiled_auto_correct(self, *args, **kwargs) -> Tuple[List[str], int, int]:
        """Profiled (memory & runtime) version of `auto_correct` method.

        No need to overwrite this method, unless you want to specify a custom
        memory and/or runtime measure.

        Returns:
            List of candidates returned from the profiled method.
            Memory consumption in bytes.
            Runtime in nano seconds.
        """
        return profile_fn(self.auto_correct, *args, **kwargs)

    def profiled_auto_complete(self, *args, **kwargs) -> Tuple[List[str], int, int]:
        """Profiled (memory & runtime) version of `auto_complete` method.

        No need to overwrite this method, unless you want to specify a custom
        memory and/or runtime measure.

        Returns:
            List of candidates returned from the profiled method.
            Memory consumption in bytes.
            Runtime in nano seconds.
        """
        return profile_fn(self.auto_complete, *args, **kwargs)

    def profiled_resolve_swipe(self, *args, **kwargs) -> Tuple[List[str], int, int]:
        """Profiled (memory & runtime) version of `resolve_swipe` method.

        No need to overwrite this method, unless you want to specify a custom
        memory and/or runtime measure.

        Returns:
            List of candidates returned from the profiled method.
            Memory consumption in bytes.
            Runtime in nano seconds.
        """
        return profile_fn(self.resolve_swipe, *args, **kwargs)

    def profiled_predict_next_word(self, *args, **kwargs) -> Tuple[List[str], int, int]:
        """Profiled (memory & runtime) version of `predict_next_word` method.

        No need to overwrite this method, unless you want to specify a custom
        memory and/or runtime measure.

        Returns:
            List of candidates returned from the profiled method.
            Memory consumption in bytes.
            Runtime in nano seconds.
        """
        return profile_fn(self.predict_next_word, *args, **kwargs)

auto_correct(context, keystrokes, word)

Method used for auto-correction. Given a context and a typed word, this method should return a list of possible candidates for correction.

Note that the typed word is given both as a plain string, and as a list of keystrokes. The child class overwriting this method can use either of them.

Parameters:

Name Type Description Default
context str

String representing the previously typed characters (the beginning of the sentence basically).

required
keystrokes List[Optional[Tuple[float, float]]]

List of positions (x and y coordinates) for each keystroke of the word being typed.

required
word str

Word being typed (corresponding to the keystrokes).

required

Returns:

Type Description
List[str]

The list of correction candidates.

Source code in kebbie/correctors.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def auto_correct(
    self,
    context: str,
    keystrokes: List[Optional[Tuple[float, float]]],
    word: str,
) -> List[str]:
    """Method used for auto-correction.
    Given a context and a typed word, this method should return a list of
    possible candidates for correction.

    Note that the typed word is given both as a plain string, and as a list
    of keystrokes. The child class overwriting this method can use either
    of them.

    Args:
        context (str): String representing the previously typed characters
            (the beginning of the sentence basically).
        keystrokes (List[Optional[Tuple[float, float]]]): List of positions
            (x and y coordinates) for each keystroke of the word being
            typed.
        word (str): Word being typed (corresponding to the keystrokes).

    Returns:
        The list of correction candidates.
    """
    return []

auto_complete(context, keystrokes, partial_word)

Method used for auto-completion. Given a context and a partially typed word, this method should return a list of possible candidates for completion.

Note that the typed word is given both as a plain string, and as a list of keystrokes. The child class overwriting this method can use either of them.

Parameters:

Name Type Description Default
context str

String representing the previously typed characters (the beginning of the sentence basically).

required
keystrokes List[Optional[Tuple[float, float]]]

List of positions (x and y coordinates) for each keystroke of the word being typed.

required
partial_word str

Partial word being typed (corresponding to the keystrokes).

required

Returns:

Type Description
List[str]

The list of completion candidates.

Source code in kebbie/correctors.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def auto_complete(
    self,
    context: str,
    keystrokes: List[Optional[Tuple[float, float]]],
    partial_word: str,
) -> List[str]:
    """Method used for auto-completion.
    Given a context and a partially typed word, this method should return
    a list of possible candidates for completion.

    Note that the typed word is given both as a plain string, and as a list
    of keystrokes. The child class overwriting this method can use either
    of them.

    Args:
        context (str): String representing the previously typed characters
            (the beginning of the sentence basically).
        keystrokes (List[Optional[Tuple[float, float]]]): List of positions
            (x and y coordinates) for each keystroke of the word being
            typed.
        partial_word (str): Partial word being typed (corresponding to the
            keystrokes).

    Returns:
        The list of completion candidates.
    """
    return []

resolve_swipe(context, swipe_gesture)

Method used for resolving a swipe gesture. Given a context and a swipe gesture, this method should return a list of possible candidates corresponding to this swipe gesture.

Parameters:

Name Type Description Default
context str

String representing the previously typed characters (the beginning of the sentence basically).

required
swipe_gesture List[Tuple[float, float]]

List of positions (x and y coordinates) along the keyboard, representing the swipe gesture.

required

Returns:

Type Description
List[str]

The list of swiped word candidates.

Source code in kebbie/correctors.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def resolve_swipe(self, context: str, swipe_gesture: List[Tuple[float, float]]) -> List[str]:
    """Method used for resolving a swipe gesture. Given a context and a
    swipe gesture, this method should return a list of possible candidates
    corresponding to this swipe gesture.

    Args:
        context (str): String representing the previously typed characters
            (the beginning of the sentence basically).
        swipe_gesture (List[Tuple[float, float]]): List of positions (x and
            y coordinates) along the keyboard, representing the swipe
            gesture.

    Returns:
        The list of swiped word candidates.
    """
    return []

predict_next_word(context)

Method used for next-word prediction task. Given a context, this method should return a list of possible candidates for next-word.

Parameters:

Name Type Description Default
context str

String representing the previously typed characters (the beginning of the sentence basically).

required

Returns:

Type Description
List[str]

The list of next-word candidates.

Source code in kebbie/correctors.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def predict_next_word(self, context: str) -> List[str]:
    """Method used for next-word prediction task. Given a context, this
    method should return a list of possible candidates for next-word.

    Args:
        context (str): String representing the previously typed characters
            (the beginning of the sentence basically).

    Returns:
        The list of next-word candidates.
    """
    return []

profiled_auto_correct(*args, **kwargs)

Profiled (memory & runtime) version of auto_correct method.

No need to overwrite this method, unless you want to specify a custom memory and/or runtime measure.

Returns:

Type Description
List[str]

List of candidates returned from the profiled method.

int

Memory consumption in bytes.

int

Runtime in nano seconds.

Source code in kebbie/correctors.py
104
105
106
107
108
109
110
111
112
113
114
115
def profiled_auto_correct(self, *args, **kwargs) -> Tuple[List[str], int, int]:
    """Profiled (memory & runtime) version of `auto_correct` method.

    No need to overwrite this method, unless you want to specify a custom
    memory and/or runtime measure.

    Returns:
        List of candidates returned from the profiled method.
        Memory consumption in bytes.
        Runtime in nano seconds.
    """
    return profile_fn(self.auto_correct, *args, **kwargs)

profiled_auto_complete(*args, **kwargs)

Profiled (memory & runtime) version of auto_complete method.

No need to overwrite this method, unless you want to specify a custom memory and/or runtime measure.

Returns:

Type Description
List[str]

List of candidates returned from the profiled method.

int

Memory consumption in bytes.

int

Runtime in nano seconds.

Source code in kebbie/correctors.py
117
118
119
120
121
122
123
124
125
126
127
128
def profiled_auto_complete(self, *args, **kwargs) -> Tuple[List[str], int, int]:
    """Profiled (memory & runtime) version of `auto_complete` method.

    No need to overwrite this method, unless you want to specify a custom
    memory and/or runtime measure.

    Returns:
        List of candidates returned from the profiled method.
        Memory consumption in bytes.
        Runtime in nano seconds.
    """
    return profile_fn(self.auto_complete, *args, **kwargs)

profiled_resolve_swipe(*args, **kwargs)

Profiled (memory & runtime) version of resolve_swipe method.

No need to overwrite this method, unless you want to specify a custom memory and/or runtime measure.

Returns:

Type Description
List[str]

List of candidates returned from the profiled method.

int

Memory consumption in bytes.

int

Runtime in nano seconds.

Source code in kebbie/correctors.py
130
131
132
133
134
135
136
137
138
139
140
141
def profiled_resolve_swipe(self, *args, **kwargs) -> Tuple[List[str], int, int]:
    """Profiled (memory & runtime) version of `resolve_swipe` method.

    No need to overwrite this method, unless you want to specify a custom
    memory and/or runtime measure.

    Returns:
        List of candidates returned from the profiled method.
        Memory consumption in bytes.
        Runtime in nano seconds.
    """
    return profile_fn(self.resolve_swipe, *args, **kwargs)

profiled_predict_next_word(*args, **kwargs)

Profiled (memory & runtime) version of predict_next_word method.

No need to overwrite this method, unless you want to specify a custom memory and/or runtime measure.

Returns:

Type Description
List[str]

List of candidates returned from the profiled method.

int

Memory consumption in bytes.

int

Runtime in nano seconds.

Source code in kebbie/correctors.py
143
144
145
146
147
148
149
150
151
152
153
154
def profiled_predict_next_word(self, *args, **kwargs) -> Tuple[List[str], int, int]:
    """Profiled (memory & runtime) version of `predict_next_word` method.

    No need to overwrite this method, unless you want to specify a custom
    memory and/or runtime measure.

    Returns:
        List of candidates returned from the profiled method.
        Memory consumption in bytes.
        Runtime in nano seconds.
    """
    return profile_fn(self.predict_next_word, *args, **kwargs)

Functions

evaluate(corrector, lang='en-US', custom_keyboard=None, dataset=None, track_mistakes=False, n_most_common_mistakes=N_MOST_COMMON_MISTAKES, n_proc=None, seed=DEFAULT_SEED, beta=DEFAULT_BETA)

Main function of the kebbie framework, it evaluates the given Corrector.

Parameters:

Name Type Description Default
corrector Corrector

The corrector to evaluate.

required
lang str

Language to test. For now, only en-US is supported.

'en-US'
custom_keyboard Dict

If provided, instead of relying on the keyboard layout provided by default, uses the given keyboard layout.

None
dataset Dict[str, List[str]]

Data to use for testing. It should be a dictionary where the key is the name of the domain, and the value is a list of sentences. If None is given, it will use the SODA dataset.

None
track_mistakes bool

If True, we will track the most common mistakes of the Corrector (these will be saved as TSV files in the working directory).

False
n_most_common_mistakes int

If track_mistakes is set to True, the top X mistakes to record.

N_MOST_COMMON_MISTAKES
n_proc int

Number of processes to use. If None, os.cpu_count() is used.

None
seed int

Seed to use for running the tests.

DEFAULT_SEED
beta float

Beta to use for computing the F-beta score.

DEFAULT_BETA

Raises:

Type Description
UnsupportedLanguage

Exception raised if lang is set to a language that is not supported yet.

Returns:

Type Description
Dict

The results, in a dictionary.

Source code in kebbie/__init__.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def evaluate(
    corrector: Corrector,
    lang: str = "en-US",
    custom_keyboard: Dict = None,
    dataset: Dict[str, List[str]] = None,
    track_mistakes: bool = False,
    n_most_common_mistakes: int = N_MOST_COMMON_MISTAKES,
    n_proc: Optional[int] = None,
    seed: int = DEFAULT_SEED,
    beta: float = DEFAULT_BETA,
) -> Dict:
    """Main function of the `kebbie` framework, it evaluates the given
    Corrector.

    Args:
        corrector (Corrector): The corrector to evaluate.
        lang (str, optional): Language to test. For now, only `en-US` is
            supported.
        custom_keyboard (Dict, optional): If provided, instead of relying on
            the keyboard layout provided by default, uses the given keyboard
            layout.
        dataset (Dict[str, List[str]], optional): Data to use for testing. It
            should be a dictionary where the key is the name of the domain, and
            the value is a list of sentences. If `None` is given, it will use
            the SODA dataset.
        track_mistakes (bool, optional): If `True`, we will track the most
            common mistakes of the Corrector (these will be saved as TSV files
            in the working directory).
        n_most_common_mistakes (int, optional): If `track_mistakes` is set to
            `True`, the top X mistakes to record.
        n_proc (int, optional): Number of processes to use. If `None`,
            `os.cpu_count()` is used.
        seed (int): Seed to use for running the tests.
        beta (float, optional): Beta to use for computing the F-beta score.

    Raises:
        UnsupportedLanguage: Exception raised if `lang` is set to a language
            that is not supported yet.

    Returns:
        The results, in a dictionary.
    """
    if lang not in SUPPORTED_LANG and custom_keyboard is None:
        raise UnsupportedLanguage(f"{lang} is not supported yet. List of supported languages : {SUPPORTED_LANG}")

    if dataset is None:
        dataset = get_soda_dataset()

    # Create the Oracle, the class used to create test cases and evaluate the scores
    oracle = Oracle(
        lang,
        dataset,
        custom_keyboard=custom_keyboard,
        track_mistakes=track_mistakes,
        n_most_common_mistakes=n_most_common_mistakes,
        beta=beta,
    )

    # Run the tests & get the results
    results = oracle.test(corrector, n_proc=n_proc, seed=seed)
    return results

Exceptions

UnsupportedLanguage

Bases: Exception

Custom Exception when the required language is not supported.

Source code in kebbie/__init__.py
19
20
21
22
class UnsupportedLanguage(Exception):
    """Custom Exception when the required language is not supported."""

    pass