Skip to main content

Regular Expressions


Open In Collab Run in Postman

Regular expression (regex) a popular method for information extraction from text. If your entities follow a pattern then a regular expression is your best choice.

Prerequisites

Make sure to login using API and have your authorization token in a variable called AUTHORIZATION_TOKEN before running this API.

Create a Regex Entity

Using this API you can create an entity and add multiple regular expressions to it. If any of these match some part of a given text, it is tagged as this entity.

curl --location --request POST 'https://platform.neuralspace.ai/api/nlu/v1/entity' \
--header "authorization: ${AUTHORIZATION_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw '{
"entity":"4_5_digit_number",
"examples":[
"[0-9]{5}",
"[0-9]{4}"
],
"language":"en",
"entityType":"regex"
}'
AttributeRequiredTypeLimitsDescription
entitytruestr30 charactersName of the entity that you are creating
examplestruelistA list of regular expressions as string.
languagetruestrone of the supported languages
entityTypetruestrOne of regex, lookup, synonymThe type of entity this is.

List Regex Entities

Use our API to access all available regex entities.

Filter by entity type
curl --location --request POST 'https://platform.neuralspace.ai/api/nlu/v1/list/entity' \
--header "authorization: ${AUTHORIZATION_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw "{
\"filter\":{
\"entityType\":\"regex\"
},
\"search\": \"\",
\"pageNumber\": 1,
\"pageSize\": 10
}"

You can filter all entities by passing an entityType attribute in the filter. This is a pagination API, hence, pageSize determines how many projects to retrieve and pageNumber determines which page to fetch.

Filter by language
curl --location --request POST 'https://platform.neuralspace.ai/api/nlu/v1/list/entity' \
--header "authorization: ${AUTHORIZATION_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw "{
\"filter\":{
\"entityType\":\"regex\",
\"language\":\"en\"
},
\"search\": \"\",
\"pageNumber\": 1,
\"pageSize\": 10
}"

This will show all pre-trained entities in Hindi (en)

Add Examples to a Regex Entity

To add an example to an existing entity you will need the entityId of that entity. In this case lets add another regex pattern to extract three digit numbers.

curl --location --request POST 'https://platform.neuralspace.ai/api/nlu/v1/add/examples/entity' \
--header 'Accept: application/json, text/plain, */*' \
--header "authorization: ${AUTHORIZATION_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw '{
"entityId":"YOUR-ENTITY-ID",
"examples": ["[0-9]{3}"]
}'
note

While updating an existing entity you can add multiple examples at once.

Delete Examples from a Regex Entity

To delete an example from an existing entity you will also need the entityId of that entity. In this case lets delete the three digits regex pattern that we had added in the previous section.

curl --location --request POST 'https://platform.neuralspace.ai/api/nlu/v1/remove/examples/entity' \
--header 'Accept: application/json, text/plain, */*' \
--header "authorization: ${AUTHORIZATION_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw '{
"entityId":"YOUR-ENTITY-ID",
"examples": ["[0-9]{3}"]
}'
note

You can delete multiple examples at once. Add all examples you want to delete in the list attribute examples to do so.

Delete Regex Entity

Using this API you can delete a regex entity. You will need the entityId for this.

curl --location --request DELETE 'https://platform.neuralspace.ai/api/nlu/v1/entity' \
--header "authorization: ${AUTHORIZATION_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw '{
"entityId":"YOUR-ENTITY-ID"
}'
warning

Once deleted an entity cannot be recovered.

Regex Entities in NLU Models

To use a regex entity in NLU you need to create a regex entity first.

Create a PNR regex entity
curl --location --request POST 'https://platform.neuralspace.ai/api/nlu/v1/entity' \
--header "authorization: ${AUTHORIZATION_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw '{
"entity":"pnr",
"examples":[
"^[A-Z0-9]{5,6}$"
],
"language":"en",
"entityType":"regex"
}'

Once you successfully create an entity you can add it to your NLU training examples.

To use regex entities in your NLU models you will have to tag at least one training example with it. Additionally, you will have to set the entityType attribute of the entity to regex.

Sample training example
{
"projectId": "YOUR-PROJECT-ID",
"language": "en",
"example": {
"intent": "flight_status",
"text": "check status for J5XTP2",
"type": "train",
"entities": [
{
"entity": "pnr",
"value": "J5XTP2",
"start": 17,
"end": 23,
"entityType": "regex"
}
]
}
}