~earboxer/SwipeBehaviors

696ae85a536b6d0c33d39ee86901607208562659 — Zach DeCook 4 months ago a096796
predictor: replace bottleneck with c program
2 files changed, 52 insertions(+), 4 deletions(-)

M predictor.sh
A quick5.c
M predictor.sh => predictor.sh +3 -4
@@ 11,9 11,8 @@ saveWord(){
	while read -r word; do
		printf "$word" > "$lw"
		echo "$word"
		end="$(printf "$word."|wc -c)"
		rm -f "$complete"
		grep -m 5 "^$word." "$words" | cut -b$end-99 |tr '\n' '\t' |sed 's/\t$/\n/g' > "$sock"
		cat "$words" "$twow" | quick5 "$word" | sed 's/\t/ \t/g'|sed 's/$/ /g' >> "$sock"
	done
}
typeSomehow(){


@@ 34,9 33,9 @@ while read -r swipe; do
	else
		if test "$swipe" = ""; then
			if test -e "$lw"; then
				printf " " >> "$lw"
				rm -f "$complete"
				word="$(cat "$lw")"
				grep -i -m 5 "^$word " "$twow" |awk '(1){printf $2; i++}(i<5){printf "\t"}END{print ""}' >> "$sock"
				cat "$twow" | quick5 "$(cat "$lw")" >> "$sock"
				rm -f "$lw"
			fi
		elif test "$swipe" = "$(echo -e "\x08")"; then

A quick5.c => quick5.c +49 -0
@@ 0,0 1,49 @@
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

#define BUFSIZE 1024
char wordBuff[BUFSIZE];

// fgets, but without the newline/tab.
char *fgetst(char *restrict s, int size, FILE *restrict stream) {
	char *r = fgets(s, size, stream);
	if (r) {
		char * sp = s;
		for(; sp[0] != '\0'; sp++) {
			if (sp[0] == '\n' || sp[0] == '\t') {
				sp[0] = '\0';
			}
		}
	}
	return r;
}

bool strdif(char* a, char* b) {
	for (;a[0];) {
		if (tolower(a[0]) != tolower(b[0])){
			return true;
		}
		a++;
		b++;
		if (!b[0])return true;
	}
	return false;
}

int main(int argc, char **argv) {
	if(argc!=2)return 1;
	int found = 0;
	size_t len = strlen(argv[1]);

	while (found < 5 && fgetst(wordBuff, BUFSIZE, stdin)) {
		if (!strdif(argv[1], wordBuff)) {
			if (found > 0) printf("\t");
			printf("%s", wordBuff+len);
			found++;
		}
	}
	printf("\n");
	return 0;
}