In this technical article, we will explore how to integrate Azure Cognitive Services with Neo4j to perform language detection and translation tasks. This integration will allow you to work with Neo4j data while leveraging the power of Azure Cognitive Services for natural language processing tasks.
Prerequisites:
- Valid Azure subscription with Azure Cognitive Services access.
- Neo4j database running in your environment.
- Familiarity with Neo4j Cypher queries and Azure Cognitive Services APIs.
Step 1: Obtain Azure Cognitive Services API Key and Endpoint
Before you can start using Azure Cognitive Services with Neo4j, you need to obtain an API key and the service endpoint from Azure.
Step2: Write Cypher Queries for Language Detection
This is the sample data we will be using in this article. You can use the following Cypher commands to create the nodes:
CREATE (j:Job {description: "Support Engineer ",
id: "JOB124",
skills: ["Java", "Python", "C++"]});
CREATE (j:Job {description: "Software Developer",
id: "JOB123",
skills: ["Java", "Python", "C++"]});
CREATE (j:Job {description: "Software Developer with Proficiency in Java or C++, and object-oriented design skills ",
id: "JOB125",
skills: ["Java", "Python", "C++"]});
CREATE (j:Job {
description: "Experienced Project Manager with strong leadership skills and background in construction",
id: "JOB234",
skills: ["Project Management", "Leadership", "Construction"]
});
We will be using apoc.load.jsonParams to call the Cognitive Service API.
Let's write a Cypher query to detect the language of text stored in a Neo4j node property. Here's an example query:
MATCH (j:Job {id: "JOB234"})
WITH j.description AS jobDescription
WITH {`Ocp-Apim-Subscription-Key`: "YOUR_API_KEY",
`Ocp-Apim-Subscription-Region`: "YOUR_REGION",
`Content-Type`: "application/json"} AS headers, jobDescription AS text
CALL apoc.load.jsonParams(
"https://api.cognitive.microsofttranslator.com/detect?api-version=3.0",
headers, '[{"text":"' + text + '"}]'
) YIELD value
RETURN value
This query fetches the job description from a Neo4j node, sends it to Azure Cognitive Services for language detection, and returns the detected language :
[{"language": "en", "score": 1.0}]
Step 3: Write Cypher Queries for Translation
Now, let's create a Cypher query for translating text from one language to another. Here's an example query:
MATCH (j:Job {id: "JOB234"})
WITH j.description AS jobDescription
WITH {`Ocp-Apim-Subscription-Key`: "YOUR_API_KEY",
`Ocp-Apim-Subscription-Region`: "YOUR_REGION",
`Content-Type`: "application/json"} AS headers, jobDescription AS text
CALL apoc.load.jsonParams(
"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=fr",
headers, '[{"text":"' + text + '"}]'
) YIELD value
RETURN value
This query translates the job description from English (from=en
) to French (to=fr
). You can customize the from
and to
parameters for your specific translation needs.
Result:
{
"translations": [
{
"text": "Gestionnaire de projet expérimenté avec de
solides compétences en leadership et une expérience dans la construction",
"to": "fr"
}
]
}
Obviously the same principles could be applied to other cognitive services from other providers. For example here is an example of calling a language detection API from detectlanguage.com
that you can test for free (get your API key from detectlanguage.com):
WITH {Authorization: "Bearer YOUR_API_KEY"} AS headers
CALL apoc.load.jsonParams("https://ws.detectlanguage.com/0.2/detect?q='bonjour'",
headers,null) yield value return value
Result:
{
"data": {
"detections": [
{
"language": "fr",
"isReliable": true,
"confidence": 9
}
]
}
}
Moreover, additional Azure Cognitive Services, including Sentiment analysis, key phrase extraction, and Entities recognition, have been incorporated into Neo4j as APOC procedures. These APOC extended procedures serve as a bridge for making API calls to perform tasks such as extracting entities and key phrases and conducting sentiment analysis on text stored within Neo4j database node properties.
Further reading on Azure Cognitive Services APOC extended implementation: https://neo4j.com/labs/apoc/5/nlp/azure/
Comments
0 comments
Please sign in to leave a comment.