Microservice Recipe: Language Detection Microservice

Service Recipes > Language Detection Microservice
11 Mar 2017

Eventn supports the NPM module "Franc" for language detection by developer Titus Wormer. Franc has support for 175 languages (or 375 using the franc-all package).This makes it incredibly simple to add powerful language detection capabilities to a microservice.

Example

To test out the Franc library, lets first create a simple HTTP GET function that returns the language detected for a test string sent as a URL parameter. Create a new Eventn service and edit the GET function as follows:

const franc = require('franc');

function onGet(context) { return franc(request.query.test); }
module.exports = onGet;

Using the inbuilt HTTP test tools, add some sample text to the url parameters (in this case thetestparameter and select theGETbutton to make the request. The detected language is returned by Franc, in this example Spanish:

Slide Background

Save on POST

Another useful use case is to detect the language of some incoming text and save that to a database. Eventn provides astoreobject providing access to an inbuilt MySQL database table that is automatically created with every new service. In the example below, the incoming content is parsed using Franc and saved along with the language detected:

const franc = require('franc');

function onPost(context, request) {
const data = { text: request.payload.text, language: franc(request.payload.text) };
return context.stores.default() .table() .insert({ data: JSON.stringify(data) }); }
module.exports = onPost;

Examples of the saved data:

{
  "id": 25,
  "ts_created": "2017-03-11T20:57:50.000Z",
  "data": {
    "text": "Protection of your Internet history is up in the air thanks to new, pending legislation.",
    "language": "eng"
  }
},
{
  "id": 26,
  "ts_created": "2017-03-11T21:00:23.000Z",
  "data": {
    "text": "Protezione della vostra storia di Internet è in aria grazie alla nuova legislazione in attesa.",
    "language": "ita"
  }
}

Conclusions

Franc provides a powerful interface for language detection. Coupled with an Eventn microservice it becomes simple to deploy HTTP services for either a stand-alone language detection service (immediately returning a result), or easily augment incoming content to be persisted to a database.

Get Started For Free

Get Started