#Construct a news articl classifier using Naive Bayes Classifier import string import math import random #This just returns a list of all the lines of text in #the source file. The source files are arranged so that #each snippet of a news article occupies a single line. def get_data(filename): linelist=[] f=open(filename,'r') for item in f: #item=item.replace("\u2019","'") linelist.append(item) return linelist #takes a string and returns a cleaned-up list of words in the string. #You will apply this in turn to each line of text in the source dataset. def text_transform(text): stopwords={'ourselves', 'hers', 'between', 'yourself', 'but', 'again',\ 'there', 'about', 'once', 'during', 'out', 'very', 'having',\ 'with', 'they', 'own', 'an', 'be', 'some', 'for', 'do', 'its',\ 'yours', 'such', 'into', 'of', 'most', 'itself', 'other',\ 'off', 'is', 's', 'am', 'or', 'who', 'as', 'from', 'him', \ 'each', 'the', 'themselves', 'until', 'below', 'are', 'we', \ 'these', 'your', 'his', 'through', 'don', 'nor', 'me', 'were',\ 'her', 'more', 'himself', 'this', 'down', 'should', 'our', 'their',\ 'while', 'above', 'both', 'up', 'to', 'ours', 'had', 'she', 'all',\ 'no', 'when', 'at', 'any', 'before', 'them', 'same', 'and', 'been',\ 'have', 'in', 'will', 'on', 'does', 'yourselves', 'then', 'that',\ 'because', 'what', 'over', 'why', 'so', 'can', 'did', 'not', 'now',\ 'under', 'he', 'you', 'herself', 'has', 'just', 'where', 'too',\ 'only', 'myself', 'which', 'those', 'i', 'after', 'few', 'whom',\ 't', 'being', 'if', 'theirs', 'my', 'against', 'a', 'by', 'doing',\ 'it', 'how', 'further', 'was', 'here', 'than'} text = text.translate(str.maketrans('', '', string.punctuation)) text = [word.lower() for word in text.split() if word.lower() not in stopwords ] return text