|
|
|
|
|
|
|
|
|
|
|
import pandas as pd |
|
|
from collections import defaultdict |
|
|
from datasets import Dataset, DatasetDict |
|
|
|
|
|
|
|
|
df_train_labels = pd.read_csv("/content/train.tsv", sep="\t") |
|
|
df_valid_labels = pd.read_csv("/content/valid.tsv", sep="\t") |
|
|
|
|
|
|
|
|
labels_dict = dict(zip(df_train_labels["tweet_id"], df_train_labels["label"])) |
|
|
labels_dict.update(dict(zip(df_valid_labels["tweet_id"], df_valid_labels["label"]))) |
|
|
|
|
|
|
|
|
def load_ids(path): |
|
|
with open(path, encoding="utf-8") as f: |
|
|
return set(line.strip() for line in f if line.strip()) |
|
|
|
|
|
train_ids = load_ids("/content/train_ids.txt") |
|
|
dev_ids = load_ids("/content/dev_ids.txt") |
|
|
test_ids = load_ids("/content/test_ids.txt") |
|
|
|
|
|
labels_dict = {str(k): v for k, v in labels_dict.items()} |
|
|
train_ids = set(str(id_) for id_ in train_ids) |
|
|
dev_ids = set(str(id_) for id_ in dev_ids) |
|
|
test_ids = set(str(id_) for id_ in test_ids) |
|
|
|
|
|
def cargar_textos_conll(path): |
|
|
textos = defaultdict(list) |
|
|
with open(path, encoding="utf-8") as f: |
|
|
for line in f: |
|
|
if line.strip(): |
|
|
parts = line.strip().split() |
|
|
if len(parts) == 5: |
|
|
token, doc_id, *_ = parts |
|
|
textos[doc_id].append(token) |
|
|
return textos |
|
|
|
|
|
textos_train = cargar_textos_conll("/content/train_spacy.txt") |
|
|
textos_valid = cargar_textos_conll("/content/valid_spacy.txt") |
|
|
textos = {**textos_train, **textos_valid} |
|
|
|
|
|
|
|
|
def construir_split(ids): |
|
|
data = [] |
|
|
for doc_id in ids: |
|
|
if doc_id in textos and doc_id in labels_dict: |
|
|
text = " ".join(textos[doc_id]) |
|
|
label = int(labels_dict[doc_id]) |
|
|
data.append({"tweet_id": doc_id, "text": text, "label": label}) |
|
|
return Dataset.from_list(data) |
|
|
|
|
|
|
|
|
dataset = DatasetDict({ |
|
|
"train": construir_split(train_ids), |
|
|
"validation": construir_split(dev_ids), |
|
|
"test": construir_split(test_ids), |
|
|
}) |
|
|
|
|
|
from datasets import ClassLabel, Features, Value |
|
|
|
|
|
|
|
|
label_names = ["SIN_PROFESION", "CON_PROFESION"] |
|
|
|
|
|
|
|
|
features = Features({ |
|
|
"tweet_id": Value("string"), |
|
|
"text": Value("string"), |
|
|
"label": ClassLabel(names=label_names) |
|
|
}) |
|
|
|
|
|
|
|
|
for split in dataset: |
|
|
dataset[split] = dataset[split].cast(features) |
|
|
|