Commit ·
33aafb8
1
Parent(s): c6f8f2f
Upload id_stance.py with huggingface_hub
Browse files- id_stance.py +137 -0
id_stance.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
import datasets
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
from nusacrowd.utils import schemas
|
| 9 |
+
from nusacrowd.utils.configs import NusantaraConfig
|
| 10 |
+
from nusacrowd.utils.constants import Tasks
|
| 11 |
+
|
| 12 |
+
_CITATION = """\
|
| 13 |
+
@INPROCEEDINGS{8629144,
|
| 14 |
+
author={R. {Jannati} and R. {Mahendra} and C. W. {Wardhana} and M. {Adriani}},
|
| 15 |
+
booktitle={2018 International Conference on Asian Language Processing (IALP)},
|
| 16 |
+
title={Stance Classification Towards Political Figures on Blog Writing},
|
| 17 |
+
year={2018},
|
| 18 |
+
volume={},
|
| 19 |
+
number={},
|
| 20 |
+
pages={96-101},
|
| 21 |
+
}
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
_LANGUAGES = ["ind"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
|
| 25 |
+
_LOCAL = False
|
| 26 |
+
|
| 27 |
+
_DATASETNAME = "id_stance"
|
| 28 |
+
_DESCRIPTION = """\
|
| 29 |
+
Stance Classification Towards Political Figures on Blog Writing.
|
| 30 |
+
This dataset contains dataset from the second research, which is combined from the first research and new dataset.
|
| 31 |
+
The dataset consist of 337 data, about five target and every target have 1 different event.
|
| 32 |
+
Two label are used: 'For' and 'Againts'.
|
| 33 |
+
1. For - the text that is created by author is support the target in an event
|
| 34 |
+
2. Against - the text that is created by author is oppose the target in an event
|
| 35 |
+
"""
|
| 36 |
+
_HOMEPAGE = "https://github.com/reneje/id_stance_dataset_article-Stance-Classification-Towards-Political-Figures-on-Blog-Writing"
|
| 37 |
+
_LICENSE = "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License"
|
| 38 |
+
_URLs = {
|
| 39 |
+
_DATASETNAME: "https://raw.githubusercontent.com/reneje/id_stance_dataset_article-Stance-Classification-Towards-Political-Figures-on-Blog-Writing/master/dataset_stance_2_label_2018_building_by_rini.csv"
|
| 40 |
+
}
|
| 41 |
+
_SUPPORTED_TASKS = [Tasks.TEXTUAL_ENTAILMENT]
|
| 42 |
+
_SOURCE_VERSION = "1.0.0"
|
| 43 |
+
_NUSANTARA_VERSION = "1.0.0"
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def parse_list(content):
|
| 47 |
+
if (not content):
|
| 48 |
+
return []
|
| 49 |
+
try:
|
| 50 |
+
return json.loads(content)
|
| 51 |
+
except:
|
| 52 |
+
return json.loads("[\"" + content[1:-1].replace("\"", "\\\"") + "\"]")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class IdStance(datasets.GeneratorBasedBuilder):
|
| 56 |
+
"""The ID Stance dataset is annotated with a label whether the article is in favor of the person in the context of the event"""
|
| 57 |
+
|
| 58 |
+
BUILDER_CONFIGS = [
|
| 59 |
+
NusantaraConfig(
|
| 60 |
+
name="id_stance_source",
|
| 61 |
+
version=datasets.Version(_SOURCE_VERSION),
|
| 62 |
+
description="IdStance source schema",
|
| 63 |
+
schema="source",
|
| 64 |
+
subset_id="id_stance",
|
| 65 |
+
),
|
| 66 |
+
NusantaraConfig(
|
| 67 |
+
name="id_stance_nusantara_pairs",
|
| 68 |
+
version=datasets.Version(_NUSANTARA_VERSION),
|
| 69 |
+
description="IdStance Nusantara schema",
|
| 70 |
+
schema="nusantara_pairs",
|
| 71 |
+
subset_id="id_stance",
|
| 72 |
+
),
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
DEFAULT_CONFIG_NAME = "id_stance_source"
|
| 76 |
+
|
| 77 |
+
def _info(self):
|
| 78 |
+
if self.config.schema == "source":
|
| 79 |
+
features = datasets.Features(
|
| 80 |
+
{
|
| 81 |
+
"person": datasets.Value("string"),
|
| 82 |
+
"event": datasets.Value("string"),
|
| 83 |
+
"title": datasets.Value("string"),
|
| 84 |
+
"content": datasets.Value("string"),
|
| 85 |
+
"stance_final": datasets.Value("string"),
|
| 86 |
+
}
|
| 87 |
+
)
|
| 88 |
+
elif self.config.schema == "nusantara_pairs":
|
| 89 |
+
features = schemas.pairs_features(["for", "against", "no"])
|
| 90 |
+
|
| 91 |
+
return datasets.DatasetInfo(
|
| 92 |
+
description=_DESCRIPTION,
|
| 93 |
+
features=features,
|
| 94 |
+
homepage=_HOMEPAGE,
|
| 95 |
+
license=_LICENSE,
|
| 96 |
+
citation=_CITATION,
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 100 |
+
data_path = Path(dl_manager.download_and_extract(_URLs[_DATASETNAME]))
|
| 101 |
+
data_files = {
|
| 102 |
+
"train": data_path,
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
return [
|
| 106 |
+
datasets.SplitGenerator(
|
| 107 |
+
name=datasets.Split.TRAIN,
|
| 108 |
+
gen_kwargs={"filepath": data_files["train"]},
|
| 109 |
+
),
|
| 110 |
+
]
|
| 111 |
+
|
| 112 |
+
def _generate_examples(self, filepath: Path):
|
| 113 |
+
df = pd.read_csv(filepath, sep=";", header="infer", keep_default_na=False).reset_index()
|
| 114 |
+
df.columns = ["id", "person", "event", "title", "content", "stance_final", ""]
|
| 115 |
+
df.content = df.content.apply(parse_list)
|
| 116 |
+
|
| 117 |
+
if self.config.schema == "source":
|
| 118 |
+
for row in df.itertuples():
|
| 119 |
+
ex = {
|
| 120 |
+
"person": row.person,
|
| 121 |
+
"event": row.event,
|
| 122 |
+
"title": row.title,
|
| 123 |
+
"content": " ".join(row.content),
|
| 124 |
+
"stance_final": row.stance_final
|
| 125 |
+
}
|
| 126 |
+
yield row.id, ex
|
| 127 |
+
elif self.config.schema == "nusantara_pairs":
|
| 128 |
+
for row in df.itertuples():
|
| 129 |
+
ex = {
|
| 130 |
+
"id": row.id,
|
| 131 |
+
"text_1": row.person + " | " + row.event,
|
| 132 |
+
"text_2": " ".join([row.title] + row.content),
|
| 133 |
+
"label": 'against' if row.stance_final == 'againts' else row.stance_final
|
| 134 |
+
}
|
| 135 |
+
yield row.id, ex
|
| 136 |
+
else:
|
| 137 |
+
raise ValueError(f"Invalid config: {self.config.name}")
|