diff options
author | Nishit Patel <nishitlimbani130@gmail.com> | 2020-12-10 14:37:57 +0000 |
---|---|---|
committer | Sam Thursfield <sam@afuera.me.uk> | 2020-12-10 14:37:57 +0000 |
commit | ecfbc7bdda83bc13b5c9fed98be7052e43e49657 (patch) | |
tree | 6c32b96a7655a002cfc1b517094d1ccdba51dae8 | |
parent | eeb0330bb4fbc9f7393d43fa91b86f753bdb7d39 (diff) | |
download | tracker-ecfbc7bdda83bc13b5c9fed98be7052e43e49657.tar.gz |
tracker:strip-punctuation function created
function will match the regex of punctuation and replace it with empty
space
-rw-r--r-- | docs/reference/libtracker-sparql/sparql-functions.xml | 10 | ||||
-rw-r--r-- | src/libtracker-data/tracker-db-interface-sqlite.c | 27 | ||||
-rw-r--r-- | src/libtracker-data/tracker-sparql.c | 5 | ||||
-rw-r--r-- | tests/libtracker-data/functions/functions-tracker-9.out | 1 | ||||
-rw-r--r-- | tests/libtracker-data/functions/functions-tracker-9.rq | 1 | ||||
-rw-r--r-- | tests/libtracker-data/tracker-sparql-test.c | 1 |
6 files changed, 45 insertions, 0 deletions
diff --git a/docs/reference/libtracker-sparql/sparql-functions.xml b/docs/reference/libtracker-sparql/sparql-functions.xml index 8fdf3f07e..6822df569 100644 --- a/docs/reference/libtracker-sparql/sparql-functions.xml +++ b/docs/reference/libtracker-sparql/sparql-functions.xml @@ -214,6 +214,16 @@ Picks the first non-null value. Equivalent to <systemitem>COALESCE</systemitem>. </para> + + </sect2> + <sect2 id="tracker:strip-punctuation"> + <title>tracker:strip-punctuation</title> + <programlisting> + tracker:strip-punctuation (?string) + </programlisting> + <para> + Removes any Unicode character which has the General Category value of P (Punctuation) from the string. + </para> </sect2> </chapter> diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c b/src/libtracker-data/tracker-db-interface-sqlite.c index f5e91de29..ca058b510 100644 --- a/src/libtracker-data/tracker-db-interface-sqlite.c +++ b/src/libtracker-data/tracker-db-interface-sqlite.c @@ -1292,6 +1292,31 @@ function_sparql_case_fold (sqlite3_context *context, sqlite3_result_text16 (context, zOutput, -1, sqlite3_free); } +static void +function_sparql_strip_punctuation (sqlite3_context *context, + int argc, + sqlite3_value *argv[]) +{ + const gchar *fn = "tracker:strip-punctuation"; + gchar *input, *replacement = "", *output = NULL; + GError *error = NULL; + GRegex *regex; + input = (gchar *)sqlite3_value_text (argv[0]); + const gchar *pattern = "\\p{P}"; + + regex = g_regex_new (pattern, 0, 0, &error); + if (error) + { + result_context_function_error (context, fn, error->message); + g_clear_error (&error); + return; + } + + output = g_regex_replace (regex, input, -1, 0, replacement, 0, &error); + + sqlite3_result_text (context, output, -1, g_free); +} + static gunichar2 * normalize_string (const gunichar2 *string, gsize string_len, /* In gunichar2s */ @@ -1970,6 +1995,8 @@ initialize_functions (TrackerDBInterface *db_interface) function_sparql_upper_case }, { "SparqlCaseFold", 1, SQLITE_ANY | SQLITE_DETERMINISTIC, function_sparql_case_fold }, + {"SparqlStripPunctuation", 1, SQLITE_ANY | SQLITE_DETERMINISTIC, + function_sparql_strip_punctuation }, { "SparqlNormalize", 2, SQLITE_ANY | SQLITE_DETERMINISTIC, function_sparql_normalize }, { "SparqlUnaccent", 1, SQLITE_ANY | SQLITE_DETERMINISTIC, diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c index 42bed5907..2a3e8c17b 100644 --- a/src/libtracker-data/tracker-sparql.c +++ b/src/libtracker-data/tracker-sparql.c @@ -7774,6 +7774,11 @@ handle_custom_function (TrackerSparql *sparql, _append_string (sparql, "SparqlCaseFold ("); _call_rule (sparql, NAMED_RULE_ArgList, error); _append_string (sparql, ") "); + } else if (g_str_equal (function, TRACKER_NS "strip-punctuation")) { + sparql->current_state->convert_to_string = TRUE; + _append_string (sparql, "SparqlStripPunctuation ("); + _call_rule (sparql, NAMED_RULE_ArgList, error); + _append_string (sparql, ") "); } else if (g_str_equal (function, TRACKER_NS "title-order")) { _call_rule (sparql, NAMED_RULE_ArgList, error); _append_string (sparql, "COLLATE " TRACKER_TITLE_COLLATION_NAME " "); diff --git a/tests/libtracker-data/functions/functions-tracker-9.out b/tests/libtracker-data/functions/functions-tracker-9.out new file mode 100644 index 000000000..612672dd7 --- /dev/null +++ b/tests/libtracker-data/functions/functions-tracker-9.out @@ -0,0 +1 @@ +"hello World" diff --git a/tests/libtracker-data/functions/functions-tracker-9.rq b/tests/libtracker-data/functions/functions-tracker-9.rq new file mode 100644 index 000000000..a011f9a61 --- /dev/null +++ b/tests/libtracker-data/functions/functions-tracker-9.rq @@ -0,0 +1 @@ +SELECT (tracker:strip-punctuation ('hello, World!?') AS ?str) {}
\ No newline at end of file diff --git a/tests/libtracker-data/tracker-sparql-test.c b/tests/libtracker-data/tracker-sparql-test.c index 9f4d68fc9..c2b7d6225 100644 --- a/tests/libtracker-data/tracker-sparql-test.c +++ b/tests/libtracker-data/tracker-sparql-test.c @@ -122,6 +122,7 @@ const TestInfo tests[] = { { "functions/functions-tracker-6", "functions/data-2", FALSE }, { "functions/functions-tracker-7", "functions/data-2", FALSE }, { "functions/functions-tracker-8", "functions/data-2", FALSE }, + { "functions/functions-tracker-9", "functions/data-2", FALSE }, { "functions/functions-tracker-loc-1", "functions/data-3", FALSE }, { "functions/functions-xpath-1", "functions/data-1", FALSE }, { "functions/functions-xpath-2", "functions/data-1", FALSE }, |