-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnaivebayes.py
More file actions
63 lines (47 loc) · 1.75 KB
/
naivebayes.py
File metadata and controls
63 lines (47 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import nltk, string
import pandas as pd
from nltk import FreqDist
from nltk.classify import NaiveBayesClassifier
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
nltk.download("punkt")
nltk.download("stopwords")
nltk.download("wordnet")
CSV_PATH = r"C:\Users\Jonathan Philips\Coding\nltk-text-tone-processing\resources\responses.csv"
stop_words = set(stopwords.words("english"))
lemmatizer = WordNetLemmatizer()
def clean_tokens(text):
tokens = word_tokenize(text.lower())
return [
lemmatizer.lemmatize(word)
for word in tokens
if word not in stop_words and word not in string.punctuation
]
def train_classifier():
try:
df = pd.read_csv(CSV_PATH, names=["text", "sentiment", "timestamp"], skiprows=1)
except Exception as e:
print("Error loading CSV:", e)
return None, []
documents = [
(clean_tokens(row["text"]), row["sentiment"]) for _, row in df.iterrows()
]
all_words = FreqDist(word for tokens, _ in documents for word in tokens)
word_features = list(all_words)[:2000]
def document_features(document):
words = set(document)
return {f"contains({w})": (w in words) for w in word_features}
featuresets = [(document_features(d), c) for (d, c) in documents]
if not featuresets:
return None, []
train_set = featuresets
classifier = NaiveBayesClassifier.train(train_set)
return classifier, word_features
classifier, word_features = train_classifier()
def classify_comment(text):
if not classifier:
return "unknown"
tokens = clean_tokens(text)
features = {f"contains({w})": (w in tokens) for w in word_features}
return classifier.classify(features)