Simple utility to turn swipes into words -- "plugin" for wvkbd to enable swipe-typing under wayland SXMO.
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | Makefile | 37 | ||||
| -rw-r--r-- | README.md | 56 | ||||
| -rw-r--r-- | map.qwerty.noapos.tsv (renamed from map.qwerty.simplegrid.tsv) | 2 | ||||
| -rw-r--r-- | mapScore.c | 15 | ||||
| -rw-r--r-- | swipeGuess.1.scd | 8 | ||||
| -rw-r--r-- | swipeGuess.c | 10 |
7 files changed, 119 insertions, 14 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d9369d --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +mapScore +swipeGuess +swipeGuess.1 +mapScore.1 +words-qwerty-en diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eeb0d6c --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +PREFIX?=/usr/local + +all: swipeGuess mapScore docs words-qwerty-en +swipeGuess: swipeGuess.c + $(CC) swipeGuess.c -o swipeGuess +mapScore: mapScore.c + $(CC) mapScore.c -o mapScore +docs: swipeGuess.1 mapScore.1 +swipeGuess.1: swipeGuess.1.scd + scdoc < swipeGuess.1.scd > $@.tmp + mv $@.tmp $@ +mapScore.1: mapScore.1.scd + scdoc < mapScore.1.scd > $@.tmp + mv $@.tmp $@ + +words-qwerty-en: /usr/share/dict/american-english mapScore + grep .. /usr/share/dict/american-english | ./mapScore map.qwerty.noapos.tsv bee | sort -nr | cut -f2 > words-qwerty-en +/usr/share/dict/american-english: + apk add words-en + +test: words-qwerty-en swipeGuess + test "`echo "asdfghjkl" | ./swipeGuess words-qwerty-en`" = "all" + test "`echo "dfghuiokmnhyt" | ./swipeGuess words-qwerty-en 1 "'"`" = "don't" + test "`echo "tyuiopoiuytrewertyuiuytrer" | ./swipeGuess words-qwerty-en 2`" = "`printf "typewriter\ttorturer"`" + +install: all + install -m755 swipeGuess -D -t "$(DESTDIR)/$(PREFIX)/bin/" + install -m755 mapScore -D -t "$(DESTDIR)/$(PREFIX)/bin/" + install -m644 swipeGuess.1 -D -t "$(DESTDIR)/$(PREFIX)/share/man/man1/" + install -m644 mapScore.1 -D -t "$(DESTDIR)/$(PREFIX)/share/man/man1/" + install -m644 words-qwerty-en -D -t "$(DESTDIR)/$(PREFIX)/share/swipeGuess/words/" +uninstall: + rm -f "$(DESTDIR)/$(PREFIX)/bin/swipeGuess" + rm -f "$(DESTDIR)/$(PREFIX)/bin/mapScore" + rm -f "$(DESTDIR)/$(PREFIX)/share/man/man1/swipeGuess.1" + rm -f "$(DESTDIR)/$(PREFIX)/share/man/man1/mapScore.1" + rm -f "$(DESTDIR)/$(PREFIX)/share/swipeGuess/words/words-qwerty-en" @@ -6,11 +6,13 @@ For each line input from stdin, it looks through a wordlist and outputs the firs it's run like `input-program | swipeGuess wordlist.txt | output-program` +swipeGuess also provides options for returning multiple results and ignoring certain characters. See [the man page](https://git.sr.ht/~earboxer/swipeGuess/tree/master/item/swipeGuess.1.scd) for more information. + ## input-program The input program should output a stream of letters "swiped through", then a newline. -This is supported by [wvkbd](https://github.com/proycon/wvkbd) since version 0.6. +This is supported by [wvkbd](https://github.com/proycon/wvkbd) since version 0.6 and [phosh-osk-stub](https://gitlab.gnome.org/guidog/phosh-osk-stub) since 0.28.0. ## wordlist @@ -25,6 +27,7 @@ Good starting points can be found on the web, based on your language. * https://raw.githubusercontent.com/dwyl/english-words/master/words.txt - 466,550 words, including many proper nouns like names. * https://norvig.com/ngrams/count_1w.txt - 333,333 words, abbreviations, etc, sorted by popularity. * https://www.keithv.com/software/wlist/index.php - page with several links providing intersections between english word lists (26,680 - 1,516,998 words) +* Your computer may have a list installed already in /usr/share/dict/, or one may be installable through your package manager. ### sorting tips @@ -36,16 +39,18 @@ ls $PATH | awk '{ print length, $0 }' | sort -nr | cut -d" " -f2- > binsSorted.t ``` -Smarter sorting would be keyboard-layout aware. mapscore.py can do that for you +Smarter sorting would be keyboard-layout aware. mapScore can do that for you ```sh -./mapScore.py map.tsv <words.txt | sort -nr | cut -f2 > wordsSorted.txt +./mapScore map.tsv <words.txt | sort -nr | cut -f2 > wordsSorted.txt ``` -map.tsv uses tabs and newlines to create the grid-based layout. See `map.qwerty.simplegrid.tsv` for a sample of how to format this file. +map.tsv uses tabs and newlines to create the grid-based layout. See `map.qwerty.noapos.tsv` for a sample of how to format this file. If your keys are in a hexagonal layout, use mapScore like -`./mapScore.py map.simple.tsv bee`. +`./mapScore map.simple.tsv bee`. + +(`mapScore.py` was the old version of this, and probably should be removed, being slower and with less features.). ## output-program @@ -53,8 +58,45 @@ If your keys are in a hexagonal layout, use mapScore like # Installation/Usage with wvkbd -1. Be using a wayland-based graphical shell (such as sway) +1. Be using a wayland-based graphical shell (such as sway), and have wtype installed. 2. Compile with your favorite C compiler: `gcc swipeGuess.c -o swipeGuess`. -2. copy swipeGuess completelyTypeWord.sh into your $PATH (`~/.local/bin/` or `/usr/local/bin/` for example) +2. copy swipeGuess and completelyTypeWord.sh into your $PATH (`~/.local/bin/` or `/usr/local/bin/` for example) 3. `wvkbd-mobintl -O | swipeGuess /path/to/words.txt | completelyTypeWord.sh` * In SXMO, `KEYBOARD_ARGS='-O | swipeGuess /path/to/words.txt | completelyTypeWord.sh'` can be added to your ~/.profile to enable this (effective on restart). + +# Usage with phosh-osk-stub + +``` +gsettings set sm.puri.phosh.osk osk-features "['key-drag']" +gsettings set sm.puri.phosh.osk.Completers.Pipe command 'swipeGuess /usr/share/swipeGuess/words/words-qwerty-en 5 | tr "\t" "\n"' +gsettings set sm.puri.phosh.osk.Completers default pipe +gsettings set sm.puri.phosh.osk completion-mode "['manual','hint']" +``` + +## Multiple suggestions + +phosh-osk-stub's pipe completer accepts multiple suggestions, newline separated. This can be scripted like + +``` +gsettings set sm.puri.phosh.osk.Completers.Pipe command 'sh -c "swipeGuess /usr/share/swipeGuess/words/words-qwerty-en 5 | tr \"\t\" \"\n\""' +``` + +# Extended information + +[SwipeBehaviors](https://git.sr.ht/~earboxer/SwipeBehaviors) is a project that uses swipeGuess and provides more advanced functionality, like presenting several choices that can be picked with [suggpicker](https://git.sr.ht/~earboxer/suggpicker). + +# Contributing + +swipeGuess is maintained by [Zach DeCook](https://zachdecook.com/), who may or may not be reached directly for related inquiries. + +Patches and long-form discussions for this project are also accepted on my swipeKeyboard mailing list on sourcehut: ([email](mailto:~earboxer/swipeKeyboard@lists.sr.ht)/[archive](https://lists.sr.ht/~earboxer/swipeKeyboard)). + +e.g. + +``` +git config sendemail.to '~earboxer/swipeKeyboard@lists.sr.ht' +git config format.subjectPrefix 'PATCH swipeGuess' +git send-email HEAD~1 +``` + +(See `man git-send-email` or https://git-send-email.io for more information) diff --git a/map.qwerty.simplegrid.tsv b/map.qwerty.noapos.tsv index 542ec1b..905bb74 100644 --- a/map.qwerty.simplegrid.tsv +++ b/map.qwerty.noapos.tsv @@ -1,4 +1,4 @@ q w e r t y u i o p -a s d f g h j k l ' +a s d f g h j k l z x c v b n m , . @@ -1,10 +1,11 @@ #include <stdio.h> #include <stdlib.h> +#include <ctype.h> #define BUFSIZE 1024 char buffer[BUFSIZE]; // TODO: Support UTF-8. -int map[256][2]; +int map[256][3]; void makeMap(FILE *f) { int x = 0; int y = 0; @@ -14,8 +15,12 @@ void makeMap(FILE *f) { case '\t': x++; break; case '\n': y++; x=0; break; default: + explicit_label: map[c][0] = x; map[c][1] = y; + map[c][2] = 1; + c = toupper(c); + if (c < 256 && map[c][2] == 0) goto explicit_label; break; } } @@ -24,9 +29,13 @@ void makeMap(FILE *f) { int scoreWord(char *word, int (*fun)(int*,int*)) { char *c = word; int score = 0; + char *p = c; for (c++;*c;c++) { if (*c == '\n') break; - score += (*fun)(map[c[-1]],map[c[0]]); + // pass over ignore unset chars. + if(map[(unsigned char)*c][3] == 0) continue; + score += (*fun)(map[(unsigned char)*p],map[(unsigned char)*c]); + p=c; } return score; } @@ -53,7 +62,7 @@ int beeDist(int *p1, int *p2) { int main(int argc, char **argv) { if (argc < 2) { - fprintf(stderr, "Usage: mapScore map.tsv <words.txt >scoredWords.tsv"); + fprintf(stderr, "Usage: mapScore map.tsv <words.txt >scoredWords.tsv\n"); return 1; } diff --git a/swipeGuess.1.scd b/swipeGuess.1.scd index 01d02c7..25c58bb 100644 --- a/swipeGuess.1.scd +++ b/swipeGuess.1.scd @@ -5,19 +5,25 @@ swipeGuess - infer the word you meant from a swipe # SYNOPSIS -*swipeGuess* _words.txt_ [_n_] < _input_swipes.txt_ > _output_guesses.tsv_ +*swipeGuess* _words.txt_ [_n_] [_ignorechars_] < _input_swipes.txt_ > _output_guesses.tsv_ wvkbd -O | *swipeGuess* _words.txt_ | completelyTypeWord.sh wvkbd -O | *swipeGuess* _words.txt_ 5 | suggpicker | completelyTypeWord.sh +wvkbd -O | *swipeGuess* _words.txt_ 1 "'" | completelyTypeWord.sh + # OPTIONS _n_ Number of results that should be returned (default 1). +_ignorechars_ + Characters that exist in _words.txt_, but won't exist in your swipes, that you still want to type. + # DESCRIPTION For each line of stdin, outputs the first _n_ results from _words.txt_ which could be an ordered subset of that line, separated by tabs. +If _ignorechars_ is given, the results may have these characters inside them even when not present in the input. # SEE ALSO *suggpicker*(1) *wvkbd*(1) *SwipeBehavior*(7) diff --git a/swipeGuess.c b/swipeGuess.c index 41fb29c..b13e5b5 100644 --- a/swipeGuess.c +++ b/swipeGuess.c @@ -6,6 +6,7 @@ #define BUFSIZE 1024 char wordBuff[BUFSIZE]; char swipeBuff[BUFSIZE]; +bool ignorechars[256] = {false}; // fgets, but without the newline. char *fgetst(char *restrict s, int size, FILE *restrict stream) { @@ -35,7 +36,7 @@ bool swipeCompare(char *swipe, char *word) { bool lastMatch = false; for(swipeP++; swipeP[0]; swipeP++) { lastMatch = false; - while (charcmp(swipeP[0], wordP[0])) { + while (charcmp(swipeP[0], wordP[0]) || ignorechars[wordP[0]]) { wordP++; lastMatch = true; } @@ -63,13 +64,18 @@ void query(FILE *wordFile, char *swipe, int n) { int main(int argc, char **argv) { if (argc < 2) { - fprintf(stderr, "Usage: swipeGuess words.txt [n]"); + fprintf(stderr, "Usage: swipeGuess words.txt [n]\n"); exit(1); } int n = 1; if (argc >= 3) { n = atoi(argv[2]); } + if (argc >= 4) { + for(int i=0; argv[3][i];i++){ + ignorechars[(int)argv[3][0]] = true; + } + } FILE *wordFile = fopen(argv[1], "r"); while (fgetst(swipeBuff, BUFSIZE, stdin)) { query(wordFile, swipeBuff, n); |