Avatar ·

Word frequency in text

📁 exception, Advanced Python Topics, question, example, work

The Python script below counts the frequency of words in a text (continuous sequences of letters excluding punctuation) and outputs a table of results.

It works correctly. The question is: can the same thing be done more simply (e.g., with fewer lines of code) in Python, Bash, PHP, Perl, or is this the best way?

import sys
import string
file = open(sys.argv[1], "r")
text = file.read()
file.close()
table = string.maketrans("", "")
words = text.lower().split(None)
frequencies = {}                    
Avatar ·

From a security perspective (as already mentioned - you need to be very careful with eval()), performance, and flexibility, it's better to use:

In [6]: import numexpr as neIn [7]: ne.evaluate('1/3+2/3')Out[7]: array(1.0)In [8]: var1=10In [9]: var2=2In [10]: ne.evaluate('var1**var2')Out[10]: array(100, dtype=int32)

It is, by the way, faster for more complex calculations and supports using

Log in to leave an answer

Blogs