Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

syllables.py This code is taken from the Internet (http://eayd.in/?p=232) written by Emre Aydin. It is used here as a helper for the homework without any

syllables.py
"""This code is taken from the Internet (http://eayd.in/?p=232)
written by Emre Aydin. It is used here as a helper for the homework
without any proof of correctness. The most popular methods exist in
the NLTK module. However, as we do not have this module installed, we
will simply use this module to illustrate the readability indices.
"""
import re
def remove_punctuation(word, L):
i = 0
while i
word = word.replace(L[i],"")
i += 1
return word
def find_num_syllables(word) :
word = word.lower()
word = remove_punctuation(word, [".",",",":","-","!","?","'"])
# exception_add are words that need extra syllables
# exception_del are words that need less syllables
exception_add = ['serious','crucial']
exception_del = ['fortunately','unfortunately']
co_one = ['cool','coach','coat','coal','count','coin','coarse','coup','coif','cook','coign','coiffe','coof','court']
co_two = ['coapt','coed','coinci']
pre_one = ['preach']
syls = 0 #added syllable number
disc = 0 #discarded syllable number
#1) if letters
if len(word)
syls = 1
return syls
#2) if doesn't end with "ted" or "tes" or "ses" or "ied" or "ies", discard "es" and "ed" at the end.
# if it has only 1 vowel or 1 set of consecutive vowels, discard. (like "speed", "fled" etc.)
if word[-2:] == "es" or word[-2:] == "ed" :
doubleAndtripple_1 = len(re.findall(r'[eaoui][eaoui]',word))
if doubleAndtripple_1 > 1 or len(re.findall(r'[eaoui][^eaoui]',word)) > 1 :
if word[-3:] == "ted" or word[-3:] == "tes" or word[-3:] == "ses" or word[-3:] == "ied" or word[-3:] == "ies" :
pass
else :
disc+=1
#3) discard trailing "e", except where ending is "le"
le_except = ['whole','mobile','pole','male','female','hale','pale','tale','sale','aisle','whale','while']
if word[-1:] == "e" :
if word[-2:] == "le" and word not in le_except :
pass
else :
disc+=1
#4) check if consecutive vowels exists, triplets or pairs, count them as one.
doubleAndtripple = len(re.findall(r'[eaoui][eaoui]',word))
tripple = len(re.findall(r'[eaoui][eaoui][eaoui]',word))
disc+=doubleAndtripple + tripple
#5) count remaining vowels in word.
numVowels = len(re.findall(r'[eaoui]',word))
#6) add one if starts with "mc"
if word[:2] == "mc" :
syls+=1
#7) add one if ends with "y" but is not surrouned by vowel
if word[-1:] == "y" and word[-2] not in "aeoui" :
syls +=1
#8) add one if "y" is surrounded by non-vowels and is not in the last word.
for i,j in enumerate(word) :
if j == "y" :
if (i != 0) and (i != len(word)-1) :
if word[i-1] not in "aeoui" and word[i+1] not in "aeoui" :
syls+=1
#9) if starts with "tri-" or "bi-" and is followed by a vowel, add one.
if word[:3] == "tri" and word[3] in "aeoui" :
syls+=1
if word[:2] == "bi" and word[2] in "aeoui" :
syls+=1
#10) if ends with "-ian", should be counted as two syllables, except for "-tian" and "-cian"
if word[-3:] == "ian" :
#and (word[-4:] != "cian" or word[-4:] != "tian") :
if word[-4:] == "cian" or word[-4:] == "tian" :
pass
else :
syls+=1
#11) if starts with "co-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly.
if word[:2] == "co" and word[2] in 'eaoui' :
if word[:4] in co_two or word[:5] in co_two or word[:6] in co_two :
syls+=1
elif word[:4] in co_one or word[:5] in co_one or word[:6] in co_one :
pass
else :
syls+=1
#12) if starts with "pre-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly.
if word[:3] == "pre" and word[3] in 'eaoui' :
if word[:6] in pre_one :
pass
else :
syls+=1
#13) check for "-n't" and cross match with dictionary to add syllable.
negative = ["doesn't", "isn't", "shouldn't", "couldn't","wouldn't"]
if word[-3:] == "n't" :
if word in negative :
syls+=1
else :
pass
#14) Handling the exceptional words.
if word in exception_del :
disc+=1
if word in exception_add :
syls+=1
# calculate the output
return numVowels - disc + syls
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Part 1: How complex is the language used in the text? Create a folder for HW 3. Download the zip file hw3Files.zip from Piazza. Put it in this folder and unzip it. You should see a file named syllables.py which will be a helper module for this homework. Write your program in the same folder as this file and name it hw3Part1.py A few things to get familiar with before solving this part. In this part, you must get familiar with a function called split) that takes a piece of text, and converts it to a list of strings. Here is an example run: > line "Citadel Morning Nevs. Nevs about the Citadel in the porning, pretty selr explanatory ['Citadel, 'Morning Nevs. w aboatthe Citadel', 'ia', 'the. corning,'pretty'elf explanatory.' You will also need to use the function find nun syllables) from the file sy11ables.py which takes as input an English word as a string and that returns the total number of syllables in that word as an integer. The module works even if the word has punctuation symbols, so you do not need to remove those explicitly. Make sure you import this module appropriately into your program findu syllablea("coapater) >> find nus ayllables("acience") >> find_nnsyllables("introduction") Clearly, the second result is incorreet. The module we provided is not a perfect implementation of syllable counting, so you may find errors. It is not your job to fix them, use the module as it is, with errors and all. Do not worry about the mistakes it makes. To properly compute this, we would need to use a Natural Language Processing (NLP) module like NLTK, which we have not installed in this course. 3 5 0 Part 1: How complex is the language used in the text? Create a folder for HW 3. Download the zip file hw3Files.zip from Piazza. Put it in this folder and unzip it. You should see a file named syllables.py which will be a helper module for this homework. Write your program in the same folder as this file and name it hw3Part1.py A few things to get familiar with before solving this part. In this part, you must get familiar with a function called split) that takes a piece of text, and converts it to a list of strings. Here is an example run: > line "Citadel Morning Nevs. Nevs about the Citadel in the porning, pretty selr explanatory ['Citadel, 'Morning Nevs. w aboatthe Citadel', 'ia', 'the. corning,'pretty'elf explanatory.' You will also need to use the function find nun syllables) from the file sy11ables.py which takes as input an English word as a string and that returns the total number of syllables in that word as an integer. The module works even if the word has punctuation symbols, so you do not need to remove those explicitly. Make sure you import this module appropriately into your program findu syllablea("coapater) >> find nus ayllables("acience") >> find_nnsyllables("introduction") Clearly, the second result is incorreet. The module we provided is not a perfect implementation of syllable counting, so you may find errors. It is not your job to fix them, use the module as it is, with errors and all. Do not worry about the mistakes it makes. To properly compute this, we would need to use a Natural Language Processing (NLP) module like NLTK, which we have not installed in this course. 3 5 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions