about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZach DeCook <zachdecook@librem.one>2022-05-16 21:16:16 -0400
committerZach DeCook <zachdecook@librem.one>2022-05-16 21:16:16 -0400
commit696ae85a536b6d0c33d39ee86901607208562659 (patch)
treecfc308d943a13bc22d9f06476a63a55b48b24c78
parenta09679604a5db635d6598bd996a44cdfd43e7f57 (diff)
downloadSwipeBehaviors-696ae85a536b6d0c33d39ee86901607208562659.tar.gz
predictor: replace bottleneck with c program
-rwxr-xr-xpredictor.sh7
-rw-r--r--quick5.c49
2 files changed, 52 insertions, 4 deletions
diff --git a/predictor.sh b/predictor.sh
index b59e1fa..91e7ffc 100755
--- a/predictor.sh
+++ b/predictor.sh
@@ -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
diff --git a/quick5.c b/quick5.c
new file mode 100644
index 0000000..7391a77
--- /dev/null
+++ b/quick5.c
@@ -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;
+}