1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*
* Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <string.h>
#include <locale.h>
#include <glib.h>
#include <gio/gio.h>
#include <libtracker-common/tracker-common.h>
/* Normally this would be in the libtracker-fts config */
#define DEFAULT_MAX_WORD_LENGTH 30
#define DEFAULT_ENABLE_STEMMER FALSE
#define DEFAULT_ENABLE_UNACCENT TRUE
#define DEFAULT_IGNORE_STOP_WORDS TRUE
#define DEFAULT_IGNORE_NUMBERS TRUE
static gchar *text;
static gchar *filename;
static gboolean verbose;
/* Command Line options */
static const GOptionEntry options [] = {
{
"verbose", 'v', G_OPTION_FLAG_NO_ARG,
G_OPTION_ARG_NONE, &verbose,
"Enable verbose output",
NULL
},
{
"text", 't', 0,
G_OPTION_ARG_STRING, &text,
"Specific text to parse",
NULL
},
{
"file", 'f', 0,
G_OPTION_ARG_STRING, &filename,
"Specific file to parse its contents",
NULL
},
{ NULL }
};
static gboolean
setup_context (gint argc,
gchar **argv)
{
GOptionContext *context = NULL;
GError *error = NULL;
/* Setup command line options */
context = g_option_context_new ("- Test the Tracker FTS parser");
g_option_context_add_main_entries (context,
options,
argv[0]);
/* Parse input arguments */
if (!g_option_context_parse (context,
&argc,
&argv,
&error))
{
g_printerr ("%s\nRun '%s --help' to see a full list of available "
"command line options.\n",
error->message,
argv[0]);
g_error_free (error);
return FALSE;
}
g_option_context_free (context);
return TRUE;
}
static gboolean
load_file_contents (void)
{
GError *error = NULL;
GFile *file;
file = g_file_new_for_commandline_arg (filename);
if (!g_file_load_contents (file, NULL, &text, NULL, NULL, &error)) {
g_printerr ("Error loading file '%s' contents: '%s'\n",
filename,
error->message);
g_error_free (error);
g_object_unref (file);
return FALSE;
}
g_object_unref (file);
return TRUE;
}
static gboolean
run_parsing (void)
{
TrackerLanguage *language;
TrackerParser *parser;
GTimer *timer;
/* Initialize timing */
timer = g_timer_new ();
/* Setup language for parser */
language = tracker_language_new (NULL);
if (!language) {
g_printerr ("Language setup failed!\n");
return FALSE;
}
/* Create the parser */
parser = tracker_parser_new (language);
if (!parser) {
g_printerr ("Parser creation failed!\n");
g_object_unref (language);
return FALSE;
}
/* Reset the parser with our string, reading the current FTS config */
tracker_parser_reset (parser,
text,
strlen (text),
DEFAULT_MAX_WORD_LENGTH,
DEFAULT_ENABLE_STEMMER,
DEFAULT_ENABLE_UNACCENT,
DEFAULT_IGNORE_STOP_WORDS,
TRUE,
DEFAULT_IGNORE_NUMBERS);
/* Loop through all words! */
while (1) {
const gchar *word;
gint position;
gint byte_offset_start;
gint byte_offset_end;
gboolean stop_word;
gint word_length;
/* Process next word */
word = tracker_parser_next (parser,
&position,
&byte_offset_start,
&byte_offset_end,
&stop_word,
&word_length);
/* Stop loop if no more words */
if (!word) {
break;
}
if (verbose) {
gchar *word_hex;
gchar *original_word;
gchar *original_word_hex;
gint original_word_length;
/* Get original word */
original_word_length = byte_offset_end - byte_offset_start;
original_word = g_malloc (original_word_length + 1);
memcpy (original_word,
&text[byte_offset_start],
original_word_length);
original_word[original_word_length] = '\0';
/* Get hex strings */
word_hex = tracker_strhex ((guint8 *)word, word_length, ':');
original_word_hex = tracker_strhex ((guint8 *)original_word,
original_word_length,
':');
g_print ("WORD at %d [%d,%d] Original: '%s' (%s), "
"Processed: '%s' (%s) (stop? %s)\n",
position,
byte_offset_start,
byte_offset_end,
original_word,
original_word_hex,
word,
word_hex,
stop_word ? "yes" : "no");
g_free (word_hex);
g_free (original_word_hex);
g_free (original_word);
}
}
g_print ("\n----> Parsing finished after '%lf' seconds\n",
g_timer_elapsed (timer, NULL));
g_timer_destroy (timer);
tracker_parser_free (parser);
g_object_unref (language);
return TRUE;
}
int
main (int argc, char **argv)
{
/* Setup locale */
setlocale (LC_ALL, "");
/* Setup context */
if (!setup_context (argc, argv)) {
g_printerr ("Context setup failed... exiting\n");
return -1;
}
/* Either text or file must be given */
if (filename == NULL &&
text == NULL) {
g_printerr ("Either 'file' or 'text' options should be used\n"
"Run '%s --help' to see a full list of available "
"command line options.\n",
argv[0]);
return -2;
}
/* If required, load file contents */
if (filename != NULL &&
!load_file_contents ()) {
g_printerr ("Loading file '%s' contents failed... exiting\n",
filename);
return -3;
}
/* Run the parsing! */
if (!run_parsing ()) {
g_printerr ("Parsing operation failed... exiting\n");
return -4;
}
/* Clean exit */
if (filename)
g_free (text);
return 0;
}
|