DEV Community

Mahboob Hussain
Mahboob Hussain

Posted on

Syllable correction and improvement

Today, the third day of the Sankranthi weekend started dark, cloudy and cool. But that did not deter me from LitTech fun and I got extra spirit nourishment with Manjari and Papon crooning soulfully on YouTube continuously.

Spent the day on the discrepancy pointed yesterday by HydRAW member Nalini Dharanipragada -- "Hyderabad" is four syllables whereas the program was counting three. You can see it in my previous post's screenshot.

When I checked with RapidAPI, I got four syllables for 'Hyderabad'. So, the immediate fix I did was to use RapidAPI. For each word in the input, check the syllables count with RapidAPI as well as the current algorithm. If RapidAPI gives syllables count for all words and there are no network exceptions for any word, then its result is sent to the user. If my algorithm's syllable count matches that of RapidAPI for every word and there are no network exceptions for any word check with RapidAPI, then the result is displayed as using the current algorithm.

Once the above functionality was done, I proceeded to find why the current algorithm was giving Hyderabad three syllables.
Indeed, there was a check for the character "y".

😎 add one if "y" is surrounded by non-vowels and is not in the last word.

But in converting the code from Python to Ruby, I had goofed up. First, I had to convert the string to an array of characters. I should have used split("") or chars on the word, but I used just split. The code was converting the word string to a single array and not an array of characters. I changed to chars. The second boo-boo was I missed an if condition. Added that too. Now the algorithm counts Hyderabad correctly as four syllables.

The interesting part is that Python enumerate over a list gives index and element whereas Ruby each_with_index gives it in the reverse order: element and index. So, for the main loop, I had to write | j, i | instead of | i, j |. The three screenshots given below show the original Python code, my error Ruby code and the fixed Ruby code.

I also added code to write words in a file whenever there is a discrepancy between the syllable count given by the self-algorithm and that from RapidAPI. I can use those words to check the algorithm and improve it.

Deployed the latest code on my cloud server. You can access it at -> https://www.mahboob.tech/poetry/

Image description
Image description

Image description

Top comments (0)