2021-05-10 10:42:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/nlp-functions
2023-04-19 17:05:55 +00:00
sidebar_position: 130
sidebar_label: NLP (experimental)
2021-05-10 10:42:32 +00:00
---
2023-06-23 12:49:41 +00:00
# Natural Language Processing (NLP) Functions
2023-03-27 18:54:05 +00:00
:::note
2022-04-09 13:29:05 +00:00
This is an experimental feature that is currently in development and is not ready for general use. It will change in unpredictable backwards-incompatible ways in future releases. Set `allow_experimental_nlp_functions = 1` to enable it.
:::
2021-05-10 10:42:32 +00:00
2022-06-02 10:55:18 +00:00
## stem
2021-05-10 10:42:32 +00:00
2021-08-02 12:32:45 +00:00
Performs stemming on a given word.
2021-05-10 10:42:32 +00:00
2023-05-22 17:02:39 +00:00
### Syntax
2021-05-10 10:42:32 +00:00
``` sql
2021-06-05 03:57:53 +00:00
stem('language', word)
2021-05-10 10:42:32 +00:00
```
2023-05-22 17:02:39 +00:00
### Arguments
2021-05-10 10:42:32 +00:00
2023-05-22 17:02:39 +00:00
- `language` — Language which rules will be applied. Use the two letter [ISO 639-1 code ](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes ).
2024-05-24 03:54:16 +00:00
- `word` — word that needs to be stemmed. Must be in lowercase. [String ](../data-types/string.md#string ).
2021-05-10 10:42:32 +00:00
2023-05-22 17:02:39 +00:00
### Examples
2021-05-10 10:42:32 +00:00
Query:
``` sql
2021-09-28 14:26:35 +00:00
SELECT arrayMap(x -> stem('en', x), ['I', 'think', 'it', 'is', 'a', 'blessing', 'in', 'disguise']) as res;
2021-05-10 10:42:32 +00:00
```
Result:
``` text
2021-06-05 03:57:53 +00:00
┌─res────────────────────────────────────────────────┐
│ ['I','think','it','is','a','bless','in','disguis'] │
└────────────────────────────────────────────────────┘
2021-05-10 10:42:32 +00:00
```
2023-05-22 16:14:23 +00:00
### Supported languages for stem()
:::note
The stem() function uses the [Snowball stemming ](https://snowballstem.org/ ) library, see the Snowball website for updated languages etc.
:::
- Arabic
- Armenian
- Basque
- Catalan
- Danish
- Dutch
- English
- Finnish
- French
- German
- Greek
- Hindi
- Hungarian
- Indonesian
- Irish
- Italian
- Lithuanian
- Nepali
- Norwegian
- Porter
- Portuguese
- Romanian
- Russian
- Serbian
- Spanish
- Swedish
- Tamil
- Turkish
- Yiddish
2021-05-10 10:42:32 +00:00
2022-06-02 10:55:18 +00:00
## lemmatize
2021-05-10 10:42:32 +00:00
2021-08-02 15:54:24 +00:00
Performs lemmatization on a given word. Needs dictionaries to operate, which can be obtained [here ](https://github.com/vpodpecan/lemmagen3/tree/master/src/lemmagen3/models ).
2021-05-10 10:42:32 +00:00
2023-05-22 17:02:39 +00:00
### Syntax
2021-05-10 10:42:32 +00:00
``` sql
2021-06-05 03:57:53 +00:00
lemmatize('language', word)
2021-05-10 10:42:32 +00:00
```
2023-05-22 17:02:39 +00:00
### Arguments
2021-05-10 10:42:32 +00:00
2024-05-24 03:54:16 +00:00
- `language` — Language which rules will be applied. [String ](../data-types/string.md#string ).
- `word` — Word that needs to be lemmatized. Must be lowercase. [String ](../data-types/string.md#string ).
2021-05-10 10:42:32 +00:00
2023-05-22 17:02:39 +00:00
### Examples
2021-05-10 10:42:32 +00:00
Query:
``` sql
2021-06-05 03:57:53 +00:00
SELECT lemmatize('en', 'wolves');
2021-05-10 10:42:32 +00:00
```
Result:
``` text
2021-06-05 03:57:53 +00:00
┌─lemmatize("wolves")─┐
│ "wolf" │
└─────────────────────┘
2021-05-10 10:42:32 +00:00
```
2023-05-22 17:02:39 +00:00
### Configuration
This configuration specifies that the dictionary `en.bin` should be used for lemmatization of English (`en`) words. The `.bin` files can be downloaded from
[here ](https://github.com/vpodpecan/lemmagen3/tree/master/src/lemmagen3/models ).
2021-06-05 03:57:53 +00:00
``` xml
< lemmatizers >
< lemmatizer >
2023-05-22 17:02:39 +00:00
<!-- highlight - start -->
2021-06-05 03:57:53 +00:00
< lang > en< / lang >
< path > en.bin< / path >
2023-05-22 17:02:39 +00:00
<!-- highlight - end -->
2021-06-05 03:57:53 +00:00
< / lemmatizer >
< / lemmatizers >
```
2021-05-10 10:42:32 +00:00
2022-06-02 10:55:18 +00:00
## synonyms
2021-06-05 03:57:53 +00:00
2021-08-02 12:32:45 +00:00
Finds synonyms to a given word. There are two types of synonym extensions: `plain` and `wordnet` .
2021-08-02 15:54:24 +00:00
With the `plain` extension type we need to provide a path to a simple text file, where each line corresponds to a certain synonym set. Words in this line must be separated with space or tab characters.
2021-08-02 12:32:45 +00:00
2021-08-02 15:54:24 +00:00
With the `wordnet` extension type we need to provide a path to a directory with WordNet thesaurus in it. Thesaurus must contain a WordNet sense index.
2021-05-10 10:42:32 +00:00
2023-05-22 17:02:39 +00:00
### Syntax
2021-05-10 10:42:32 +00:00
``` sql
2021-06-05 03:57:53 +00:00
synonyms('extension_name', word)
2021-05-10 10:42:32 +00:00
```
2023-05-22 17:02:39 +00:00
### Arguments
2021-05-10 10:42:32 +00:00
2024-05-24 03:54:16 +00:00
- `extension_name` — Name of the extension in which search will be performed. [String ](../data-types/string.md#string ).
- `word` — Word that will be searched in extension. [String ](../data-types/string.md#string ).
2021-05-10 10:42:32 +00:00
2023-05-22 17:02:39 +00:00
### Examples
2021-05-10 10:42:32 +00:00
Query:
``` sql
2021-06-05 03:57:53 +00:00
SELECT synonyms('list', 'important');
2021-05-10 10:42:32 +00:00
```
Result:
``` text
2021-06-05 03:57:53 +00:00
┌─synonyms('list', 'important')────────────┐
│ ['important','big','critical','crucial'] │
└──────────────────────────────────────────┘
2021-05-10 10:42:32 +00:00
```
2023-05-22 17:02:39 +00:00
### Configuration
2021-06-05 03:57:53 +00:00
``` xml
< synonyms_extensions >
< extension >
< name > en< / name >
< type > plain< / type >
< path > en.txt< / path >
< / extension >
< extension >
< name > en< / name >
< type > wordnet< / type >
< path > en/< / path >
< / extension >
< / synonyms_extensions >
2021-08-02 15:54:24 +00:00
```
2023-01-10 19:26:51 +00:00
## detectLanguage
Detects the language of the UTF8-encoded input string. The function uses the [CLD2 library ](https://github.com/CLD2Owners/cld2 ) for detection, and it returns the 2-letter ISO language code.
The `detectLanguage` function works best when providing over 200 characters in the input string.
2023-05-22 17:02:39 +00:00
### Syntax
2023-01-10 19:26:51 +00:00
``` sql
detectLanguage('text_to_be_analyzed')
```
2023-05-22 17:02:39 +00:00
### Arguments
2023-01-10 19:26:51 +00:00
2024-05-24 03:54:16 +00:00
- `text_to_be_analyzed` — A collection (or sentences) of strings to analyze. [String ](../data-types/string.md#string ).
2023-01-10 19:26:51 +00:00
2023-05-22 17:02:39 +00:00
### Returned value
2023-01-10 19:26:51 +00:00
- The 2-letter ISO code of the detected language
Other possible results:
- `un` = unknown, can not detect any language.
- `other` = the detected language does not have 2 letter code.
2023-05-22 17:02:39 +00:00
### Examples
2023-01-10 19:26:51 +00:00
Query:
```sql
2023-09-08 01:27:22 +00:00
SELECT detectLanguage('Je pense que je ne parviendrai jamais à parler français comme un natif. Where there’ s a will, there’ s a way.');
2023-01-10 19:26:51 +00:00
```
Result:
```response
fr
```
## detectLanguageMixed
Similar to the `detectLanguage` function, but `detectLanguageMixed` returns a `Map` of 2-letter language codes that are mapped to the percentage of the certain language in the text.
2023-05-22 17:02:39 +00:00
### Syntax
2023-01-10 19:26:51 +00:00
``` sql
detectLanguageMixed('text_to_be_analyzed')
```
2023-05-22 17:02:39 +00:00
### Arguments
2023-01-10 19:26:51 +00:00
2024-05-24 03:54:16 +00:00
- `text_to_be_analyzed` — A collection (or sentences) of strings to analyze. [String ](../data-types/string.md#string ).
2023-01-10 19:26:51 +00:00
2023-05-22 17:02:39 +00:00
### Returned value
2023-01-10 19:26:51 +00:00
2023-05-04 16:35:18 +00:00
- `Map(String, Float32)` : The keys are 2-letter ISO codes and the values are a percentage of text found for that language
2023-01-10 19:26:51 +00:00
2023-05-22 17:02:39 +00:00
### Examples
2023-01-10 19:26:51 +00:00
Query:
```sql
SELECT detectLanguageMixed('二兎を追う者は一兎をも得ず二兎を追う者は一兎をも得ず A vaincre sans peril, on triomphe sans gloire.');
```
Result:
```response
┌─detectLanguageMixed()─┐
│ {'ja':0.62,'fr':0.36 │
└───────────────────────┘
```
## detectLanguageUnknown
Similar to the `detectLanguage` function, except the `detectLanguageUnknown` function works with non-UTF8-encoded strings. Prefer this version when your character set is UTF-16 or UTF-32.
2023-05-22 17:02:39 +00:00
### Syntax
2023-01-10 19:26:51 +00:00
``` sql
detectLanguageUnknown('text_to_be_analyzed')
```
2023-05-22 17:02:39 +00:00
### Arguments
2023-01-10 19:26:51 +00:00
2024-05-24 03:54:16 +00:00
- `text_to_be_analyzed` — A collection (or sentences) of strings to analyze. [String ](../data-types/string.md#string ).
2023-01-10 19:26:51 +00:00
2023-05-22 17:02:39 +00:00
### Returned value
2023-01-10 19:26:51 +00:00
- The 2-letter ISO code of the detected language
Other possible results:
- `un` = unknown, can not detect any language.
- `other` = the detected language does not have 2 letter code.
2023-05-22 17:02:39 +00:00
### Examples
2023-01-10 19:26:51 +00:00
Query:
```sql
SELECT detectLanguageUnknown('Ich bleibe für ein paar Tage.');
```
Result:
```response
┌─detectLanguageUnknown('Ich bleibe für ein paar Tage.')─┐
│ de │
└────────────────────────────────────────────────────────┘
```
## detectCharset
The `detectCharset` function detects the character set of the non-UTF8-encoded input string.
2023-05-22 17:02:39 +00:00
### Syntax
2023-01-10 19:26:51 +00:00
``` sql
detectCharset('text_to_be_analyzed')
```
2023-05-22 17:02:39 +00:00
### Arguments
2023-01-10 19:26:51 +00:00
2024-05-24 03:54:16 +00:00
- `text_to_be_analyzed` — A collection (or sentences) of strings to analyze. [String ](../data-types/string.md#string ).
2023-01-10 19:26:51 +00:00
2023-05-22 17:02:39 +00:00
### Returned value
2023-01-10 19:26:51 +00:00
- A `String` containing the code of the detected character set
2023-05-22 17:02:39 +00:00
### Examples
2023-01-10 19:26:51 +00:00
Query:
```sql
SELECT detectCharset('Ich bleibe für ein paar Tage.');
```
Result:
```response
┌─detectCharset('Ich bleibe für ein paar Tage.')─┐
│ WINDOWS-1252 │
└────────────────────────────────────────────────┘
```