Internals
cmd.py
Module containing the implementation for the kebbie
command line.
instantiate_correctors(keyboard, get_layout=True, fast_mode=True, instantiate_emulator=True)
Create the right correctors (with the right platform, etc...) given the arguments from the command line.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keyboard |
str
|
Name fo the keyboard to load. |
required |
fast_mode |
bool
|
If |
True
|
instantiate_emulator |
bool
|
If |
True
|
get_layout |
bool
|
If |
True
|
Returns:
Type | Description |
---|---|
List[EmulatorCorrector]
|
The list of created Correctors. |
Source code in kebbie/cmd.py
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 |
|
common_args(parser)
Add common arguments to the given parser.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parser |
ArgumentParser
|
Parser where to add the arguments. |
required |
Source code in kebbie/cmd.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
cli()
Entry-point of the kebbie
command line.
Source code in kebbie/cmd.py
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
correctors.py
Module containing the base Corrector class.
EmulatorCorrector
Bases: Corrector
Corrector using an emulated keyboard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
platform |
str
|
Name of the platform used. |
required |
keyboard |
str
|
Name of the keyboard to test. |
required |
device |
str
|
Device UDID to use for the emulator. |
None
|
fast_mode |
bool
|
If |
True
|
instantiate_emulator |
bool
|
If |
True
|
Source code in kebbie/correctors.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
__reduce__()
This method simply makes the object pickable.
Returns:
Type | Description |
---|---|
Tuple
|
Tuple of callable and arguments. |
Source code in kebbie/correctors.py
211 212 213 214 215 216 217 218 219 220 |
|
cached_type(context, word)
This class keeps track of the content of the context currently typed in the emulator. This method uses this current context to determine if we need to retype the sentence or not. Instead of always erasing the content being typed, we can directly type the remaining characters, which saves up time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
str
|
Context to paste. |
required |
word |
str
|
Word to type. |
required |
Source code in kebbie/correctors.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
auto_correct(context, keystrokes, word)
Implementation of auto_correct
method for emulated keyboards.
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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
auto_complete(context, keystrokes, partial_word)
Implementation of auto_complete
method for emulated keyboards.
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
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
predict_next_word(context)
Implementation of predict_next_word
method for emulated keyboards.
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
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
emulator.py
Module containing the code necessary to interact with the emulators, using Appium.
Emulator
Class used to interact with an emulator and type word on a given keyboard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
platform |
str
|
|
required |
keyboard |
str
|
The name of the keyboard installed on the emulator. This is needed because each keyboard has a different layout, and we need to know each key's position in order to type words. |
required |
device |
str
|
Device UDID to use. |
None
|
host |
str
|
Appium server's address. |
'127.0.0.1'
|
port |
str
|
Appium server's port. |
'4723'
|
get_layout |
bool
|
Set False to don't map the keys. |
True
|
Raises:
Type | Description |
---|---|
ValueError
|
Error raised if the given platform doesn't exist. |
Source code in kebbie/emulator.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 |
|
get_android_devices()
Static method that uses the adb devices
command to retrieve the
list of devices running.
Returns:
Type | Description |
---|---|
List[str]
|
List of detected device UDID. |
Source code in kebbie/emulator.py
403 404 405 406 407 408 409 410 411 412 413 |
|
select_keyboard(keyboard)
Searches the IME of the desired keyboard and selects it, only for Android.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keyboard |
str
|
Keyboard to search. |
required |
Source code in kebbie/emulator.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
|
get_ios_devices()
Static method that uses the xcrun simctl
command to retrieve the
list of booted devices.
Returns:
Type | Description |
---|---|
List[Tuple[str, str]]
|
List of booted device platform and device name. |
Source code in kebbie/emulator.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
|
paste(text)
Paste the given text into the typing field, to quickly simulate typing a context.
This method is just a wrapper around _paste()
, making sure the typing
field is accessible. If for some reason it is not accessible, it tries
to access it and perform the action again.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Text to paste. |
required |
Source code in kebbie/emulator.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
|
type_characters(characters)
Type the given sentence on the keyboard. For each character, it finds the keys to press and send a tap on the keyboard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
characters |
str
|
The sentence to type. |
required |
Source code in kebbie/emulator.py
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
|
get_predictions(lang='en')
Retrieve the predictions displayed by the keyboard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lang |
str
|
Language to use for the OCR. |
'en'
|
Returns:
Type | Description |
---|---|
List[str]
|
List of predictions from the keyboard. |
Source code in kebbie/emulator.py
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
|
get_text()
Return the text currently contained in the typing field.
This method is just a wrapper around _get_text()
, making sure the
typing field is accessible. If for some reason it is not accessible, it
tries to access it and perform the action again.
Returns:
Type | Description |
---|---|
str
|
Text of the typing field. |
Source code in kebbie/emulator.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
|
show_keyboards()
Take a screenshot and overlay the given layout, for debugging the position of each keys.
Source code in kebbie/emulator.py
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
|
LayoutDetector
Base class for auto-detection of the keyboard layout.
To auto-detect a new keyboard, create a new sub-class, and overwite
__init__()
and get_suggestions()
. Use the existing subclass for GBoard
as reference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
driver |
Remote
|
The Appium driver, used to access elements on the emulator. |
required |
tap_fn |
Callable
|
A callback used to tap at specific position on the
screen. See |
required |
xpath_root |
str
|
XPath to the root element of the keyboard. |
required |
xpath_keys |
str
|
XPath to detect the keys elements. |
required |
Source code in kebbie/emulator.py
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 |
|
get_suggestions()
Method to retrieve the keyboard suggestions from the XML tree.
Note that it's slower to access the XML through methods like
find_element()
, and it's faster to access the raw XML with
self.driver.page_source
and parse it as text directly.
Raises:
Type | Description |
---|---|
NotImplementedError
|
Exception raised if this method is not overwritten. |
Returns:
Type | Description |
---|---|
List[str]
|
List of suggestions from the keyboard. |
Source code in kebbie/emulator.py
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
|
GboardLayoutDetector
Bases: LayoutDetector
Layout detector for the Gboard keyboard. See LayoutDetector
for more
information.
Source code in kebbie/emulator.py
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 |
|
get_suggestions()
Method to retrieve the keyboard suggestions from the XML tree.
Returns:
Type | Description |
---|---|
List[str]
|
List of suggestions from the keyboard. |
Source code in kebbie/emulator.py
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 |
|
IosLayoutDetector
Bases: LayoutDetector
Layout detector for the iOS default keyboard. See LayoutDetector
for
more information.
Source code in kebbie/emulator.py
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 |
|
get_suggestions()
Method to retrieve the keyboard suggestions from the XML tree.
Returns:
Type | Description |
---|---|
List[str]
|
List of suggestions from the keyboard. |
Source code in kebbie/emulator.py
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 |
|
KbkitproLayoutDetector
Bases: LayoutDetector
Layout detector for the KeyboardKit Pro demo keyboard. See
LayoutDetector
for more information.
Source code in kebbie/emulator.py
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 |
|
get_suggestions()
Method to retrieve the keyboard suggestions from the XML tree.
Returns:
Type | Description |
---|---|
List[str]
|
List of suggestions from the keyboard. |
Source code in kebbie/emulator.py
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 |
|
KbkitossLayoutDetector
Bases: LayoutDetector
Layout detector for the KeyboardKit OSS demo keyboard. See
LayoutDetector
for more information.
Source code in kebbie/emulator.py
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
|
get_suggestions()
Method to retrieve the keyboard suggestions from the XML tree.
Returns:
Type | Description |
---|---|
List[str]
|
List of suggestions from the keyboard. |
Source code in kebbie/emulator.py
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
|
SwiftkeyLayoutDetector
Bases: LayoutDetector
Layout detector for the Swiftkey keyboard. See LayoutDetector
for more
information.
Source code in kebbie/emulator.py
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 |
|
get_suggestions()
Method to retrieve the keyboard suggestions from the XML tree.
Returns:
Type | Description |
---|---|
List[str]
|
List of suggestions from the keyboard. |
Source code in kebbie/emulator.py
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 |
|
YandexLayoutDetector
Bases: LayoutDetector
Layout detector for the Yandex keyboard. See LayoutDetector
for more
information.
Source code in kebbie/emulator.py
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 |
|
get_suggestions()
Method to retrieve the keyboard suggestions from the XML tree.
Returns:
Type | Description |
---|---|
List[str]
|
List of suggestions from the keyboard. |
Source code in kebbie/emulator.py
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 |
|
TappaLayoutDetector
Bases: LayoutDetector
Layout detector for the Tappa keyboard. See LayoutDetector
for more
information.
Source code in kebbie/emulator.py
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 |
|
get_suggestions()
Method to retrieve the keyboard suggestions from the XML tree.
Returns:
Type | Description |
---|---|
List[str]
|
List of suggestions from the keyboard. |
Source code in kebbie/emulator.py
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 |
|
FleksyLayoutDetector
Bases: LayoutDetector
Layout detector for the Fleksy keyboard. See LayoutDetector
for more
information.
Note that this class is only semi-automatically detected : the layout itself is not detected, but the suggestions are retrieved from the XML tree (no need to rely on OCR, much faster). The layout is hard-coded for now.
Source code in kebbie/emulator.py
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
|
get_suggestions()
Method to retrieve the keyboard suggestions from the XML tree.
Returns:
Type | Description |
---|---|
List[str]
|
List of suggestions from the keyboard. |
Source code in kebbie/emulator.py
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
|
gesture.py
Module containing the function make_swipe_gesture
, which is used to create
a natural-looking swipe gesture from a list of letter-points.
make_swipe_gesture(control_points)
Function to generate artificial swipe gesture from a list of points. The given points represents the typed letters on the keyboard. This function simply generate several other points between the control points. Points are generated using sequential Bezier curves. The resulting swipe gesture pass by the control points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
control_points |
List[Tuple[float, float]]
|
Control points, representing the letter typed. The resulting swipe gesture will pass by these points. |
required |
Returns:
Type | Description |
---|---|
List[Tuple[float, float]]
|
Points generated by the swipe gesture. |
Source code in kebbie/gesture.py
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 |
|
random_point_around(p, radius)
Generate a random point around the given point p, within the given radius.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p |
Tuple[float, float]
|
Coordinates to use as a starting point. |
required |
radius |
float
|
Radius within the starting point to generate the random point. |
required |
Returns:
Type | Description |
---|---|
Tuple[float, float]
|
Coordinates of the generated random point. |
Source code in kebbie/gesture.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
bernstein_poly(i, n, t)
The Bernstein polynomial of n, i as a function of t.
Taken from : https://stackoverflow.com/a/12644499/9494790
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
int
|
i |
required |
n |
int
|
n |
required |
t |
float
|
t |
required |
Returns:
Type | Description |
---|---|
float
|
The computed value for this polynomial function. |
Source code in kebbie/gesture.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
bezier_curve(control_points, linspace)
Given a set of control points, return the bezier curve defined by the control points.
See : http://processingjs.nihongoresources.com/bezierinfo/
Taken from : https://stackoverflow.com/a/12644499/9494790
Parameters:
Name | Type | Description | Default |
---|---|---|---|
control_points |
List[Tuple[float, float]]
|
Control points used to generate the bezier curve. |
required |
linspace |
List[float]
|
Linspace to use for sampling points across the Bezier curve. |
required |
Returns:
Type | Description |
---|---|
Tuple[List[float], List[float]]
|
Sampled points along the bezier curve. |
Source code in kebbie/gesture.py
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 |
|
accelerated_linspace(n, acceleration)
Alternative to np.linspace, instead of giving a range of number evenly distributed, this one is not evenly distributed, and simulate an acceleration at first, and then a deceleration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
Number of points to generate in the linspace. |
required |
acceleration |
float
|
A number that dictate how constant the acceleration is. The lower, the more S-shape is used. |
required |
Returns:
Type | Description |
---|---|
List[float]
|
Generated points. |
Source code in kebbie/gesture.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
layout.py
Module containing the helpers LayoutHelper
, useful class to deal with the
layout of a keyboard, access key positions, etc...
KeyInfo
dataclass
Structure containing all information needed for a given character (key).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
klayer_id |
int
|
Keyboard Layer ID where this key is located. |
required |
width |
float
|
Width of the key. |
required |
height |
float
|
Height of the key. |
required |
center |
Tuple[float, float]
|
Center position (x, y coordinates) of the key. |
required |
Source code in kebbie/layout.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Key
dataclass
Structure containing information needed for each key of a given keyboard layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
char |
str
|
Character associated with this key. |
required |
bounds |
Dict[str, float]
|
Dictionary representing the bounding box of
the key. The dictionary should contains the following keys :
|
required |
Source code in kebbie/layout.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
LayoutHelper
Small class that represents a Keyboard layout. The goal of this class is to offer some easy-to-use method to deal with a keyboard layout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lang |
str
|
Language of the layout to load. |
'en-US'
|
custom_keyboard |
Dict
|
If provided, instead of relying on the keyboard layout provided by default, uses the given keyboard layout. |
None
|
ignore_layers_after |
Optional[int])
|
Ignore higher layers of the
keyboard layout. If |
None
|
Source code in kebbie/layout.py
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
get_key_info(char)
Method to retrieve the information associated to a specific key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
char |
str
|
Character for which to retrieve key information. |
required |
Raises:
Type | Description |
---|---|
KeyError
|
Exception raised if the given character can't be typed ( because it doesn't exist on this keyboard layout). |
Returns:
Type | Description |
---|---|
float
|
Width of the key for the requested character. |
float
|
Height of the key for the requested character. |
float
|
Center position (x-axis) of the key for the requested character. |
float
|
Center position (y-axis) of the key for the requested character. |
int
|
Keyboard layer ID where the character's key is located. |
Source code in kebbie/layout.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
get_key(pos, klayer_id)
Get the character associated with the given position.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos |
Tuple[float, float]
|
Position (x, y) in the keyboard. |
required |
klayer_id |
int
|
Keyboard layer ID to use. |
required |
Returns:
Type | Description |
---|---|
str
|
Character associated to the given position. |
Source code in kebbie/layout.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
noise_model.py
Module defining the NoiseModel
class, which takes care of introducing
typos in a clean text (and later see if the model can properly correct these
typos).
Typo
Bases: Enum
Enum listing all possible typos that can be introduced.
Source code in kebbie/noise_model.py
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 |
|
NoiseModel
Class responsible for introducing typo in a clean text.
Most of typos are introduced on text directly. Then fuzzy typing is applied, using two Gaussian distributions (for x-axis and y-axis), mimicking a user typing on a soft keyboard.
The ratio arguments are here to choose how wide the Gaussian distribution is. A wider distribution will be less precise, a narrower distribution will be more precise. To test how wide a ratio is, run the following code :
from scipy.stats import norm
def compute(x):
cdf = norm.cdf(x)
return cdf - (1 - cdf)
print(compute(2.32)) # >>> 0.9796591226625606
2.32
gives a precision of ~98% (a typo will
be introduced in 2% of the cases).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lang |
str
|
Language used. |
required |
custom_keyboard |
Dict
|
If provided, instead of relying on the keyboard layout provided by default, uses the given keyboard layout. |
None
|
common_typos |
Optional[Dict[str, List[str]]]
|
Dictionary of
common typos. If |
None
|
typo_probs |
Optional[Dict[str, float]]
|
Probabilities for
each type of typos. If |
None
|
x_offset |
float
|
Parameter for the Gaussian distribution for the fuzzy typing. Base position offset on the x-axis. |
0
|
y_offset |
float
|
Parameter for the Gaussian distribution for the fuzzy typing. Base position offset on the y-axis. |
0
|
x_ratio |
float
|
Parameter for the Gaussian distribution for the fuzzy typing. It controls how wide the distribution is on the x-axis, which is the precision of the typing. |
DEFAULT_SIGMA_RATIO
|
y_ratio |
float
|
Parameter for the Gaussian distribution for the fuzzy typing. It controls how wide the distribution is on the y-axis, which is the precision of the typing. |
DEFAULT_SIGMA_RATIO
|
Source code in kebbie/noise_model.py
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
|
type_till_space(words)
Method introducing typos word by word.
This method receives a list of words, and type these words while introducing typos. So most of the time, only one word will be typed and the method will return. In some cases, the space is mistyped or deleted, so two words are typed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
words |
List[str]
|
List of words to type. |
required |
Returns:
Type | Description |
---|---|
List[Optional[Tuple[float, float]]]
|
List of keystrokes (may contains some None). |
str
|
The typed characters as string. |
int
|
The number of words typed. |
List[Typo]
|
The list of typos introduced in the string typed. |
Source code in kebbie/noise_model.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
swipe(word)
Method for creating an artificial swipe gesture given a word.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
word |
str
|
Word to type with a swipe gesture. |
required |
Returns:
Type | Description |
---|---|
Optional[List[Tuple[float, float]]]
|
Positions (x, y) of the generated swipe gesture, or None if the swipe gesture couldn't be created. |
Source code in kebbie/noise_model.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
oracle.py
Module defining the Oracle
class, which is the class taking care of
iterating the dataset, introducing typos using the noise model, and querying
the Corrector to correct these typos. Then the scorer is used to compute
metrics about the performances, and the results are returned.
Oracle
Class that takes care of testing a Corrector. It basically gets clean text data, adds noise to it, send the noisy data to the Corrector, and scores its output.
This class spawn multiple processes to decrease runtime.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lang |
str
|
Language used. |
required |
test_data |
Dict[str, List[str]]
|
List of clean sentences for each domain. |
required |
custom_keyboard |
Dict
|
If provided, instead of relying on the keyboard layout provided by default, uses the given keyboard layout. |
required |
track_mistakes |
bool
|
Set to |
required |
n_most_common_mistakes |
int
|
If |
required |
beta |
float
|
Beta to use for computing the F-beta score. |
required |
Source code in kebbie/oracle.py
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
test(corrector, n_proc, seed)
Main method, it tests the given Corrector, and returns results as a dictionary.
This method spawn multiple processes to decrease runtime.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
corrector |
Union[Corrector, List[Corrector]]
|
Corrector to test.
If a list of Corrector is given, the argument |
required |
n_proc |
Optional[int]
|
Number of processes to use. If |
required |
seed |
int
|
Seed to use for running the tests. |
required |
Returns:
Type | Description |
---|---|
Dict
|
Results formatted in a dictionary. |
Source code in kebbie/oracle.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
init_tester(fn, lang, custom_keyboard, correctors, seed, track_mistakes)
Function run at process initialization for Tester workers.
Each worker in a Pool will run this function when created. It will instanciate several things needed for testing the given corrector : * A Tokenizer to split sentences into words * A NoiseModel to introduce typos * A Corrector instance, which is the model we want to test
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn |
Callable
|
Main tester function (instanciated objects will be attached to this function). |
required |
lang |
str
|
Language used. |
required |
custom_keyboard |
Dict
|
If provided, instead of relying on the keyboard layout provided by default, uses the given keyboard layout. |
required |
correctors |
Queue
|
Queue containing list of correctors to test. Each process will get the next corrector available in queue. |
required |
seed |
int
|
Base seed to use. |
required |
track_mistakes |
bool
|
Set to |
required |
Source code in kebbie/oracle.py
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 |
|
tester(sentence)
Function to test a given sentence.
It uses the noise model to introduce typos word by word, run the Corrector on various tasks (auto-completion, auto-correction, next-word prediction), and score the results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sentence |
str
|
Sentence to use as data for the test. |
required |
Returns:
Type | Description |
---|---|
Scorer
|
Scorer class with the prediction counts for this sentence. |
Source code in kebbie/oracle.py
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 |
|
scorer.py
Module implementing Scorer
, a class that keep track of how many errors
the model is making, and output various corresponding metrics.
Count
dataclass
Structure representing the most basic counts for a task.
It counts : * Number of correct predictions * Number of top3-correct predictions * Total number of predictions
Source code in kebbie/scorer.py
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 |
|
__add__(count)
Merge two Count
instance by adding their counts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
count |
Count
|
Count instance to add. |
required |
Returns:
Type | Description |
---|---|
Count
|
Merged Count. |
Source code in kebbie/scorer.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
__mul__(proportion)
Multiply the current Count
instance by a given proportion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proportion |
float
|
Proportion to multiply by. |
required |
Returns:
Type | Description |
---|---|
Count
|
Count with the right proportion. |
Source code in kebbie/scorer.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
Mistake
dataclass
Structure representing a mistake (including the context of the mistake, the expected word and the predictions).
Source code in kebbie/scorer.py
67 68 69 70 71 72 73 74 75 |
|
Scorer
Class keeping track of the predictions and how correct they are, but also computing the associated score for each task after the end of test.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domains |
List[str]
|
The list of domains in the dataset. The Scorer keeps track of the score for each domain, so that we can spot discrepancies between domain, if any. |
required |
human_readable |
bool
|
If set to |
True
|
track_mistakes |
bool
|
Set to |
False
|
Source code in kebbie/scorer.py
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
|
add(scorer)
Method to update the current Scorer with the counts from another Scorer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scorer |
Scorer
|
Scorer to add. |
required |
Source code in kebbie/scorer.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
nwp(true_word, predicted_words, context, memory, runtime, domain=None)
Method used to record a prediction for the next-word prediction task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
true_word |
str
|
The label (clean word to predict). |
required |
predicted_words |
List[str]
|
Predictions of the model. |
required |
context |
str
|
The context (previous words in the sentence). |
required |
memory |
int
|
Memory consumption for the call of the model. |
required |
runtime |
int
|
Runtime for the call of the model. |
required |
domain |
str
|
Domain of this prediction. |
None
|
Source code in kebbie/scorer.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
acp(true_word, predicted_words, partial_word, context, memory, runtime, domain=None)
Method used to record a prediction for the auto-completion task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
true_word |
str
|
The label (clean word to predict). |
required |
predicted_words |
List[str]
|
Predictions of the model. |
required |
partial_word |
str
|
The input sent to the model (only part of the word to predict, with potential typos). |
required |
context |
str
|
The context (previous words in the sentence). |
required |
memory |
int
|
Memory consumption for the call of the model. |
required |
runtime |
int
|
Runtime for the call of the model. |
required |
domain |
str
|
Domain of this prediction. |
None
|
Source code in kebbie/scorer.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
acr(true_word, predicted_words, typed_word, context, typos, memory, runtime, domain=None)
Method used to record a prediction for the auto-correction task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
true_word |
str
|
The label (clean word to predict). |
required |
predicted_words |
List[str]
|
Predictions of the model. |
required |
typed_word |
str
|
The word typed, containing potential typos. |
required |
context |
str
|
The context (previous words in the sentence). |
required |
typos |
List[Typo]
|
List of typos introduced. |
required |
memory |
int
|
Memory consumption for the call of the model. |
required |
runtime |
int
|
Runtime for the call of the model. |
required |
domain |
str
|
Domain of this prediction. |
None
|
Source code in kebbie/scorer.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
swp(true_word, predicted_words, context, memory, runtime, domain=None)
Method used to record a prediction for the swipe resolution task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
true_word |
str
|
The label (clean word to predict). |
required |
predicted_words |
List[str]
|
Predictions of the model. |
required |
context |
str
|
The context (previous words in the sentence). |
required |
memory |
int
|
Memory consumption for the call of the model. |
required |
runtime |
int
|
Runtime for the call of the model. |
required |
domain |
str
|
Domain of this prediction. |
None
|
Source code in kebbie/scorer.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
|
set_domain(domain)
Method setting the domain for the scores associated with no domain.
To make it easier to score a single sentence, it's possible to call the
scorer without a domain (see signature of nwp()
, acp()
, acr()
).
In this case the scores are associated to no domain (None
key).
This method allows the user to set the domain name for these scores
with no domain (effectively moving the None
domain scores to the
given domain name).
Note
If some scores were already linked to the given domain, these
scores will be erased (replaced by the scores of the None
domain).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domain |
str
|
Domain name to associate the scores to. |
required |
Source code in kebbie/scorer.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|
score(beta=DEFAULT_BETA)
Method that computes the final scores (as well as some alternative metrics that can bring insight in the capabilities of the model), and output these in an organized dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
beta |
float
|
Beta to use for computing the F-beta score. |
DEFAULT_BETA
|
Returns:
Type | Description |
---|---|
Dict
|
Dictionary containing the computed scores and metrics for the |
Dict
|
model tested. |
Source code in kebbie/scorer.py
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
|
dd_x_layers(n_layers=1)
Helper function for creating a nested defaultdict, with a specified number of nest level. The end object is a Count.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_layers |
int
|
Number of layer for the defaultdict. |
1
|
Returns:
Type | Description |
---|---|
defaultdict
|
Created nested defaultdict. |
Source code in kebbie/scorer.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
one_score(results)
One Score to rule them all, One Score to find them, One Score to bring them all and in the darkness bind them.
This function is here to gather the various testing metrics of a JET file in a single number, to easily compare models.
We take a single metric for each task, and weight them based on the importance of the task (these metrics already have the same scale : between 0 and 1).
For NWP and ACP we take a top-3 metric, because these tasks usually involve a user action from a proposed list. For ACR and SWP, we take a top-1 metric, since usually it's automatically applied without user input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results |
Dict
|
Testing results. Should be a dictionary containing all the metrics (used to compute the one score). |
required |
Returns:
Type | Description |
---|---|
float
|
One score, computed from the results given. |
Source code in kebbie/scorer.py
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 |
|
tokenizer.py
Module defining BasicTokenizer
, very basic tokenizer to separate a
sentence into words.
BasicTokenizer
A basic tokenizer, used for regular latin languages. This tokenizer simply use space as word separator. Since it is used for testing only, we don't need to care about punctuations, etc...
Source code in kebbie/tokenizer.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 |
|
preprocess(sentence)
Method for simple preprocessing.
The goal of this function is not to provide an extensive and clean preprocessing. The goal is just to normalize some characters (that are not in our keyboard, so the user can't officially type them) into their normal counterpart, that are in the keyboard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sentence |
str
|
String to normalize. |
required |
Returns:
Type | Description |
---|---|
str
|
Normalized string. |
Source code in kebbie/tokenizer.py
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 |
|
word_split(sentence)
Method for splitting a sentence into a list of words.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sentence |
str
|
Sentence to split. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List of words from the sentence. |
Source code in kebbie/tokenizer.py
51 52 53 54 55 56 57 58 59 60 |
|
update_context(context, word)
Method for updating a context, given a word that was typed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
str
|
Existing context. |
required |
word |
str
|
Word being typed. |
required |
Returns:
Type | Description |
---|---|
str
|
Updated context. |
Source code in kebbie/tokenizer.py
62 63 64 65 66 67 68 69 70 71 72 |
|
utils.py
Various utils function used by kebbie
.
profile_fn(fn, *args, **kwargs)
Profile the runtime and memory usage of the given function.
Note that it will only account for memory allocated by python (if you use a library in C/C++ that does its own allocation, it won't report it).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn |
Callable
|
Function to profile. |
required |
*args |
Any
|
Positional arguments to pass to the given function. |
()
|
**kwargs |
Any
|
Keywords arguments to pass to the given function. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
The return value of the function called. |
int
|
The memory usage (in bytes). |
int
|
The runtime (in nano seconds). |
Source code in kebbie/utils.py
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 |
|
euclidian_dist(p1, p2)
Function computing the euclidian distance between 2 points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p1 |
Tuple[float, float]
|
Point 1. |
required |
p2 |
Tuple[float, float]
|
Point 2. |
required |
Returns:
Type | Description |
---|---|
float
|
Euclidian distance between the 2 given points. |
Source code in kebbie/utils.py
45 46 47 48 49 50 51 52 53 54 55 |
|
load_keyboard(lang='en-US')
Load the keyboard data for the given language.
For now, only en-US
is supported.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lang |
str
|
Language of the keyboard to load. |
'en-US'
|
Returns:
Type | Description |
---|---|
Dict
|
The keyboard data. |
Source code in kebbie/utils.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
strip_accents(s)
Util function for removing accents from a given string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s |
str
|
Accented string. |
required |
Returns:
Type | Description |
---|---|
str
|
Same string, without accent. |
Source code in kebbie/utils.py
75 76 77 78 79 80 81 82 83 84 85 |
|
sample(proba)
Simple function to sample an event with the given probability.
For example, calling sample(0.95)
will return True
in 95% cases, and
False
in 5% cases.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proba |
float
|
Probability of the event to happen. Should be between 0 and 1 (included). |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in kebbie/utils.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
sample_among(probs, with_none=True)
Function that sample an event among several with different probabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
probs |
Dict[Any, float]
|
Dictionary representing the different events and their probabilities. Each probability should be above 0 and their sum should not exceed 1. |
required |
with_none |
bool
|
If set to |
True
|
Returns:
Type | Description |
---|---|
Any
|
The corresponding key of the event sampled. |
Source code in kebbie/utils.py
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 |
|
sample_partial_word(keystrokes, word, true_word)
Sample a partial word from a given word, and extract the corresponding keystrokes as well.
Sampling is done with increasing weights (more chances to sample a longer list). For example if the list represent the keystrokes of "abcdef", the probabilities are as follow: * "a" : 1/15 * "ab" : 2/15 * "abc" : 3/15 * "abcd" : 4/15 * "abcde" : 5/15
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keystrokes |
List[Optional[Tuple[float, float]]]
|
Complete list of keystrokes, representing a full word. |
required |
word |
str
|
The word corresponding to the keystrokes. |
required |
true_word |
str
|
Actual word (without typo). Necessary to ensure the sampled keystrokes are partial. |
required |
Returns:
Type | Description |
---|---|
List[Optional[Tuple[float, float]]]
|
The partial list of keystrokes (sampled from the given word). |
str
|
The partial word (sampled from the given word). |
Source code in kebbie/utils.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
accuracy(tp, tn, fp, fn)
Function computing the precision.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tp |
int
|
Number of True Positive. |
required |
tn |
int
|
Number of True Negative. |
required |
fp |
int
|
Number of False Positive. |
required |
fn |
int
|
Number of False Negative. |
required |
Returns:
Type | Description |
---|---|
float
|
Accuracy. |
Source code in kebbie/utils.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
precision(tp, fp)
Function computing the precision.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tp |
int
|
Number of True Positive. |
required |
fp |
int
|
Number of False Positive. |
required |
Returns:
Type | Description |
---|---|
float
|
Precision. |
Source code in kebbie/utils.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
recall(tp, fn)
Function computing the recall.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tp |
int
|
Number of True Positive. |
required |
fn |
int
|
Number of False Negative. |
required |
Returns:
Type | Description |
---|---|
float
|
Recall. |
Source code in kebbie/utils.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
fbeta(precision, recall, beta=1)
Function computing the F-beta score (which is a generalization of the F1 score).
The value of Beta changes how much we weight recall versus precision
- For beta=0.5, Precision is twice as important as Recall
- For beta=2, Recall is twice as important as Precision
Parameters:
Name | Type | Description | Default |
---|---|---|---|
precision |
float
|
Precision. |
required |
recall |
float
|
Recall. |
required |
beta |
float
|
Beta factor. |
1
|
Returns:
Type | Description |
---|---|
float
|
F-beta score. |
Source code in kebbie/utils.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
round_to_n(x, n=2)
Util function to round a given number to n significant digits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
float
|
Number to round. |
required |
n |
int
|
Number of significant digits to use. |
2
|
Returns:
Type | Description |
---|---|
float
|
Rounded number. |
Source code in kebbie/utils.py
239 240 241 242 243 244 245 246 247 248 249 |
|
human_readable_memory(x)
Given a number in bytes, return a human-readable string of this number, with the right unit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
int
|
Number in bytes. |
required |
Returns:
Type | Description |
---|---|
str
|
Human-readable version of the given number, with the right unit. |
Source code in kebbie/utils.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
human_readable_runtime(x)
Given a number in nanoseconds, return a human-readable string of this number, with the right unit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
int
|
Number in nanoseconds. |
required |
Returns:
Type | Description |
---|---|
str
|
Human-readable version of the given number, with the right unit. |
Source code in kebbie/utils.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
get_soda_dataset(max_sentences=2000, seed=31)
Load the SODA dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_sentences |
int
|
Maximum number of sentences in total in
the dataset. They will be shared across domain (50% from the
|
2000
|
seed |
int
|
Seed to use when shuffling the dataset (since we don't use the whole dataset, it's better to shuffle it before extracting the X first sentences). |
31
|
Returns:
Type | Description |
---|---|
Dict[str, List[str]]
|
The dataset, separated into two domains : narrative and dialogue. |
Source code in kebbie/utils.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|