Grapheme to Phoneme Conversion
This guide demonstrates how to perform Grapheme to Phoneme Conversion with Phonexia Speech Platform 4. You can find a high-level description in the Grapheme to Phoneme Conversion article.
At the end of this guide, you'll find the full Python code example that combines all the steps that will first be discussed separately. This guide should give you a comprehensive understanding on how to integrate Grapheme to Phoneme Conversion in your own projects.
Prerequisites
Follow the prerequisites for setup of Virtual Appliance and Python environment as described in the Task lifecycle code examples.
Run Grapheme to Phoneme Conversion
To run Grapheme to Phoneme Conversion, you should start by sending a POST
request to the
/api/technology/grapheme-to-phoneme-conversion
endpoint. language is a mandatory parameter. Words to convert must be provided
in the words field of the request body. Individual strings in the words list
can consist of multiple words separated by + (e.g., "hello+world" is a valid
word string). See the
endpoint documentation
for more details.
In Python, you can do this as follows:
import json
import requests
VIRTUAL_APPLIANCE_ADDRESS = "http://<virtual-appliance-address>:8000" # Replace with your address
ENDPOINT_URL = f"{VIRTUAL_APPLIANCE_ADDRESS}/api/technology/grapheme-to-phoneme-conversion"
params = {"language": "cs-CZ"}
words = ["ahoj", "čau+světe"]
start_task_response = requests.post(
url=ENDPOINT_URL,
params=params,
json={"words": words}
)
print(start_task_response.status_code) # Should print '202'
Note that to get meaningful results, the language parameter has to match the
language the words come from. See the documentation for the list of
supported languages.
If the task has been successfully accepted, the 202 code will be returned
together with a unique task ID in the response body. The task isn't processed
immediately, but only scheduled for processing. You can check the current task
status by polling for the result.
Polling
To obtain the final result, periodically query the task status until the task
state changes to done, failed, or rejected. The general polling procedure
is described in detail in the
Task lifecycle code examples.
Result of Grapheme to Phoneme Conversion
The result field of the task contains the list of word_pronunciations. Each
word pronunciation contains information about the input word, pronunciation,
and pronunciation_source.
For our sample request, the task result should look as follows:
{
"word_pronunciations": [
{
"word": "ahoj",
"pronunciations": [
{
"pronunciation": "a h o j",
"source": "dictionary"
}
]
},
{
"word": "čau+světe",
"pronunciations": [
{
"pronunciation": "tS a u s v j e t e",
"source": "dictionary"
}
]
}
]
}
Full Python code
Here is the full example on how to run the Grapheme to Phoneme Conversion technology. The code is slightly adjusted and wrapped into functions for better readability. Refer to the Task lifecycle code examples for a generic code template, applicable to all technologies.
import json
import requests
import time
VIRTUAL_APPLIANCE_ADDRESS = "http://<virtual-appliance-address>:8000" # Replace with your address
ENDPOINT_URL = f"{VIRTUAL_APPLIANCE_ADDRESS}/api/technology/grapheme-to-phoneme-conversion"
def poll_result(polling_url, polling_interval=5):
"""Poll the task endpoint until processing completes."""
while True:
polling_task_response = requests.get(polling_url)
polling_task_response.raise_for_status()
polling_task_response_json = polling_task_response.json()
task_state = polling_task_response_json["task"]["state"]
if task_state in {"done", "failed", "rejected"}:
break
time.sleep(polling_interval)
return polling_task_response
def run_task(params, words):
"""Create a task and wait for results."""
start_task_response = requests.post(
url=ENDPOINT_URL,
params=params,
json={"words": words}
)
start_task_response.raise_for_status()
polling_url = start_task_response.headers["Location"]
task_result = poll_result(polling_url)
return task_result.json()
# Run Grapheme to Phoneme Conversion
params = {"language": "cs-CZ"}
words = ["ahoj", "čau+Phonexia"]
print(f"Running Grapheme to Phoneme Conversion.")
task = run_task(params=params, words=words)
task_result = task["result"]
print(json.dumps(task_result, indent=2))