0% found this document useful (0 votes)
31 views1 page

File: /home/nayeem/junk/x.txt Page 1 of 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.

Uploaded by

junkforme999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views1 page

File: /home/nayeem/junk/x.txt Page 1 of 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.

Uploaded by

junkforme999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

File: /home/nayeem/junk/x.

txt Page 1 of 1

import sys import sys


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)

You might also like