Question
A start up program is provided, which has a MyString class. This class has one data attribute __myString, and two methods. countWord method calculates how
A start up program is provided, which has a MyString class. This class has one data attribute __myString, and two methods.
countWord method calculates how many words in __myString
findMostFrequentChar method finds the most frequent character in __myString (case insensitive)
**Please provide an indentation. Getting few syntax errors in the main method?
**For Reference***Python language
class MyString:
def __init__(self, mystring):
self.__myString = mystring
def getmystring(self):
return self.__myString
def setmystring(self, mystring):
self.__myString = mystring
def countWord(self):
c=0
try:
listInfo=[]
file= open (self.__myString,'r')
for line in file:
listInfo.append(line)
except ValueError:
print("value cannot be converted to float")
except IOError:
print("Error reading/opening file")
except:
print("An error has occured")
for i in range(0,len(listInfo)):
c=c+len(listInfo[i].split())
return c #return number of words in a sentence
## return len(nltk.word_tokenize(self.myString)) ## second way.
def findMostFrequentChar(self):
string = open('TextFile.txt','r')
chars=""
for line in string:
chars=chars+line
completed=[]
# store frequent character in fc
fc = ''
# store frequency of the frequent character in nfc
nfc= 0
#run loop for each chaacter in the string
for ch in chars:
if(ch not in completed):
# initialte the variable
i= 0
# soters teh character count in a string
j= 0
# iterating the loop tills the condition
for i in chars:
if ch.lower()== i.lower():
j+=1
if j>nfc and ch!='':
nfc=j
fc=ch
return fc
def main():
try:
info= input("Please enter anything you like: ")
stringFile =open('TextFile.txt','w')
stringFile.write(info)
stringFile.close()
except IOError:
print("Error opening/writing file")
except Exception as err:
print(err)
InfoFile = MyString("TextFile.txt")
wordcount = InfoFile.countWord()
print("The words you entered in",info, "is", wordcount)
Fc= InfoFile.findMostFrequentChar()
print("The most frequent character is", Fc)
main();
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started