# Natural Language Processing (/docs/reference/sql/functions/natural-language-processing)



This page describes the RawTree engine binary used to generate this reference. It does not define Query API support. Descriptions retain upstream ClickHouse terminology. See [SQL interface scope](/docs/reference/clickhouse-compatibility#service-interfaces).

detectCharset [#detectcharset]

<ExperimentalBadge />

<CloudNotSupportedBadge />

:::warning
This function is experimental and may change in unpredictable backwards-incompatible ways in future releases.
Set `allow_experimental_nlp_functions = 1` to enable it.
:::

Detects the character set of a non-UTF8-encoded input string.

**Syntax**

```sql
detectCharset(s)
```

**Arguments**

* `s` — The text to analyze. [`String`](/reference/data-types/string)

**Returned value**

Returns a string containing the code of the detected character set [`String`](/reference/data-types/string)

**Examples**

**Basic usage**

```sql title=Query
SELECT detectCharset('Ich bleibe für ein paar Tage.')
```

```text title=Response
WINDOWS-1252
```

*Introduced in version 22.2.0.*

detectLanguage [#detectlanguage]

<ExperimentalBadge />

<CloudNotSupportedBadge />

:::warning
This function is experimental and may change in unpredictable backwards-incompatible ways in future releases.
Set `allow_experimental_nlp_functions = 1` to enable it.
:::

Detects the language of the UTF8-encoded input string.
The function uses the [CLD2 library](https://github.com/CLD2Owners/cld2) for detection and returns the 2-letter ISO language code.

The longer the input, the more precise the language detection will be.

**Syntax**

```sql
detectLanguage(s)
```

**Arguments**

* `text_to_be_analyzed` — The text to analyze. [`String`](/reference/data-types/string)

**Returned value**

Returns 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. [`String`](/reference/data-types/string)

**Examples**

**Mixed language text**

```sql title=Query
SELECT detectLanguage('Je pense que je ne parviendrai jamais à parler français comme un natif. Where there\'s a will, there\'s a way.')
```

```text title=Response
fr
```

*Introduced in version 22.2.0.*

detectLanguageMixed [#detectlanguagemixed]

<ExperimentalBadge />

<CloudNotSupportedBadge />

:::warning
This function is experimental and may change in unpredictable backwards-incompatible ways in future releases.
Set `allow_experimental_nlp_functions = 1` to enable it.
:::

Similar to the [`detectLanguage`](#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.

**Syntax**

```sql
detectLanguageMixed(s)
```

**Arguments**

* `s` — The text to analyze [`String`](/reference/data-types/string)

**Returned value**

Returns a map with keys which are 2-letter ISO codes and corresponding values which are a percentage of the text found for that language [`Map(String, Float32)`](/reference/data-types/map)

**Examples**

**Mixed languages**

```sql title=Query
SELECT detectLanguageMixed('二兎を追う者は一兎をも得ず二兎を追う者は一兎をも得ず A vaincre sans peril, on triomphe sans gloire.')
```

```text title=Response
{'ja':0.62,'fr':0.36}
```

*Introduced in version 22.2.0.*

detectLanguageUnknown [#detectlanguageunknown]

<ExperimentalBadge />

<CloudNotSupportedBadge />

:::warning
This function is experimental and may change in unpredictable backwards-incompatible ways in future releases.
Set `allow_experimental_nlp_functions = 1` to enable it.
:::

Similar to the [`detectLanguage`](#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.

**Syntax**

```sql
detectLanguageUnknown('s')
```

**Arguments**

* `s` — The text to analyze. [`String`](/reference/data-types/string)

**Returned value**

Returns 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. [`String`](/reference/data-types/string)

**Examples**

**Basic usage**

```sql title=Query
SELECT detectLanguageUnknown('Ich bleibe für ein paar Tage.')
```

```text title=Response
de
```

*Introduced in version 22.2.0.*

detectTonality [#detecttonality]

<ExperimentalBadge />

<CloudNotSupportedBadge />

:::warning
This function is experimental and may change in unpredictable backwards-incompatible ways in future releases.
Set `allow_experimental_nlp_functions = 1` to enable it.
:::

Determines the sentiment of the provided text data.

The function makes use of an embedded emotional dictionary and only works for the Russian language at the moment.

**Syntax**

```sql
detectTonality(s)
```

**Arguments**

* `s` — The text to be analyzed. [`String`](/reference/data-types/string)

**Returned value**

Returns the average sentiment value of the words in text [`Float32`](/reference/data-types/float)

**Examples**

**Russian sentiment analysis**

```sql title=Query
SELECT
    detectTonality('Шарик - хороший пёс'),
    detectTonality('Шарик - пёс'),
    detectTonality('Шарик - плохой пёс')
```

```text title=Response
0.44445, 0, -0.3
```

*Introduced in version 22.2.0.*

lemmatize [#lemmatize]

<ExperimentalBadge />

<CloudNotSupportedBadge />

:::warning
This function is experimental and may change in unpredictable backwards-incompatible ways in future releases.
Set `allow_experimental_nlp_functions = 1` to enable it.
:::

Performs lemmatization on a given word.
This function needs dictionaries to operate, which can be obtained from [github](https://github.com/vpodpecan/lemmagen3/tree/master/src/lemmagen3/models).
For more details on loading a dictionary from a local file see page ["Defining Dictionaries"](/reference/statements/create/dictionary/sources/local-file).

**Syntax**

```sql
lemmatize(lang, word)
```

**Arguments**

* `lang` — Language which rules will be applied. [`String`](/reference/data-types/string)
* `word` — Lowercase word that needs to be lemmatized. [`String`](/reference/data-types/string)

**Returned value**

Returns the lemmatized form of the word [`String`](/reference/data-types/string)

**Examples**

**English lemmatization**

```sql title=Query
SELECT lemmatize('en', 'wolves')
```

```text title=Response
wolf
```

*Introduced in version 21.9.0.*

stem [#stem]

Performs stemming on a word or an array of words using the Snowball algorithms.
Each input string must be a single, lowercase word — strings containing whitespace cause an exception.
Passing uppercase characters produces undefined results.
Returns String for scalar inputs (including FixedString) and Array(String) for array inputs.
Nullable and LowCardinality variants of String and FixedString are supported.
The list of supported language identifiers is available in [system.stemmers](/reference/system-tables/stemmers).

**Syntax**

```sql
stem(word, language)
```

**Arguments**

* `word` — A single lowercase word (or array of words) to stem. Must be lowercase — uppercase characters produce undefined results. Accepts String, FixedString, Array(String), Array(FixedString), Array(Nullable(String)), or Array(Nullable(FixedString)). [`String`](/reference/data-types/string) or [`FixedString`](/reference/data-types/fixedstring) or [`Array(String)`](/reference/data-types/array) or [`Array(FixedString)`](/reference/data-types/array)
* `language` — Language whose stemming rules will be applied. The canonical identifiers are listed in [system.stemmers](/reference/system-tables/stemmers) (e.g. 'english', 'german', 'porter'). Snowball also accepts 2- or 3-letter ISO 639 codes (e.g. 'en', 'eng') as aliases where defined, but coverage varies by language — prefer the names from `system.stemmers` for portability. [`String`](/reference/data-types/string)

**Returned value**

The stemmed form of the word (String), or an array of stemmed words (Array(String)). [`String`](/reference/data-types/string) or [`Array(String)`](/reference/data-types/array)

**Examples**

**Stemming a single word**

```sql title=Query
SELECT stem('blessing', 'en') AS res
```

```text title=Response
bless
```

**Stemming an array of words**

```sql title=Query
SELECT stem(['blessing', 'disguise'], 'en') AS res
```

```text title=Response
['bless','disguis']
```

**Stemming a FixedString**

```sql title=Query
SELECT stem(toFixedString('blessing', 10), 'en') AS res
```

```text title=Response
bless
```

**Stemming a Nullable word**

```sql title=Query
SELECT stem(toNullable('blessing'), 'en') AS res
```

```text title=Response
bless
```

*Introduced in version 21.9.0.*

synonyms [#synonyms]

<ExperimentalBadge />

<CloudNotSupportedBadge />

:::warning
This function is experimental and may change in unpredictable backwards-incompatible ways in future releases.
Set `allow_experimental_nlp_functions = 1` to enable it.
:::

Finds synonyms of a given word.

There are two types of synonym extensions:

* `plain`
* `wordnet`

With the `plain` extension type you 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.

With the `wordnet` extension type you need to provide a path to a directory with the WordNet thesaurus in it.
The thesaurus must contain a WordNet sense index.

**Syntax**

```sql
synonyms(ext_name, word)
```

**Arguments**

* `ext_name` — Name of the extension in which search will be performed. [`String`](/reference/data-types/string)
* `word` — Word that will be searched in extension. [`String`](/reference/data-types/string)

**Returned value**

Returns array of synonyms for the given word. [`Array(String)`](/reference/data-types/array)

**Examples**

**Find synonyms**

```sql title=Query
SELECT synonyms('list', 'important')
```

```text title=Response
['important','big','critical','crucial']
```

*Introduced in version 21.9.0.*
