Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?

How would I go about counting the words in a sentence? I'm using Python.

For example, I might have the string:

string = "I     am having  a   very  nice  23!@$      day. " 

That would be 7 words. I'm having trouble with the random amount of spaces after/before each word as well as when numbers or symbols are involved.

like image 851
HossBender Avatar asked Oct 16 '13 17:10

HossBender


People also ask

How do you count specific words in Python?

Using the count() Function The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.


2 Answers

str.split() without any arguments splits on runs of whitespace characters:

>>> s = 'I am having a very nice day.' >>>  >>> len(s.split()) 7 

From the linked documentation:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

like image 84
arshajii Avatar answered Sep 16 '22 22:09

arshajii


You can use regex.findall():

import re line = " I am having a very nice day." count = len(re.findall(r'\w+', line)) print (count) 
like image 42
karthikr Avatar answered Sep 17 '22 22:09

karthikr