Language Detection API resources

On this page we collect code examples of the implementations of the Language Detection API documentation, together with the best practices in the deployment and the use of the code.

This page currently contains the (links to) following implementations:

  • Python
  • PHP

We encourage, support and reward the community efforts to extend and streamline the implementations for different platforms and programming languages, as we believe that no one knows their tools better than the seasoned developers themselves. If you want to know more or wish to submit an implementation of your own, please contact us!

Python (2.6, 2.7) implementation

This Python implementation allows you to communicate with the Language Detection API using your an API key. To use this implementation in your own project, please make sure you extract it in your project's directory, or somewhere on the Python's path.  

Download the Python class for Language Detection API here

The ZIP file above contains two files: 

  1. aia_LanguageDetectionAPI.py: This contains the API wrapper class which can be instantiatied to talk directly to the API. To be instantiated, the class requires an API key. 
  2. test_language_detection.py: a simple test script, creating an instance of the aia_LanguageDetectionAPI class, adding some data for processing, and processing it using the API.

The API connection class is an extension of a ordinary Python list, inheriting all the Python list behaviors, and adding a process() command allowing you to call the API. A use example of the class is present in the test script, with a simple example provided here:

# Import the API connection library
import aia_LanguageDetectionAPI

# Create a API connection instance using your API key
# The optional data parameter allows you to assign the data in the appropriate format to the API if you have it stored
# in another Python list object, for instance, if you have retrieved the data already from an online source.
api = aia_LanguageDetectionAPI.aia_LanguageDetectionAPI("<your_api_key_here>", data=[])

# Add some objects for classification to the API connection instance in the same way you would add them to a ordinary Python list
api.append({"text": "Я должен сказать, что персонал был недружественный."})
api.append({"text": "The hotel maid is very nice."})
api.append({"text": "Ontbijt was vreselijk."})
api.append({"text": "La piscina coperta riscaldata mi è piaciuta."})
api.append({"text": "J'étais ici l'été dernier, et c'était très amusant."})

# Execute the API call for all your text, optionally specifying whether you want to have verbose output of the processing
# The objects in the api list will be automatically changed by this call, adding to them the annotations from the API
# The text summarizing the processing will be returned by the .process() call
summary = api.process(verbose=False)

# Print the processing summary to stdout
>> print summary
"OK. Call processed."

# Print the first object from the processed data. Remember, the api instance is just another Python list
# allowing you all the normal list operations on it.
>> print api[0]
{"text": "Я должен сказать, что персонал был недружественный.", "language_iso": "rus", "language_eng_name": "Russian", "confidence_language": 1.0}

A common use of Language Detection API is as a pre-processing step for Sentiment Analysis and other more advanced analyses.

Because an instance of an Language Detection API behaves in full like a Python list, it is possible to use it as a part of a chain of text processing commands. An extension of the example above to Sentiment Analysis, using the appropriate Python class provided here, would look as follows:

import aia_SentimentAnalysisAPI

# Create a Sentiment Analysis API instance here, using the "api" object from the example above with the language-annotated data as a input for the Sentiment Analysis
sentiment_api = aia_SentimentAnalysisAPI.aia_SentimentAnalysisAPI("<your_api_key_here>", sentiment_classifier="default", data=api)

# Execute the API call for all your text, optionally specifying whether you want to have verbose output of the processing
# The objects in the api list will be automatically updated by this call, adding to them the annotations from the Sentiment API
# The text summarizing the processing will be returned by the .process() call
sentiment_summary = sentiment_api.process(verbose=False)

# Print the processing summary to stdout
>> print sentiment_summary
"OK. Call processed."

# Print the first object from the processed data. It is now further annotated by the sentiment information.
>> print sentiment_api[0]
{"text": "Я должен сказать, что персонал был недружественный.", "language_iso": "rus", "language_eng_name": "Russian", "confidence_language": 1.0, "sentiment_class": "negative", "confidence_sentiment": 0.99487627042885}
 

PHP Implementation

A PHP implementation of all Ai Applied's APIs has been created by Mark Mooij. This implementation also supports the Language Detection API. The full implementation is downloadable from this link:

Download the PHP implementation of, amongst others, Language Detection API

The PHP implementetion contains a api_tester.php file, containing the actual code which calls the APIs, as well as a index.php file which contains API call examples. Both files are well documented on different aspects and configuration parameters for calling the API. At this point in time, we consider this implementation to be rudimentary, and any additions or re-implementations are most welcome!

 

For more information, please consult the Language Detection API documentation.

Please feel free to use the comments section for your questions, discussion and tips!