{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using LX-Syllabifier to syllabify all words in a text\n", "This is an example notebook that illustrates how you can use the LX-Syllabifier web service to\n", "analyse a text.\n", "\n", "**Before you run this example**, replace `access_key_goes_here` by your webservice access key, below:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "LXSLLABIFIER_WS_API_KEY = 'access_key_goes_here'\n", "LXSLLABIFIER_WS_API_URL = 'https://portulanclarin.net/workbench/lx-syllabifier/api/'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Importing required Python modules\n", "The next cell will take care of installing the `requests` package,\n", "if not already installed, and make it available to use in this notebook." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "try:\n", " import requests\n", "except:\n", " !pip3 install requests\n", " import requests\n", "from IPython.display import HTML, display_html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Wrapping the complexities of the JSON-RPC API in a simple, easy to use function\n", "\n", "The `WSException` class defined below, will be used later to identify errors\n", "from the webservice." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "class WSException(Exception):\n", " 'Webservice Exception'\n", " def __init__(self, errordata):\n", " \"errordata is a dict returned by the webservice with details about the error\"\n", " super().__init__(self)\n", " assert isinstance(errordata, dict)\n", " self.message = errordata[\"message\"]\n", " # see https://json-rpc.readthedocs.io/en/latest/exceptions.html for more info\n", " # about JSON-RPC error codes\n", " if -32099 <= errordata[\"code\"] <= -32000: # Server Error\n", " if errordata[\"data\"][\"type\"] == \"WebServiceException\":\n", " self.message += f\": {errordata['data']['message']}\"\n", " else:\n", " self.message += f\": {errordata['data']!r}\"\n", " def __str__(self):\n", " return self.message" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next function invoques the LX-Suite webservice through it's public JSON-RPC API." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def syllabify(text):\n", " '''\n", " Arguments\n", " text: a string with a maximum of 10000 characters, Portuguese text, with\n", " the input to be processed\n", "\n", " Returns a string or JSON object with the output according to specification in\n", " https://portulanclarin.net/workbench/lx-syllabifier/\n", " \n", " Raises a WSException if an error occurs.\n", " '''\n", "\n", " request_data = {\n", " 'method': 'syllabify',\n", " 'jsonrpc': '2.0',\n", " 'id': 0,\n", " 'params': {\n", " 'text': text,\n", " 'key': LXSLLABIFIER_WS_API_KEY,\n", " },\n", " }\n", " request = requests.post(LXSLLABIFIER_WS_API_URL, json=request_data)\n", " response_data = request.json()\n", " if \"error\" in response_data:\n", " raise WSException(response_data[\"error\"])\n", " else:\n", " return response_data[\"result\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next function will count the number of syllables in a given string (already processed by LX-Syllabifier):" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def count_syllables(s):\n", " # this is a naive tokenization based on whitespace, but in principle it poses no problem\n", " # because punctuation will be attached to the previous token and that will not change the\n", " # number of syllables\n", " return sum(len(token.split(\"|\")) for token in s.split(\" \"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are a few stanzas from Luís de Camões' work \"Os Lusíadas\" that we will use in our experiment:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "stanzas = [\"\"\"\n", "As armas e os barões assinalados,\n", "Que da ocidental praia Lusitana,\n", "Por mares nunca de antes navegados,\n", "Passaram ainda além da Taprobana,\n", "Em perigos e guerras esforçados,\n", "Mais do que prometia a força humana,\n", "E entre gente remota edificaram\n", "Novo Reino, que tanto sublimaram;\n", "\"\"\",\"\"\"\n", "E também as memórias gloriosas\n", "Daqueles Reis, que foram dilatando\n", "A Fé, o Império, e as terras viciosas\n", "De África e de Ásia andaram devastando;\n", "E aqueles, que por obras valerosas\n", "Se vão da lei da morte libertando;\n", "Cantando espalharei por toda parte,\n", "Se a tanto me ajudar o engenho e arte.\n", "\"\"\",\"\"\"\n", "Cessem do sábio Grego e do Troiano\n", "As navegações grandes que fizeram;\n", "Cale-se de Alexandro e de Trajano\n", "A fama das vitórias que tiveram;\n", "Que eu canto o peito ilustre Lusitano,\n", "A quem Neptuno e Marte obedeceram:\n", "Cesse tudo o que a Musa antígua canta,\n", "Que outro valor mais alto se alevanta.\n", "\"\"\",\"\"\"\n", "E vós, Tágides minhas, pois criado\n", "Tendes em mim um novo engenho ardente,\n", "Se sempre em verso humilde celebrado\n", "Foi de mim vosso rio alegremente,\n", "Dai-me agora um som alto e sublimado,\n", "Um estilo grandíloquo e corrente,\n", "Porque de vossas águas, Febo ordene\n", "Que não tenham inveja às de Hipoerene.\n", "\"\"\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we will use the functions we defined above for syllabifying an excerpt from Luís de Camões' work \"Os Lusíadas\", and to count the number of syllables in each line:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "