nextmatch

Find close matches to a line in a quote file, and print the next one.
git clone git://git.vgx.fr/nextmatch
Log | Files | Refs | Submodules

commit f95d689b389822c1a3a1eb4f23bc1747bafcba6b
Author: Léo Villeveygoux <leo.villeveygoux@etu.u-bordeaux.fr>
Date:   Fri,  2 Dec 2016 00:01:53 +0100

initial commit

Diffstat:
A.gitignore | 5+++++
AMakefile | 4++++
Anextmatch.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 66 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,5 @@ +nextmatch +*.o + +*~ +*.swp diff --git a/Makefile b/Makefile @@ -0,0 +1,4 @@ +CFLAGS+=-D LEV_CASE_INSENSITIVE + +nextmatch: levenshtein/levenshtein.o + diff --git a/nextmatch.c b/nextmatch.c @@ -0,0 +1,57 @@ +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "levenshtein/levenshtein.h" + +#define MINSCORE 0.7 + +void replace(char **dest, const char *src) { + if (*dest) + free(*dest); + *dest = strdup(src); +} + +int main(int argc, char *argv[]) { + FILE *fp; + char *line = NULL, *key = NULL, *next = NULL; + double best_score = MINSCORE; + bool get_next = false; + size_t len = 0; + ssize_t read; + + // we could maybe take a file as an argument + fp = stdin; + + key = argv[1]; + + if (fp == NULL || argc != 2 || key == NULL) + exit(EXIT_FAILURE); + + while ((read = getline(&line, &len, fp)) != -1) { + if (get_next) { + replace(&next, line); + get_next = false; + } + + int dist = levenshtein(key, line) - 1; // -1 is for the trailing \n + + double score = (read - dist) / (double)read; + + if (score > best_score) { + + fprintf(stderr, "candidate (%lf = (%d-%d)/%d) : %s\n", score, read, dist, + read, line); + get_next = true; + } + } + + if (next) { + printf("%s", next); + free(next); + } + + free(line); + return EXIT_SUCCESS; +}