Python to detect dipthongs in English

Viewed 30

Is there a Python library that can detect/highlight all the dipthongs (in normal spelling not IPA) in a given English text?

1 Answers

If you are defining 'dipthongs' as just letter pairs/combinations that typically indicate double vowel sounds ('ee', 'ou', etc.), then something like the following would work to go through and search for letter combinations from a pre-defined set:

while len(text) > 0:
    # Iterate over dipthongs
    for d in DIPTHONGS:
        # If dipthong is in remaining text
        if d in text:
            # Partition remaining text
            before, dip, after = text.partition(d)
            # Append the part before and the highlighted dipthong to highlighted_text
            highlighted_text = highlighted_text + before
            highlighted_text = highlighted_text + f'*{d}*'
            # Update text to the remaining text
            text = after
    else:
        # No dipthongs found, so append remainder of text to highlighted_text
        highlighted_text = highlighted_text + text
        text = ''

print(highlighted_text)

Output:

Output

I have used asterisks for the highlight as it's quick and easy but you could easily adapt this use colours, or whatever you need for your use case.

I can't think of any examples off the top of my head, but I suspect there are cases where the spelling is dipthong-like but the pronuncation is not a dipthong or vice-versa (because English is like that). To actually take the pronunciation into consideration you could use something like the NLTK CMUdict corpus - see section 4.2 of https://www.nltk.org/book/ch02.html to get started.

Related