📄 select_ifile.c
字号:
/* Copyright (C) 2002 Mikael Ylikoski * See the accompanying file "README" for the full copyright notice *//** * @file * A command-line interface to Select. * This was inspired by Jason Rennie's ifile. * * @author Mikael Ylikoski * @date 2002 */#include <getopt.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include "protocol_c.h"static int text; /**< Treat input as plain text */static int numbers; /**< Use folder numbers instead of names */static char *buf; /**< Input buffer */static int blen; /**< Input buffer length */static int len; /**< Current input length */static protocol_c_data *pdata; /**< Protocol data */static struct option longopts[] = { /* ifile options */ { "help", no_argument, NULL, '?' }, { "insert", required_argument, NULL, 'i' }, { "query", no_argument, NULL, 'q' }, { "query-insert", no_argument, NULL, 'Q' }, { "update", required_argument, NULL, 'u' }, /* other options */ { "address", required_argument, NULL, 'a' }, { "numbers", no_argument, NULL, 'n' }, { "text", no_argument, NULL, 't' }, { 0, 0, 0, 0 }};static intinput_file (FILE *fd) { char ch, *str; len = 0; while ((ch = fgetc (fd)) != EOF) { buf[len] = ch; len++; if (len == blen) { str = realloc (buf, blen + 10000); if (!str) { fprintf (stderr, "Error: Out of memory\n"); return -1; } buf = str; blen += 10000; } } return 0;}static intread_file (char *file) { FILE *fd; int i; fd = fopen (file, "r"); if (!fd) return -1; i = input_file (fd); fclose (fd); return i;}static intdo_insert (char *folder) { if (protocol_c_open (pdata)) { fprintf (stderr, "Error: Cannot connect to selectd\n"); return -1; } protocol_c_part (pdata, "text", NULL, buf, len); if (protocol_c_learn_folder (pdata, folder)) { fprintf (stderr, "Error: Cannot teach selectd\n"); return -1; } protocol_c_close (pdata); return 0;}static intdo_query (void) { double val, oval; int i, j, pos; double_array *dl; str_array *sa; if (protocol_c_open (pdata)) { fprintf (stderr, "Error: Cannot connect to selectd\n"); return -1; } protocol_c_part (pdata, "text", NULL, buf, len); sa = NULL; if (!numbers) sa = protocol_c_get_table (pdata, "folders"); dl = protocol_c_classify_score (pdata, "0"); protocol_c_close (pdata); if (!dl) return -1; /* find min */ oval = 0; for (j = 0; j < dl->len; j++) if (dl->array[j] != 0 && oval >= dl->array[j]) oval = dl->array[j] - 10; /* set zeros to min */ for (j = 0; j < dl->len; j++) if (dl->array[j] == 0) dl->array[j] = oval; /* print list */ val = 0; for (i = 0; i < dl->len; i++) { pos = -1; for (j = 0; j < dl->len; j++) if (dl->array[j] >= oval && (pos == -1 || val < dl->array[j])) { val = dl->array[j]; pos = j; } dl->array[pos] = oval - 1; if (numbers) printf ("%d %.8f\n", pos, val); else printf ("%s %.8f\n", sa->array[pos], val); } printf ("---------\n"); free (dl); if (sa) { free (sa->array); free (sa); } return -1;}intmain (int argc, char **argv) { int i; int insert, query, query_insert, update; char *address; char *folder; str_array *sa; insert = 0; query = 0; query_insert = 0; text = 0; update = 0; address = NULL; folder = NULL; buf = malloc (10000); if (!buf) { printf ("Error: Out of memory!\n"); return 1; } blen = 10000; len = 0; while ((i = getopt_long (argc, argv, "a:i:nqu:", longopts, NULL)) != EOF) { switch (i) { case 'a': address = optarg; break; case 'i': folder = optarg; insert = 1; break; case 'n': numbers = 1; break; case 'q': query = 1; break; case 'Q': query_insert = 1; break; case 't': text = 1; break; case 'u': folder = optarg; insert = 1; update = 1; break; default: printf ("Usage: ...\n"); return -1; } } if (insert + query != 1) { fprintf (stderr, "Error: One (and only one) of the options -i, -u and " "-q must be given.\n"); return 1; } pdata = protocol_c_new (15000, address); /* protocol_c_open (pdata); protocol_c_open_send (pdata); sa = protocol_c_get_table (pdata, "classifiers"); protocol_c_close (pdata); */ if (update) { protocol_c_open (pdata); sa = protocol_c_get_table (pdata, "folders"); protocol_c_close (pdata); for (i = 0; i < sa->len; i++) if (strcmp (sa->array[i], folder)) break; if (i == sa->len) return 1; free (sa->array); free (sa); } if (insert) { if (optind == argc) { i = input_file (stdin); i = do_insert (folder); } else { for (; optind < argc; optind++) { i = read_file (argv[optind]); i = do_insert (folder); } } } else if (query) { if (optind == argc) { i = input_file (stdin); i = do_query (); } else { for (; optind < argc; optind++) { if (read_file (argv[optind])) { fprintf (stderr, "Error: Cannot read file\n"); continue; } i = do_query (); } } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -