Hawaiian Pronunciation [duplicate]

2024/9/20 21:19:03

Hitting a snag with an assignment and thought I'd ask for help. The goal is to be able to pronounce Hawaiian words. Been successful with everything else from another post linked here but only issue is that if there is a space in the original word, it is intentional. So when I try the word 'E komo mai' the return value is eh- kohmoh meye. I put all the code I have used so far with the function calls to test. Any help would be appreciated.

# Variables
vowels = {'a': 'ah','e': 'eh','i': 'ee','o': 'oh','u': 'oo'
}vowelPairs = {'ai': 'eye','ae': 'eye','ao': 'ow','au': 'ow','ei': 'ay','eu': 'eh-oo','iu': 'ew','oi': 'oyo','ou': 'ow','ui': 'ooey','iw': 'v','ew': 'v'
}
# Not used but is part of it so is in dictionary for reference
constants = {'p': 'p','k': 'k','h': 'h','l': 'l','m': 'm','n': 'n'
}# Checks for valid characters in the input
def check(word):valid = ['p', 'k', 'h', 'l', 'm', 'n', 'w','a', 'e', 'i', 'o', 'u', ' ', '\'']for c in word:c = c.lower()if valid.count(c) == 0:print(c, ' is not a valid character.')return Falsereturn True# This function does the transition and pronunciation
def pronounce(word):chars = word.lower()print(len(chars))i = 0result = []while i < len(chars):char = chars[i]# print(char)if i < len(chars) - 1:print(char + chars[i + 1])pair = char + chars[i + 1]tr = vowelPairs.get(pair)if tr is None:tr = vowels.get(char)else:i = i + 1else:tr = vowels.get(char)if tr is not None and i < len(chars) - 1:tr = tr + '-'result.append(tr or char)i = i + 1wordResult = ''.join(result)print(wordResult)return wordResultcheck('E komo mai')
pronounce('E komo mai')
Answer

I first split the word where-ever there was a space. This allows you to handle them seperately. I then added a few more if-statements and logic to the process.

Here is what I came up with:

# Variables
vowels = {'a': 'ah','e': 'eh','i': 'ee','o': 'oh','u': 'oo'
}vowelPairs = {'ai': 'eye','ae': 'eye','ao': 'ow','au': 'ow','ei': 'ay','eu': 'eh-oo','iu': 'ew','oi': 'oyo','ou': 'ow','ui': 'ooey','iw': 'v','ew': 'v'
}
# Not used but is part of it so is in dictionary for reference
constants = {'p': 'p','k': 'k','h': 'h','l': 'l','m': 'm','n': 'n'
}# Checks for valid characters in the input
def check(word):valid = ['p', 'k', 'h', 'l', 'm', 'n', 'w','a', 'e', 'i', 'o', 'u', ' ', '\'']for c in word:c = c.lower()if valid.count(c) == 0:print(c, ' is not a valid character.')return Falsereturn Truedef soundout(part):lower_part = part.lower()result = ''part_len = len(lower_part)if part_len < 2:result = vowels.get(lower_part)else:for i, char in enumerate(lower_part):if constants.get(char):if (i-1) < 0:result += charelif vowels.get(lower_part[i-1]):result += "-" + charelse:if i < part_len-1:pair = char + lower_part[i+1]if vowelPairs.get(pair):result += vowelPairs.get(pair)elif vowels.get(char):result += vowels.get(char)elif vowels.get(char):if ((i-1) > -1) and vowels.get(lower_part[i-1]):passelse:result += vowels.get(char)return resultdef pronounce(word):wordResult = []parts = word.split(' ')for part in parts:wordResult.append(soundout(part))return(' '.join(wordResult).capitalize())# Eh koh-moh meye
check('E komo mai')
pronounce('E komo mai')

OUTPUT:

'Eh koh-moh meye'
https://en.xdnf.cn/q/119285.html

Related Q&A

mutiline python script in html (pyscript)

Ive tried to use pyscript in html but i can only get it to work in one line of code can somebody help me get it to work for the following code? def vpn(website):from selenium import webdriverfrom sele…

How to write an output of a command to stdout and a file in Python3?

I have a Windows command which I want to write to stdout and to a file. For now, I only have 0 string writen in my file:#!/usr/bin/env python3 #! -*- coding:utf-8 -*-import subprocesswith open(auto_cha…

Mongodb adding a new field in an existing document, with specific position

I am facing this issue where I need to insert a new field in an existing document at a specific position. Sample document: { "name": "user", "age" : "21", "…

how to check every 3 x 3 box in sudoku?

I am trying to build a sudoku solver without much googling around. Right now, I am working on a function to test whether the board is valid, which I will use later in a loop. This is what the function …

multiple model accuracy json result format using python

I am building a multiple model and i am getting results with 7 models accuracy, i need those results with a proper json format.My multiple model building code will be like thisseed = 7"prepare mod…

Calculate Time Difference based on Conditionals

I have a dataframe that looks something like this (actual dataframe is millions of rows):ID Category Site Task Completed Access Completed1 A X 1/2/22 12:00:00AM 1/1/22 12:00:00 AM1 A Y 1/3/22 12:00:00A…

Cannot open jpg images with PIL or open()

I am testing to save ImageField in Django, but for some reason all the *.jpg files Ive tried dont work while the one png I had works. Using django shell in WSL VCode terminal. python 3.7 django 3.0 pil…

how to delete tensorflow model before retraining

I cant retrain my image classifier with new images, I get the following error:AssertionError: Export directory already exists. Please specify a different export directory: /tmp/saved_models/1/How do I …

Use beautifulsoup to scrape a table within a webpage?

I am scraping a county website that posts emergency calls and their locations. I have found success webscraping basic elements, but am having trouble scraping the rows of the table. (Here is an example…

Encrypt folder or zip file using python

So I am trying to encrypt a directory using python and Im not sure what the best way to do that is. I am easily able to turn the folder into a zip file, but from there I have tried looking up how to en…