Python code for counting the number of English words

  • 2020-04-02 09:28:21
  • OfStack

Word for the English word statistics is also very good, you might as well try. If you don't have word installed and you're a programmer, you can use my code. Through the test, word's statistical result is 18674, and the software's statistical result is 18349, with a difference of less than 2%, which can be used as a reference.

The code is as follows:
 
# -*- coding: utf-8 -*- 

import os,sys 
info = os.getcwd() # Gets the current file name  
fin = open(u' Google C++ Programming code specification .txt') 

info = fin.read() 
alist = info.split(' ') #  Divide the article into Spaces  

fout = open(u'count.txt', 'w') 
fout.write('n'.join(alist)) #  You can also see this with the line number of the text file  
##fout.write('%s' % alist) 
fout.close() 

allen = len(alist) #  Total number of words  
nulen = alist.count('') #  Number of Spaces  
print "words' number is",allen 
print "null number is",nulen 
print "poor words number is", allen-nulen #  The actual number of words  

fin.close() 

Related articles: