| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: scorer_tf_idf |
| 5 |
|
| 6 |
``scorer_tf_idf`` |
| 7 |
================= |
| 8 |
|
| 9 |
.. versionadded:: 5.0.1 |
| 10 |
|
| 11 |
Summary |
| 12 |
------- |
| 13 |
|
| 14 |
``scorer_tf_idf`` is a scorer based of `TF-IDF |
| 15 |
<https://en.wikipedia.org/wiki/Tf%E2%80%93idf>`_ (term |
| 16 |
frequency-inverse document frequency) score function. |
| 17 |
|
| 18 |
To put it simply, TF (term frequency) divided by DF (document |
| 19 |
frequency) is TF-IDF. "TF" means that "the number of occurrences is |
| 20 |
more important". "TF divided by DF" means that "the number of |
| 21 |
occurrences of important term is more important". |
| 22 |
|
| 23 |
The default score function in Groonga is TF (term frequency). It |
| 24 |
doesn't care about term importance but is fast. |
| 25 |
|
| 26 |
TF-IDF cares about term importance but is slower than TF. |
| 27 |
|
| 28 |
TF-IDF will compute more suitable score rather than TF for many cases. |
| 29 |
But it's not perfect. |
| 30 |
|
| 31 |
If document contains many same keywords such as "They are keyword, |
| 32 |
keyword, keyword ... and keyword", it increases score by TF and |
| 33 |
TF-IDF. Search engine spammer may use the technique. But TF-IDF |
| 34 |
doesn't guard from the technique. |
| 35 |
|
| 36 |
`Okapi BM25 <https://en.wikipedia.org/wiki/Okapi_BM25>`_ can solve the |
| 37 |
case. But it's more slower than TF-IDF and not implemented yet in |
| 38 |
Groonga. |
| 39 |
|
| 40 |
Groonga provides :doc:`scorer_tf_at_most` scorer that can also solve |
| 41 |
the case. |
| 42 |
|
| 43 |
.. include:: ../scoring_note.rst |
| 44 |
|
| 45 |
Syntax |
| 46 |
------ |
| 47 |
|
| 48 |
This scorer has only one parameter:: |
| 49 |
|
| 50 |
scorer_tf_idf(column) |
| 51 |
scorer_tf_idf(index) |
| 52 |
|
| 53 |
Usage |
| 54 |
----- |
| 55 |
|
| 56 |
This section describes how to use this scorer. |
| 57 |
|
| 58 |
Here are a schema definition and sample data to show usage. |
| 59 |
|
| 60 |
Sample schema: |
| 61 |
|
| 62 |
.. groonga-command |
| 63 |
.. include:: ../../example/reference/scorers/scorer_tf_idf/usage_setup_schema.log |
| 64 |
.. table_create Logs TABLE_NO_KEY |
| 65 |
.. column_create Logs message COLUMN_SCALAR Text |
| 66 |
.. |
| 67 |
.. table_create Terms TABLE_PAT_KEY ShortText \ |
| 68 |
.. --default_tokenizer TokenBigram \ |
| 69 |
.. --normalizer NormalizerAuto |
| 70 |
.. column_create Terms message_index COLUMN_INDEX|WITH_POSITION Logs message |
| 71 |
|
| 72 |
Sample data: |
| 73 |
|
| 74 |
.. groonga-command |
| 75 |
.. include:: ../../example/reference/scorers/scorer_tf_idf/usage_setup_data.log |
| 76 |
.. load --table Logs |
| 77 |
.. [ |
| 78 |
.. {"message": "Error"}, |
| 79 |
.. {"message": "Warning"}, |
| 80 |
.. {"message": "Warning Warning"}, |
| 81 |
.. {"message": "Warning Warning Warning"}, |
| 82 |
.. {"message": "Info"}, |
| 83 |
.. {"message": "Info Info"}, |
| 84 |
.. {"message": "Info Info Info"}, |
| 85 |
.. {"message": "Info Info Info Info"}, |
| 86 |
.. {"message": "Notice"}, |
| 87 |
.. {"message": "Notice Notice"}, |
| 88 |
.. {"message": "Notice Notice Notice"}, |
| 89 |
.. {"message": "Notice Notice Notice Notice"}, |
| 90 |
.. {"message": "Notice Notice Notice Notice Notice"} |
| 91 |
.. ] |
| 92 |
|
| 93 |
You specify ``scorer_tf_idf`` in :ref:`select-match-columns` like the |
| 94 |
following: |
| 95 |
|
| 96 |
.. groonga-command |
| 97 |
.. include:: ../../example/reference/scorers/scorer_tf_idf/usage_no_weight.log |
| 98 |
.. select Logs \ |
| 99 |
.. --match_columns "scorer_tf_idf(message)" \ |
| 100 |
.. --query "Error OR Info" \ |
| 101 |
.. --output_columns "message, _score" \ |
| 102 |
.. --sort_keys "-_score" |
| 103 |
|
| 104 |
Both the score of ``Info Info Info`` and the score of ``Error`` are |
| 105 |
``2`` even ``Info Info Info`` includes three ``Info`` terms. Because |
| 106 |
``Error`` is more important term rather than ``Info``. The number of |
| 107 |
documents that include ``Info`` is ``4``. The number of documents that |
| 108 |
include ``Error`` is ``1``. Term that is included in less documents |
| 109 |
means that the term is more characteristic term. Characteristic term |
| 110 |
is important term. |
| 111 |
|
| 112 |
Parameters |
| 113 |
---------- |
| 114 |
|
| 115 |
This section describes all parameters. |
| 116 |
|
| 117 |
Required parameters |
| 118 |
^^^^^^^^^^^^^^^^^^^ |
| 119 |
|
| 120 |
There is only one required parameter. |
| 121 |
|
| 122 |
``column`` |
| 123 |
"""""""""" |
| 124 |
|
| 125 |
The data column that is match target. The data column must be indexed. |
| 126 |
|
| 127 |
``index`` |
| 128 |
""""""""" |
| 129 |
|
| 130 |
The index column to be used for search. |
| 131 |
|
| 132 |
Optional parameters |
| 133 |
^^^^^^^^^^^^^^^^^^^ |
| 134 |
|
| 135 |
There is no optional parameter. |
| 136 |
|
| 137 |
Return value |
| 138 |
------------ |
| 139 |
|
| 140 |
This scorer returns score as :ref:`builtin-type-float`. |
| 141 |
|
| 142 |
:doc:`/reference/commands/select` returns ``_score`` as ``Int32`` not |
| 143 |
``Float``. Because it casts to ``Int32`` from ``Float`` for keeping |
| 144 |
backward compatibility. |
| 145 |
|
| 146 |
Score is computed as TF-IDF based algorithm. |
| 147 |
|
| 148 |
See also |
| 149 |
-------- |
| 150 |
|
| 151 |
* :doc:`../scorer` |