Skip to content

Instantly share code, notes, and snippets.

@rajacsp
Created February 11, 2019 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajacsp/3ce0d0e1ba01f3e4d748da2ef78a75a6 to your computer and use it in GitHub Desktop.
Save rajacsp/3ce0d0e1ba01f3e4d748da2ef78a75a6 to your computer and use it in GitHub Desktop.
import os
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
porter_stemmer = PorterStemmer()
def get_dir():
dir_path = os.path.dirname(os.path.realpath(__file__))
return dir_path
def get_frequency(filename):
content = ''
with open(filename, encoding="utf8") as f:
for line in f:
#print(line, end = '')
content = content + line.lower()
#print(content)
tokens = word_tokenize(content)
freq = nltk.FreqDist(tokens)
clean_tokens = []
stop_words = stopwords.words('english')
for token in tokens:
# ignore string less than 4 characters
if(len(token) < 4):
continue
if(token in stopwords.words('english')):
continue
token = porter_stemmer.stem(token)
clean_tokens.append(token)
freq = nltk.FreqDist(clean_tokens)
freq.plot(20, cumulative=False)
get_frequency(get_dir() + "/article1.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment