-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
147 lines (123 loc) · 4.86 KB
/
main.py
File metadata and controls
147 lines (123 loc) · 4.86 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import pandas as pd
import numpy as np
import time
import random
import nltk
import sys
import os
from sklearn.datasets import fetch_20newsgroups
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
from data.data_loader import load_data, split_data
from data.data_model import EmailDataModel
from data.preprocessing import EmailPreprocessor
from utils.feature_extraction import TfidfFeatureExtractor
from models.chained_model import ChainedClassifier
from models.hierarchical_model import HierarchicalClassifier
from models.classifiers.random_forest import RandomForestModel
from models.classifiers.svm import SVMModel
from config.config import TYPE2_CATEGORIES, TYPE3_CATEGORIES, TYPE4_CATEGORIES
def create_sample_data(n_samples=500):
print(f"\nGenerating sample data with {n_samples} samples...")
start = time.time()
data = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))
texts = data.data[:n_samples]
print("Preprocessing texts...")
preprocessor = EmailPreprocessor()
processed_texts = []
for i, text in enumerate(texts):
if i % 100 == 0:
print(f"Processing text {i}/{len(texts)}")
try:
processed = preprocessor.preprocess(text)
processed_texts.append(processed)
except Exception as e:
processed_texts.append(preprocessor.clean_email(text))
type2_labels = [random.choice(TYPE2_CATEGORIES) for _ in range(len(processed_texts))]
type3_labels = [random.choice(TYPE3_CATEGORIES) for _ in range(len(processed_texts))]
type4_labels = [random.choice(TYPE4_CATEGORIES) for _ in range(len(processed_texts))]
df = pd.DataFrame({
'email_text': processed_texts,
'type2_label': type2_labels,
'type3_label': type3_labels,
'type4_label': type4_labels
})
end = time.time()
print(f"Sample data created in {end-start:.2f} seconds")
return df
def main():
print("\n*** EMAIL CLASSIFICATION SYSTEM ***")
print("Starting at", time.strftime("%H:%M:%S", time.localtime()))
try:
print("\nTrying to load data from file...")
data = load_data()
if data is None or len(data) < 10:
print("No good data found. Creating sample data...")
data = create_sample_data()
except Exception as e:
print(f"Error loading data: {e}")
data = create_sample_data()
print(f"\nData loaded with {len(data)} samples")
train_data, test_data = split_data(data)
print(f"Training set: {len(train_data['X'])} samples")
print(f"Testing set: {len(test_data['X'])} samples")
data_model = EmailDataModel.from_dict(train_data, test_data)
print("\nExtracting features from text...")
feature_extractor = TfidfFeatureExtractor(max_feat=1000)
X_train = feature_extractor.fit_transform(data_model.X_train)
X_test = feature_extractor.transform(data_model.X_test)
print("\n" + "=" * 50)
print("DESIGN 1: CHAINED CLASSIFICATION")
print("=" * 50)
chained_model = ChainedClassifier(base_classifier=RandomForestModel)
chained_model.train(
X_train,
data_model.y_train_type2,
data_model.y_train_type3,
data_model.y_train_type4
)
type2_pred, type3_pred, type4_pred = chained_model.predict(X_test)
chained_results = chained_model.print_results(
data_model.y_test_type2,
data_model.y_test_type3,
data_model.y_test_type4,
type2_pred,
type3_pred,
type4_pred
)
print("\n" + "=" * 50)
print("DESIGN 2: HIERARCHICAL CLASSIFICATION")
print("=" * 50)
hierarchical_model = HierarchicalClassifier(base_classifier=SVMModel)
hierarchical_model.train(
X_train,
data_model.y_train_type2,
data_model.y_train_type3,
data_model.y_train_type4
)
type2_pred, type3_pred, type4_pred = hierarchical_model.predict(X_test)
hierarchical_results = hierarchical_model.print_results(
data_model.y_test_type2,
data_model.y_test_type3,
data_model.y_test_type4,
type2_pred,
type3_pred,
type4_pred
)
print("\n" + "=" * 50)
print("FINAL COMPARISON")
print("=" * 50)
print(f"Chained Overall Accuracy: {chained_results['overall_acc']:.4f}")
print(f"Hierarchical Overall Accuracy: {hierarchical_results['overall_acc']:.4f}")
print(f"Winner: {'Chained' if chained_results['overall_acc'] > hierarchical_results['overall_acc'] else 'Hierarchical'}")
print("\nClassification completed at", time.strftime("%H:%M:%S", time.localtime()))
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"ERROR: {e}")
print("Something went wrong! Check the error above.")