# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pickle

# +
train_sentences = []
train_ancorra_tags = []
train_features = []

dev_sentences= []
dev_ancorra_tags = []
dev_features = []
# -

with open('Data/Training_sentences.pkl', 'rb') as f:
    train_sentences = pickle.load(f)
with open('Data/Training_ancorra_tags.pkl', 'rb') as f:
    train_ancorra_tags = pickle.load(f)
with open('Data/Training_features.pkl', 'rb') as f:
    train_features = pickle.load(f)
with open('Data/Development_sentences.pkl', 'rb') as f:
    dev_sentences = pickle.load(f)
with open('Data/Development_ancorra_tags.pkl', 'rb') as f:
    dev_ancorra_tags = pickle.load(f)
with open('Data/Development_features.pkl', 'rb') as f:
    dev_features = pickle.load(f)




# %%time
from sklearn_crfsuite import CRF
crf = CRF(
    algorithm='lbfgs',
    c1=0.01,
    c2=0.1,
    max_iterations=100,
    all_possible_transitions=True
)
crf.fit(train_features, train_ancorra_tags)

from sklearn_crfsuite import metrics
from sklearn_crfsuite import scorers

# +
pred_ancorra_tags = crf.predict(dev_features)
print("F1 score on Dev Data ")
print(metrics.flat_f1_score(dev_ancorra_tags, pred_ancorra_tags, average='weighted',labels=crf.classes_))

### Look at class wise score
print(metrics.flat_classification_report(
    dev_ancorra_tags, pred_ancorra_tags, labels=crf.classes_, digits=3
))

# +
test_sentences = []
test_ancorra_tags = []
test_features = []

with open('Data/Testing_sentences.pkl', 'rb') as f:
    test_sentences = pickle.load(f)
with open('Data/Testing_ancorra_tags.pkl', 'rb') as f:
    test_ancorra_tags = pickle.load(f)
with open('Data/Testing_features.pkl', 'rb') as f:
    test_features = pickle.load(f)
# -

pred_test_ancorra_tags = crf.predict(test_features)
print("F1 score on Test Data ")
print(metrics.flat_f1_score(test_ancorra_tags, pred_test_ancorra_tags, average='weighted',labels=crf.classes_))
### Look at class wise score
print(metrics.flat_classification_report(
    test_ancorra_tags, pred_test_ancorra_tags, labels=crf.classes_, digits=3
))

with open('crf_model.pkl', 'wb') as model:
    pickle.dump(crf, model)





StarcoderdataJupyterScriptsDedupFiltered