Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC] Tokenizers - Classic #8357

Merged
merged 15 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions _analyzers/tokenizers/classic-tokenizer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
layout: default
title: Classic Tokenizer
parent: Tokenizers
nav_order: 35

---

# Classic tokenizer

The classic tokenizer is built to handle English text efficiently, applying grammatical rules to break the text into tokens. It includes specific logic to handle patterns such as:
- acronyms
- email addresses
- domain names
- certain types of punctuation

This tokenizer works best with the English language, it may not produce optimal results for other languages, especially those with different grammatical structures.
{: .note}

The classic tokenizer splits words at most punctuation marks while removing the punctuation. Dots that aren't followed by spaces are treated as part of the token. It also breaks words at hyphens, except when a number is present, treating the entire sequence as a single token, like a product code. Additionally, it recognizes email addresses and hostnames as individual tokens to keep them intact.

## Example using the classic tokenizer

By using the classic tokenizer, we can see how the tokenizer retains patterns like email addresses and phone numbers, while splitting other text at punctuation marks:

```json
POST _analyze
{
"tokenizer": "classic",
"text": "Send an email to john.doe@example.com or call 555-1234!"
}
```
{% include copy-curl.html %}

The tokenizer keeps the email address `john.doe@example.com` and the phone number `555-1234` as single tokens, while splitting the rest of the sentence at punctuation marks.

By analyzing the text "Send an email to john.doe@example.com or call 555-1234!", we can see the punctuation has been removed, while email and phone number

```
"Send", "an", "email", "to", "john.doe", "example.com", "or", "call", "555-1234"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I was wondering if it would be better to show entire output as there are different token types.
{"<ALPHANUM>", "<APOSTROPHE>", "<ACRONYM>", "<COMPANY>", "<EMAIL>", "<HOST>", "<NUM>", "<CJ>", "<ACRONYM_DEP>"}

{
  "tokens": [
    {
      "token": "Send",
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "an",
      "start_offset": 5,
      "end_offset": 7,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "email",
      "start_offset": 8,
      "end_offset": 13,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "to",
      "start_offset": 14,
      "end_offset": 16,
      "type": "<ALPHANUM>",
      "position": 3
    },
    {
      "token": "john.doe@example.com",
      "start_offset": 17,
      "end_offset": 37,
      "type": "<EMAIL>",
      "position": 4
    },
    {
      "token": "or",
      "start_offset": 38,
      "end_offset": 40,
      "type": "<ALPHANUM>",
      "position": 5
    },
    {
      "token": "call",
      "start_offset": 41,
      "end_offset": 45,
      "type": "<ALPHANUM>",
      "position": 6
    },
    {
      "token": "555-1234",
      "start_offset": 46,
      "end_offset": 54,
      "type": "<NUM>",
      "position": 7
    }
  ]
}

```

2 changes: 1 addition & 1 deletion _analyzers/tokenizers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: default
title: Tokenizers
nav_order: 60
has_children: false
has_children: true
has_toc: false
redirect_from:
- /analyzers/tokenizers/index/
Expand Down