SILPA Webservice APIs
Introduction
Silpa provides a set of webservice APIs over json-rpc protocol. Silpa services can be used from any programming language which has an RPC implementation. The request and response formats of APIs are in json. It is recommended to read the json-rpc documentation if you are not familiar with that.
This page explains the available usage and sample usage from python application. Implementing this in other programming languages should not be difficult and should be a matter of changing the programming language syntax For further assistance, see the related links section of this page
For using the APIs you need the jsonrpc library of python. You can get this from here.
Concepts
JSON-RPC wraps an object, allowing you to call methods on that object and get the return values. It also provides a way to get error responses.
The specification goes into the details (though in a vague sort of way). Here’s the basics:
All access goes through a POST to a single URL.
The POST contains a JSON body that looks like:
{"method": "methodName", "id": "arbitrary-something", "params": [arg1, arg2, ...]}
The id parameter is just a convenience for the client to keep track of which response goes with which request. This makes asynchronous calls (like an XMLHttpRequest) easier. We just send the exact same id back as we get, we never look at it.
The response is JSON. A successful response looks like:
{"result": the_result, "error": null, "id": "arbitrary-something"}
The error response looks like:
{"result": null, "error": {"name": "JSONRPCError", "code": (number 100-999), "message": "Some Error Occurred", "error": "whatever you want\n(a traceback?)"}, "id": "arbitrary-something"}It doesn’t seem to indicate if an error response should have a 200 response or a 500 response. So as not to be completely stupid about HTTP, we choose a 500 resonse, as giving an error with a 200 response is irresponsible.
Available Services
Silpa provides a service discovery APIs to get the list of available service APIs. The following code illustrates the usage:
# -*- coding: utf-8 -*-
from jsonrpc import ServiceProxy
silpaService = ServiceProxy("http://smc.org.in/silpa/JSONRPC")
print silpaService.system.listMethods()
When the above code is executed you should get a response like this:
['modules.CharDetails.getdetails', 'modules.Dictionary.getdef', 'modules.Fortune.fortune_ml', 'modules.LangGuess.getScriptName', 'modules.LangGuess.guessLanguage', 'modules.Payyans.ASCII2Unicode', 'modules.Payyans.ASCII2Unicode', 'modules.Payyans.Unicode2ASCII', 'modules.Payyans.Unicode2ASCII', 'modules.Soundex.compare', 'modules.Soundex.soundex','modules.Syllabalizer.syllabalize', 'modules.Transliterator.transliterate']
As you can see the response is a json formatted list of available methods available in the server
Dictionary
Dictionary APIs provide dictionary lookup service.- Method: modules.Dictionary.getdef
- arg1 : the word
- arg2 : the dictionary
print silpaService.modules.Dictionary.getdef("help","freedict-eng-hin")
Response will be the string containing the definition of the given word
Spellcheck
Spellcheck API provides langauge independent spellchecking service across Indian languages and English- Method: modules.Spellchecker.check
- arg1 : the word
- arg2 : the language for the word(optional)
- Return : True or False. True means the word is with correct spelling. Otherwise false.
- Method: modules.Spellchecker.suggest
- arg1 : the word
- arg2 : the language for the word(optional)
- Return : List of string containing spelling suggestions
>>>print silpaService.modules.Spellchecker.check("speling")
False
>>>print silpaService.modules.Spellchecker.check("speling","en_US")
False
>>>print silpaService.modules.Spellchecker.suggest("speling")
["spelling","spieling","spewing"]
The language parameter is optional. If not provided the language of the input word will be detected.
Soundex
This service provides indic soundex algorithm based soundex codes for a word- Method: modules.Soundex.soundex
- arg1 : the word
- Return : The soundex code for the word
- Method: modules.Soundex.compare
- arg1 : first word
- arg2 : second word
- Return : 0 if both strings are same, 1 if both strngs sounds alike and from same language, 2 if strings are from different langauges but sounds alike
>>>print silpaService.modules.Soundex.soundex("കാര്ത്തിക്")
കAPKKBF0
>>>print silpaService.modules.Soundex.compare("കാര്ത്തിക്","கார்திக்")
2
Ngram
This service provides indic ngram libraries- Method: modules.Ngram.wordNgram
- arg1 : the sentence
- n : n of n-gram (Optional)
- Return : The ngram for the sentence
- Method: modules.Ngram.letterNgram
- arg1 : the word
- n : n of n-gram (Optional)
- Return : The ngram for the word
- Method: modules.Ngram.syllableNgram
- arg1 : the word
- n : n of n-gram (Optional)
- Return : The ngram for the word, the letters being splitted at syllable level
Fortune
This service provides random quotes- Method: modules.Fortune.fortune
- arg1 : database file name
- arg2 : Search word(optional)
- Return : Random quote
>>>print silpaService.modules.Fortune.fortune('chanakya') Do not inhabit a country where you are not respected,\n cannot earn your livelihood, have no friends, or cannot\n acquire knowledge.\n" >>>print silpaService.modules.Fortune.fortune('chanakya','charity') " The good habits of charity, learning and austerity\n practised during many past lives continue to be cultivated\n in this birth by virtue of the link (yoga) of this present\n life to the previous ones.\n"