NLP and Elastic: Getting started
Tags: nlp, tutorial, elasticsearch
Published: 2022-06-02 21:13:34

| [Menu](https://dev.to/priscilla_parodi/a-guide-to-machine-learning-ai-with-the-elastic-stack-1dkl) | [Next Post: NLP HandsOn](https://dev.to/elastic/nlp-handson-4f90) |

**Natural language processing (NLP)** is the branch of artificial intelligence (AI) that focuses on understanding human language as closely as possible to human interpretation, combining computational linguistics with statistical, machine learning and deep learning models.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wy77qmvnlsb6oyavpdze.png)

## Some examples of NLP tasks:

- **Named entity recognition** is a type of information extraction, identifying words or phrases as entities.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cquav4p0h8gheba0zhwv.png)([_model used_](https://huggingface.co/dslim/bert-base-NER))

- **Sentiment analysis** is a type of text classification, attempting to extract subjective emotions from text.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3oseiqe4zrzrbvpywsa3.png)([_model used_](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english))

There are more examples that can be used according to your use case.

## BERT

In 2018, Google sourced a new technique for pre-training NLP called [BERT](https://www.youtube.com/watch?v=2lR8Fzays4I).

BERT uses “transfer learning”, which is the method of pre-training linguistic representations. Pre-training refers to how BERT was first trained using unsupervised learning on a large source of plain text extracted from a collection of samples (800 million words) and Wikipedia documents (2,500 million words). Earlier models required manual labeling.

BERT was pretrained on two tasks: language modeling (15% of tokens were masked and BERT was trained to predict them from context) and next sentence prediction (BERT was trained to predict if a chosen next sentence was probable or not given the first sentence). With this understanding, BERT can be adapted to many other types of NLP tasks very easily.

Knowing the intent and context and not just the keywords, it is possible to go further in understanding in a way that is even closer to the way humans understand.

## NLP with Elastic

To support models that use the same tokenizer as BERT, Elastic is supporting the PyTorch library, one of the most popular machine learning libraries that supports neural networks like the Transformer architecture that BERT uses, enabling NLP tasks.

In general, any trained model that has a supported architecture is deployable in Elasticsearch, including BERT and variants.

These models are listed by NLP task. [Currently](https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-overview.html), these are the tasks supported:

- [Extract information](https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-extract-info.html)

Named entity recognition
Fill-mask
Question answering

- [Classify text](https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-classify-text.html)

Language identification
Text classification
Zero-shot text classification

- [Search and compare text](https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-search-compare.html)

Text embedding
Text similarity

As in the cases of [classification](https://dev.to/priscilla_parodi/elastic-data-frame-classification-analysis-1b3f) and [regression](https://dev.to/priscilla_parodi/elastic-data-frame-regression-analysis-2ge2), when a [trained model](https://dev.to/priscilla_parodi/trained-models-for-supervised-learning-154n) is imported you can use it to make predictions ([inference](https://dev.to/priscilla_parodi/elastic-data-frame-inference-processor-handson-3392)).

_Note: For NLP tasks you must choose and deploy a third-party NLP model. If you choose to perform language identification, as an option we have a trained model `lang_ident_model_1` provided in the cluster._

## NLP with Elastic Solutions

There are many possible use cases to add NLP capabilities to your Elastic project and here are some examples:

- **Security**

Spam detection: Text classification capabilities are useful for scanning emails for language that often indicates spam, allowing content to be blocked or deleted and preventing malware emails.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t4dljvgvf6yapd9ug52.png)

```
PUT spam-detection/_doc/1
{
  "email subject": "Camera - You are awarded a SiPix Digital Camera! Call 09061221066. Delivery within 28 days.",
  "is_spam": true
}
```

- **Enterprise Search**

Analysis of unstructured text: Entity recognition is useful for structuring text data, adding new field types to your documents and allowing you to analyze more data and obtain even more valuable insights.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksptdxozxqx09ugsh3vk.png)

```
PUT /source-index
{
  "mappings": {
    "properties": {
      "input":    { "type": "text" }
    }
  }
}
```
```
PUT /new-index
{
  "mappings": {
    "properties": {
      "input":    { "type": "text" },  
      "organization":  { "type": "keyword"  }, 
      "location":   { "type": "keyword"  }     
    }
  }
}
```

- **Observability**

Service request and incident data: Extracting meaning from operational data, including ticket resolution comments, allows you to not only generate alerts during incidents, but also go further by observing your application, predicting behavior, and having more data to improve ticket resolution time.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwxuc8kv6g1ur3lbvki9.png)

```
...
  "_source": {
    "support_ticket_id": 119237,
    "customer_id": 283823,
    "timestamp": "2021-06-06T17:23:02.770Z",
    "text_field": "Response to the case was fast and problem was solved after first response, did not need to provide any additional info.",
    "ml": {
      "inference": {
        "predicted_value": "positive",
        "prediction_probability": 0.9499962712516151,
        "model_id": "heBERT_sentiment_analysis"
      }
    }
  }
...
```

## NLP HandsOn

Now, let's proceed with an **[end-to-end example](https://dev.to/elastic/nlp-handson-4f90)**! To prepare for the [NLP HandsOn](https://dev.to/elastic/nlp-handson-4f90), we will need an Elasticsearch cluster running at least version 8.0 with an ML node. If you haven't created your [Elastic Cloud Trial](https://dev.to/priscilla_parodi/handson-setup-elastic-cloud-4j8p) yet, now is the time.

| [Menu](https://dev.to/priscilla_parodi/a-guide-to-machine-learning-ai-with-the-elastic-stack-1dkl) | [Next Post: NLP HandsOn](https://dev.to/elastic/nlp-handson-4f90) |

>*This post is part of a series that covers [Artificial Intelligence with a focus on Elastic's (Creators of Elasticsearch, Kibana, Logstash and Beats) Machine Learning solution](https://dev.to/priscilla_parodi/a-guide-to-machine-learning-ai-with-the-elastic-stack-1dkl), aiming to introduce and exemplify the possibilities and options available, in addition to addressing the context and usability.*