Word is calculated in Python using regular expression

What is the correct way to count English words in a document using regular expression?

I tried:

words=re.findall('\w+', open('text.txt').read().lower())
len(words)

but it seems that I am missing a few words (compared with the word count in gedit). Am I doing it right?

Thank you so much!

+3
source share
2 answers

Using \ w + incorrectly counts words containing apostrophes or hyphens, for example, "cannot" will be considered 2 words. It will also count numbers (lines of digits); β€œ12.345” and β€œ6.7” will be considered two words (β€œ12” and β€œ345”, β€œ6” and β€œ7”).

+3
source

This seems to work as expected.

>>> import re
>>> words=re.findall('\w+', open('/usr/share/dict/words').read().lower())
>>> len(words)
234936
>>> 
bash-3.2$ wc /usr/share/dict/words
  234936  234936 2486813 /usr/share/dict/words

? ?

, :

words=re.findall(r'\w+', open('/usr/share/dict/words').read())
+1

All Articles