Datasets:
Tasks:
Text Classification
Modalities:
Text
Formats:
text
Sub-tasks:
sentiment-classification
Languages:
English
Size:
1M - 10M
License:
Upload 2 files
Browse files- .gitattributes +1 -0
- HF_data.txt +3 -0
- HF_dataset.py +143 -0
.gitattributes
CHANGED
|
@@ -52,3 +52,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 52 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 53 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 54 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 52 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 53 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 54 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
| 55 |
+
HF_data.txt filter=lfs diff=lfs merge=lfs -text
|
HF_data.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:393027e9869b5c02bdc216c92fced1ad7532c5bf031773c127ed09b5ed339325
|
| 3 |
+
size 183289353
|
HF_dataset.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from os.path import exists
|
| 3 |
+
|
| 4 |
+
import datasets
|
| 5 |
+
from datasets.tasks import TextClassification
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import json
|
| 10 |
+
|
| 11 |
+
from transformers import AutoTokenizer
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
logger = datasets.logging.get_logger(__name__)
|
| 15 |
+
|
| 16 |
+
## Constants
|
| 17 |
+
USE_FULL_DATASET = True
|
| 18 |
+
PROJECT_PATH = "./"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _define_columns(example):
|
| 23 |
+
text_splited = example["text"].split('\t')
|
| 24 |
+
return {"text": text_splited[1].strip(), "labels": int(text_splited[0])}
|
| 25 |
+
|
| 26 |
+
class Sentiment(datasets.GeneratorBasedBuilder):
|
| 27 |
+
'''Custom Dataset created using the HuggingFace api so we can
|
| 28 |
+
use all of their's api on the dataset'''
|
| 29 |
+
def _info(self):
|
| 30 |
+
class_names = ["negative", "positive"]
|
| 31 |
+
return datasets.DatasetInfo(
|
| 32 |
+
description="Our nice dataset in HF format",
|
| 33 |
+
features=datasets.Features(
|
| 34 |
+
{"text": datasets.Value("string"),
|
| 35 |
+
"labels": datasets.ClassLabel(num_classes=2, names=class_names)} # Value("int32")
|
| 36 |
+
),
|
| 37 |
+
supervised_keys=("text", "labels"),
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def _split_generators(self, _):
|
| 41 |
+
"""Returns SplitGenerators."""
|
| 42 |
+
|
| 43 |
+
data_dir = "./"
|
| 44 |
+
|
| 45 |
+
data = load_dataset("text", data_files="./HF_data.txt")
|
| 46 |
+
data = data.map(_define_columns)
|
| 47 |
+
|
| 48 |
+
texts_dataset_clean = data["train"].train_test_split(train_size=0.95, seed=12345)
|
| 49 |
+
# Rename the default "test" split to "validation"
|
| 50 |
+
texts_dataset_clean["validation"] = texts_dataset_clean.pop("test")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
for split, dataset in texts_dataset_clean.items():
|
| 54 |
+
dataset.to_json(data_dir + f"twitter-sentiment-analysis-{split}.jsonl")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
return [
|
| 58 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": os.path.join(data_dir, "twitter-sentiment-analysis-train.jsonl")}),
|
| 59 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": os.path.join(data_dir, "twitter-sentiment-analysis-validation.jsonl")}),
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
def _generate_examples(self, filepath):
|
| 63 |
+
"""This function returns the examples in the raw (text) form."""
|
| 64 |
+
logger.info("generating examples from = %s", filepath)
|
| 65 |
+
with open(filepath, encoding="utf-8") as f:
|
| 66 |
+
for key, row in enumerate(f):
|
| 67 |
+
data = json.loads(row)
|
| 68 |
+
yield key, {
|
| 69 |
+
"text": data["text"],
|
| 70 |
+
"labels": data["labels"],
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def read_file(file_name_label_tuple):
|
| 75 |
+
fname, label = file_name_label_tuple
|
| 76 |
+
tweets, labels = [], []
|
| 77 |
+
with open(fname, 'r', encoding='utf-8') as f:
|
| 78 |
+
tweets = f.readlines()
|
| 79 |
+
|
| 80 |
+
labels = [label] * (len(tweets))
|
| 81 |
+
|
| 82 |
+
return(tweets, labels)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def load_train_data():
|
| 86 |
+
|
| 87 |
+
if USE_FULL_DATASET == True:
|
| 88 |
+
X_train_neg_path = PROJECT_PATH + "train_neg_full.txt"
|
| 89 |
+
X_train_pos_path = PROJECT_PATH + "train_pos_full.txt"
|
| 90 |
+
|
| 91 |
+
else:
|
| 92 |
+
X_train_neg_path = PROJECT_PATH + "train_neg.txt"
|
| 93 |
+
X_train_pos_path = PROJECT_PATH + "train_pos.txt"
|
| 94 |
+
|
| 95 |
+
tweets, labels = read_file((X_train_neg_path, 0))
|
| 96 |
+
tweets = list(set(tweets))
|
| 97 |
+
labels = labels[:len(tweets)]
|
| 98 |
+
print("There are ", len(tweets), " negative tweets after removing the duplicates.")
|
| 99 |
+
|
| 100 |
+
tweets_2, labels_2 = read_file((X_train_pos_path, 1))
|
| 101 |
+
tweets_2 = list(set(tweets_2))
|
| 102 |
+
labels_2 = labels_2[:len(tweets_2)]
|
| 103 |
+
print("There are ", len(tweets_2), " positive tweets after removing the duplicates.")
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
tweets += tweets_2
|
| 107 |
+
tweets_2 = []
|
| 108 |
+
del(tweets_2)
|
| 109 |
+
labels += labels_2
|
| 110 |
+
labels_2 = []
|
| 111 |
+
del(labels_2)
|
| 112 |
+
print(f"Loaded {len(tweets)} tweets!")
|
| 113 |
+
|
| 114 |
+
tweets, labels = np.array(tweets), np.array(labels)
|
| 115 |
+
print(tweets)
|
| 116 |
+
|
| 117 |
+
# To shuffle the data before cerating the .txt file dataset
|
| 118 |
+
nb_of_samples = len(tweets)
|
| 119 |
+
shuffled_indices = np.random.permutation(nb_of_samples)
|
| 120 |
+
tweets = tweets[shuffled_indices]
|
| 121 |
+
labels = labels[shuffled_indices]
|
| 122 |
+
|
| 123 |
+
print("Number of indices for training: ", len(shuffled_indices))
|
| 124 |
+
|
| 125 |
+
return tweets, labels
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def create_data_file(tweets, labels):
|
| 129 |
+
|
| 130 |
+
with open("HF_data.txt", "wb") as f:
|
| 131 |
+
for i in range(len(tweets)):
|
| 132 |
+
# print(tweets[i])
|
| 133 |
+
f.write(f"{labels[i]} \t {tweets[i]}".encode('utf-8'))
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def main():
|
| 139 |
+
tweets, labels = load_train_data()
|
| 140 |
+
create_data_file(tweets, labels)
|
| 141 |
+
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
main()
|