Count words in a file without counting special characters, numbers, spaces or individual letters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Count words in a file without counting special characters, numbers, spaces or individual letters

In operation B in my code if it should find the number of words without counting up any spaces or special characters, numbers or individual letters. If i test operation b in my code on my test file called breaktest which has special characters, spaces, numbers, individual letters, and one word, thus output should be 1, but its not, it counts the individual letters as words, making my output 4. Here is a pastebin link to my code please help https://pastebin.com/J6zw4pCm

16th Sep 2019, 12:19 PM
gabi jo
gabi jo - avatar
11 Answers
+ 1
Taste Your regex expression counts "ab42cd" as two words, but it has numbers in it so it shouldn't be counted at all. gabi jo W_num = sum([len(i)>1 and i.isalpha() for i in [j.strip(string.punctuation) for j in F_Contents.split()]])
16th Sep 2019, 2:32 PM
Diego
Diego - avatar
+ 1
Sousou if i enter an individual letter in the string, for example in the middle of the special characters, it still adds the individual character to the result m="Hello.! #@+$+'*) World?'" Has no individual letters in it, if i were to make it m="Hello.! #@p+$l+'g*) World?'" It adds the individual letters to the result, equalling HelloplgWorld
16th Sep 2019, 2:30 PM
gabi jo
gabi jo - avatar
+ 1
Thank you anyway for trying, this is frustrating hahaha
16th Sep 2019, 2:31 PM
gabi jo
gabi jo - avatar
+ 1
gabi jo There's clearly 6 words in testfile.txt. They are: "The", "dog", "walked", "across", "the", "room". Am I missing something?
16th Sep 2019, 2:58 PM
Diego
Diego - avatar
+ 1
Oof i need sleep ill try it again i thought i entered breaktest not testfile haha
16th Sep 2019, 3:01 PM
gabi jo
gabi jo - avatar
0
maybe add another filter to check whether the character is more than 1. sorry not so familiar with python. regex also able to do the trick len(re.findall('([a-zA-Z]{2,})',yourString))
16th Sep 2019, 12:56 PM
Taste
Taste - avatar
0
I have no understanding of the regex (re) stuff sadly, and when i try to add another filter i get error after error :(
16th Sep 2019, 12:59 PM
gabi jo
gabi jo - avatar
0
letterfilter(only upper and lower letters): https://code.sololearn.com/cLodGi04Oh3Z/?ref=app
16th Sep 2019, 2:18 PM
Sousou
Sousou - avatar
0
ooooh yeah i didnt think about that case. aaaaaa.... i keep forget to use and instead of && šŸ˜‚
16th Sep 2019, 2:35 PM
Taste
Taste - avatar
0
W_num = sum([len(i)>1 and i.isalpha() for i in [j.strip(string.punctuation) for j in F_Contents.split()]]) ended up with 6 as my word count in testfile but theres only 1 word :((( thank you though
16th Sep 2019, 2:38 PM
gabi jo
gabi jo - avatar
0
Omg omg you were right thank you so much ive been stressing so much over this :)))))
16th Sep 2019, 3:03 PM
gabi jo
gabi jo - avatar