[tracker: 1/2] tracker:strip-punctuation function created function will match the regex of punctuation and replace




commit ecfbc7bdda83bc13b5c9fed98be7052e43e49657
Author: Nishit Patel <nishitlimbani130 gmail com>
Date:   Thu Dec 10 14:37:57 2020 +0000

    tracker:strip-punctuation function created
    function will match the regex of punctuation and replace it with empty
    space

 .../libtracker-sparql/sparql-functions.xml         | 10 ++++++++
 src/libtracker-data/tracker-db-interface-sqlite.c  | 27 ++++++++++++++++++++++
 src/libtracker-data/tracker-sparql.c               |  5 ++++
 .../functions/functions-tracker-9.out              |  1 +
 .../functions/functions-tracker-9.rq               |  1 +
 tests/libtracker-data/tracker-sparql-test.c        |  1 +
 6 files changed, 45 insertions(+)
---
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 },


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]