testing, removed obsolete files.
Browse files- model_loader.py +0 -31
- models/Corpus_cleaner.py +8 -6
- models/feature_extractor.py +1 -2
- models/random_forest.py +6 -4
model_loader.py
DELETED
|
@@ -1,31 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""Install CalamanCy model properly"""
|
| 3 |
-
|
| 4 |
-
import sys
|
| 5 |
-
import subprocess
|
| 6 |
-
|
| 7 |
-
def install_calamancy_model():
|
| 8 |
-
"""Install the Filipino NLP model"""
|
| 9 |
-
try:
|
| 10 |
-
import calamancy
|
| 11 |
-
print("CalamanCy found, loading model...")
|
| 12 |
-
# This will download and cache the model
|
| 13 |
-
nlp = calamancy.load("tl_calamancy_md-0.2.0")
|
| 14 |
-
print("✅ Model loaded successfully!")
|
| 15 |
-
return True
|
| 16 |
-
except Exception as e:
|
| 17 |
-
print(f"Error loading model: {e}")
|
| 18 |
-
print("Attempting to install via spacy...")
|
| 19 |
-
try:
|
| 20 |
-
subprocess.run([
|
| 21 |
-
sys.executable, "-m", "spacy", "download", "tl_calamancy_md"
|
| 22 |
-
], check=True)
|
| 23 |
-
print("✅ Model installed via spacy!")
|
| 24 |
-
return True
|
| 25 |
-
except Exception as e2:
|
| 26 |
-
print(f"Installation failed: {e2}")
|
| 27 |
-
return False
|
| 28 |
-
|
| 29 |
-
if __name__ == "__main__":
|
| 30 |
-
success = install_calamancy_model()
|
| 31 |
-
sys.exit(0 if success else 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models/Corpus_cleaner.py
CHANGED
|
@@ -4,11 +4,11 @@ import re
|
|
| 4 |
|
| 5 |
input_file = "corpus.csv"
|
| 6 |
output_file = "corpus_clean.csv"
|
| 7 |
-
expected_cols = 3
|
| 8 |
|
| 9 |
clean_rows = []
|
| 10 |
|
| 11 |
-
#
|
| 12 |
with open(input_file, encoding="utf-8") as f:
|
| 13 |
reader = csv.reader(f)
|
| 14 |
header = next(reader)
|
|
@@ -28,13 +28,15 @@ with open(input_file, encoding="utf-8") as f:
|
|
| 28 |
# Skip rows with too few columns
|
| 29 |
print(f"Skipped line {i} (too few columns): {row}")
|
| 30 |
|
| 31 |
-
#
|
| 32 |
df = pd.DataFrame(clean_rows[1:], columns=clean_rows[0])
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
| 36 |
text_col = 'text'
|
| 37 |
|
|
|
|
| 38 |
def clean_text(text):
|
| 39 |
text = text.lower() # lowercase
|
| 40 |
text = re.sub(r'\s+', ' ', text) # remove extra whitespace
|
|
@@ -44,7 +46,7 @@ def clean_text(text):
|
|
| 44 |
|
| 45 |
df[text_col] = df[text_col].apply(clean_text)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
df.to_csv(output_file, index=False, encoding="utf-8")
|
| 49 |
print(f"Clean CSV saved to {output_file}")
|
| 50 |
|
|
|
|
| 4 |
|
| 5 |
input_file = "corpus.csv"
|
| 6 |
output_file = "corpus_clean.csv"
|
| 7 |
+
expected_cols = 3
|
| 8 |
|
| 9 |
clean_rows = []
|
| 10 |
|
| 11 |
+
# Read the CSV manually to handle bad lines
|
| 12 |
with open(input_file, encoding="utf-8") as f:
|
| 13 |
reader = csv.reader(f)
|
| 14 |
header = next(reader)
|
|
|
|
| 28 |
# Skip rows with too few columns
|
| 29 |
print(f"Skipped line {i} (too few columns): {row}")
|
| 30 |
|
| 31 |
+
# Convert to DataFrame
|
| 32 |
df = pd.DataFrame(clean_rows[1:], columns=clean_rows[0])
|
| 33 |
|
| 34 |
+
# Clean the text column for NLP
|
| 35 |
+
|
| 36 |
+
# assign the column containing the sentences to text_col
|
| 37 |
text_col = 'text'
|
| 38 |
|
| 39 |
+
# clean sentences -- uniformed
|
| 40 |
def clean_text(text):
|
| 41 |
text = text.lower() # lowercase
|
| 42 |
text = re.sub(r'\s+', ' ', text) # remove extra whitespace
|
|
|
|
| 46 |
|
| 47 |
df[text_col] = df[text_col].apply(clean_text)
|
| 48 |
|
| 49 |
+
# Save clean CSV
|
| 50 |
df.to_csv(output_file, index=False, encoding="utf-8")
|
| 51 |
print(f"Clean CSV saved to {output_file}")
|
| 52 |
|
models/feature_extractor.py
CHANGED
|
@@ -3,7 +3,7 @@ import sys
|
|
| 3 |
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
# Support running
|
| 7 |
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
BACKEND_DIR = os.path.abspath(os.path.join(CURRENT_DIR, ".."))
|
| 9 |
if BACKEND_DIR not in sys.path:
|
|
@@ -11,7 +11,6 @@ if BACKEND_DIR not in sys.path:
|
|
| 11 |
|
| 12 |
from feature_core import extract_features, load_nlp_model # noqa: E402
|
| 13 |
|
| 14 |
-
|
| 15 |
# Load corpus
|
| 16 |
_df = pd.read_csv("corpus_with_group.csv")
|
| 17 |
|
|
|
|
| 3 |
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
# Support running the file both as a module and as a direct script.
|
| 7 |
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
BACKEND_DIR = os.path.abspath(os.path.join(CURRENT_DIR, ".."))
|
| 9 |
if BACKEND_DIR not in sys.path:
|
|
|
|
| 11 |
|
| 12 |
from feature_core import extract_features, load_nlp_model # noqa: E402
|
| 13 |
|
|
|
|
| 14 |
# Load corpus
|
| 15 |
_df = pd.read_csv("corpus_with_group.csv")
|
| 16 |
|
models/random_forest.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
|
@@ -46,7 +48,7 @@ preprocessor = ColumnTransformer(
|
|
| 46 |
remainder="passthrough"
|
| 47 |
)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 51 |
X, y, test_size=0.2, random_state=42, stratify=y
|
| 52 |
)
|
|
@@ -56,7 +58,7 @@ print(f"Test set size: {len(X_test)}")
|
|
| 56 |
print(f"Training set class distribution: {np.bincount(y_train)}")
|
| 57 |
print(f"Test set class distribution: {np.bincount(y_test)}")
|
| 58 |
|
| 59 |
-
# Train a model with
|
| 60 |
print("\n=== ANALYZING FEATURE IMPORTANCE ===")
|
| 61 |
best_params = {
|
| 62 |
'max_depth': 10,
|
|
@@ -152,7 +154,7 @@ smaller_grid = {
|
|
| 152 |
"classifier__class_weight": ["balanced"]
|
| 153 |
}
|
| 154 |
|
| 155 |
-
cv = StratifiedKFold(n_splits=
|
| 156 |
f1_macro_scorer = make_scorer(f1_score, average="macro")
|
| 157 |
|
| 158 |
grid_search = GridSearchCV(
|
|
@@ -218,7 +220,7 @@ for i, (name, score) in enumerate(results.items()):
|
|
| 218 |
plt.ylim(0, 1)
|
| 219 |
plt.show()
|
| 220 |
|
| 221 |
-
#
|
| 222 |
print("\n=== OPTION 3: ENSEMBLE MODEL ===")
|
| 223 |
|
| 224 |
# Create a few different models
|
|
|
|
| 1 |
+
# THIS FILE IS NOT CRUCIAL TO THE CURRENT SYSTEM. USED ONLY AS A BENCHMARK.
|
| 2 |
+
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
| 48 |
remainder="passthrough"
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# split the data
|
| 52 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 53 |
X, y, test_size=0.2, random_state=42, stratify=y
|
| 54 |
)
|
|
|
|
| 58 |
print(f"Training set class distribution: {np.bincount(y_train)}")
|
| 59 |
print(f"Test set class distribution: {np.bincount(y_test)}")
|
| 60 |
|
| 61 |
+
# Train a model with best params from previous run
|
| 62 |
print("\n=== ANALYZING FEATURE IMPORTANCE ===")
|
| 63 |
best_params = {
|
| 64 |
'max_depth': 10,
|
|
|
|
| 154 |
"classifier__class_weight": ["balanced"]
|
| 155 |
}
|
| 156 |
|
| 157 |
+
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) # Used 5-fold based on planned methodology
|
| 158 |
f1_macro_scorer = make_scorer(f1_score, average="macro")
|
| 159 |
|
| 160 |
grid_search = GridSearchCV(
|
|
|
|
| 220 |
plt.ylim(0, 1)
|
| 221 |
plt.show()
|
| 222 |
|
| 223 |
+
# Try ensemble of best models
|
| 224 |
print("\n=== OPTION 3: ENSEMBLE MODEL ===")
|
| 225 |
|
| 226 |
# Create a few different models
|