| 1 |
========================= |
| 2 |
Custom language detection |
| 3 |
========================= |
| 4 |
|
| 5 |
`dateparser` allows to customize the language detection behavior by using the ``detect_languages_function`` parameter. |
| 6 |
It currently supports two language detection libraries out of the box: `fastText <https://github.com/facebookresearch/fastText>`_ |
| 7 |
and `langdetect <https://github.com/Mimino666/langdetect>`_, and allows you to implement your own custom language detection. |
| 8 |
|
| 9 |
.. warning:: |
| 10 |
|
| 11 |
For short strings the language detection could fail, so it's highly recommended to use ``detect_languages_function`` |
| 12 |
along with ``DEFAULT_LANGUAGES``. |
| 13 |
|
| 14 |
Built-in implementations |
| 15 |
======================== |
| 16 |
|
| 17 |
fastText |
| 18 |
~~~~~~~~ |
| 19 |
Language detection with fastText. |
| 20 |
|
| 21 |
Import the fastText wrapper and pass it as ``detect_languages_function`` |
| 22 |
parameter. Example:: |
| 23 |
|
| 24 |
>>> from dateparser.custom_language_detection.fasttext import detect_languages |
| 25 |
>>> dateparser.parse('12/12/12', detect_languages_function=detect_languages) |
| 26 |
|
| 27 |
The fastText integration currently supports the large and the small models. |
| 28 |
Find more information about `fasttext <https://fasttext.cc/blog/2017/10/02/blog-post.html>`__ models. |
| 29 |
You can download your model of choice using ``dateparser-download``. |
| 30 |
|
| 31 |
Downloading small model:: |
| 32 |
|
| 33 |
>>> dateparser-download --fasttext small |
| 34 |
|
| 35 |
Downloading large model:: |
| 36 |
|
| 37 |
>>> dateparser-download --fasttext large |
| 38 |
|
| 39 |
Deleting all cached models:: |
| 40 |
|
| 41 |
>>> dateparser-download --clear_cache |
| 42 |
|
| 43 |
.. note:: |
| 44 |
|
| 45 |
If no model has been downloaded, the fastText wrapper downloads and uses |
| 46 |
the small model by default. |
| 47 |
|
| 48 |
langdetect |
| 49 |
~~~~~~~~~~ |
| 50 |
Language detection with langdetect. |
| 51 |
|
| 52 |
Import the langdetect wrapper and pass it as ``detect_languages_function`` |
| 53 |
parameter. Example:: |
| 54 |
|
| 55 |
>>> from dateparser.custom_language_detection.langdetect import detect_languages |
| 56 |
>>> dateparser.parse('12/12/12', detect_languages_function=detect_languages) |
| 57 |
|
| 58 |
|
| 59 |
.. note:: |
| 60 |
|
| 61 |
From some tests we did, we recommend to use ``fastText`` for faster and more accurate results. |
| 62 |
|
| 63 |
Custom implementation |
| 64 |
===================== |
| 65 |
|
| 66 |
``dateparser`` allows the integration of any library to detect languages by |
| 67 |
wrapping that library in a function that accepts 2 parameters, ``text`` and |
| 68 |
``confidence_threshold``, and returns a list of the detected language codes in |
| 69 |
ISO 639 standards. |
| 70 |
|
| 71 |
|
| 72 |
Wrapper for boilerplate for implementing custom language detections:: |
| 73 |
|
| 74 |
def detect_languages(text, confidence_threshold): |
| 75 |
""" |
| 76 |
Takes 2 parameters, `text` and `confidence_threshold`, and returns |
| 77 |
a list of `languages codes`. |
| 78 |
|
| 79 |
* `text` is the input string whose language needs to be detected. |
| 80 |
|
| 81 |
* `confidence_threshold` is a number between 0 and 1 that indicates the |
| 82 |
minimum confidence required for language matches. |
| 83 |
|
| 84 |
For language detection libraries that, for each language, indicate how |
| 85 |
confident they are that the language matches the input text, you should |
| 86 |
filter out languages with a confidence lower than this value (adjusted, |
| 87 |
if needed, to the confidence range of the target library). |
| 88 |
|
| 89 |
This value comes from the dateparser setting |
| 90 |
`LANGUAGE_DETECTION_CONFIDENCE_THRESHOLD`. |
| 91 |
|
| 92 |
The result must be a list of languages codes (strings). |
| 93 |
""" |
| 94 |
# here you can apply your own logic |
| 95 |
return language_codes |
| 96 |
|