0 оценок0% нашли этот документ полезным (0 голосов)
13 просмотров1 страница
This Python code takes a user input string, cleans it by removing non-alphabetic characters and converting to lowercase. It then splits the string into individual words and counts the frequency of each word, storing the results in a dictionary. The dictionary is then pretty printed to output the word frequencies. Any input containing numbers, multiple special characters or certain special characters not at the end of words will result in an "Invalid input" message.
This Python code takes a user input string, cleans it by removing non-alphabetic characters and converting to lowercase. It then splits the string into individual words and counts the frequency of each word, storing the results in a dictionary. The dictionary is then pretty printed to output the word frequencies. Any input containing numbers, multiple special characters or certain special characters not at the end of words will result in an "Invalid input" message.
This Python code takes a user input string, cleans it by removing non-alphabetic characters and converting to lowercase. It then splits the string into individual words and counts the frequency of each word, storing the results in a dictionary. The dictionary is then pretty printed to output the word frequencies. Any input containing numbers, multiple special characters or certain special characters not at the end of words will result in an "Invalid input" message.
import re import re from pprint import pprint from pprint import pprint para=input() para=input() para=para.lower() para=para.lower() para=para.rstrip() para=para.rstrip() flag=1 flag=1 splited=para.split(' ') splited=para.split(' ') for c in splited: for c in splited: if re.search('\d',c): if re.search ('\d',c): flag=0 flag=0 print("Invalid input") < sys.exit() < if re.search('\W\W',c): if re.search('\W \W',c): flag=0 flag=0 print("Invalid input") < sys.exit() < if re.search('\W',c): if re.search ('\W',c): flag=0 < lastch=c[-1] < if re.search('[,.?:;!]$',c): if re.search ('[,.?:;!]$',c): flag=1 flag=1 temp2=c[:-1] | else: if re.search('[,.?:;!]',temp2): | flag=0 flag=0 < print("flag====",flag) < < if flag==0: < print("Invalid input") < sys.exit() < < < if flag==0: if flag==0: print("Invalid input") print("Invalid input") sys.exit() sys.exit() < l1=[] l1=[] for x in splited: for x in splited: > temp=splited.count (x) if re.search('\W$',x): if re.search('\W $',x): pos=splited.index(x) < x=x[:-1] x=x[:-1] splited[pos]=x < < temp=splited.count(x) < l1.append(temp) l1.append(temp) < d={ch:count for(ch,count) in zip(splited,l1)} d={ch:count for (ch,count) in zip(splited,l1)} pprint(d) pprint(d)