asahi417 commited on
Commit
f113fa7
1 Parent(s): 606156d

Create tweet_sentiment_multilingual.py

Browse files
Files changed (1) hide show
  1. tweet_sentiment_multilingual.py +79 -0
tweet_sentiment_multilingual.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import datasets
3
+
4
+
5
+ logger = datasets.logging.get_logger(__name__)
6
+ _VERSION = "0.0.0"
7
+ _NAME = "tweet_sentiment_multilingual"
8
+ _DESCRIPTION = "Multilingual sentiment analysis dataset on Twitter."
9
+ _CITATION = """
10
+ @inproceedings{barbieri-etal-2022-xlm,
11
+ title = "{XLM}-{T}: Multilingual Language Models in {T}witter for Sentiment Analysis and Beyond",
12
+ author = "Barbieri, Francesco and
13
+ Espinosa Anke, Luis and
14
+ Camacho-Collados, Jose",
15
+ booktitle = "Proceedings of the Thirteenth Language Resources and Evaluation Conference",
16
+ month = jun,
17
+ year = "2022",
18
+ address = "Marseille, France",
19
+ publisher = "European Language Resources Association",
20
+ url = "https://aclanthology.org/2022.lrec-1.27",
21
+ pages = "258--266",
22
+ abstract = "Language models are ubiquitous in current NLP, and their multilingual capacity has recently attracted considerable attention. However, current analyses have almost exclusively focused on (multilingual variants of) standard benchmarks, and have relied on clean pre-training and task-specific corpora as multilingual signals. In this paper, we introduce XLM-T, a model to train and evaluate multilingual language models in Twitter. In this paper we provide: (1) a new strong multilingual baseline consisting of an XLM-R (Conneau et al. 2020) model pre-trained on millions of tweets in over thirty languages, alongside starter code to subsequently fine-tune on a target task; and (2) a set of unified sentiment analysis Twitter datasets in eight different languages and a XLM-T model trained on this dataset.",
23
+ }
24
+ """
25
+ _HOMEPAGE = "https://github.com/cardiffnlp/xlm-t"
26
+ _BASE_URL = "https://huggingface.co/datasets/cardiffnlp/tweet_sentiment_multilingual/resolve/main/data"
27
+ _LANGUAGE = ["arabic", "english", "french", "german", "hindi", "italian", "portuguese", "spanish"]
28
+ _URLS = {l: {
29
+ k: f'{_BASE_URL}/{l}/{k}.jsonl' for k in [str(datasets.Split.TEST), str(datasets.Split.TRAIN), str(datasets.Split.VALIDATION)]
30
+ } for l in _LANGUAGE}
31
+
32
+
33
+ class TweetSentimentMultilingualConfig(datasets.BuilderConfig):
34
+ """BuilderConfig"""
35
+
36
+ def __init__(self, **kwargs):
37
+ """BuilderConfig
38
+ Args:
39
+ **kwargs: keyword arguments forwarded to super.
40
+ """
41
+ super(TweetSentimentMultilingualConfig, self).__init__(**kwargs)
42
+
43
+
44
+ class TweetSentimentMultilingual(datasets.GeneratorBasedBuilder):
45
+
46
+ BUILDER_CONFIGS = [
47
+ TweetSentimentMultilingualConfig(name=_NAME, type=l, version=datasets.Version(_VERSION), description=_DESCRIPTION) for l in _LANGUAGE
48
+ ]
49
+ BUILDER_CONFIGS += [TweetSentimentMultilingualConfig(name=_NAME, type='all', version=datasets.Version(_VERSION), description=_DESCRIPTION)]
50
+
51
+ def _info(self):
52
+ names = ["negative", "neutral", "positive"]
53
+ return datasets.DatasetInfo(
54
+ description=_DESCRIPTION,
55
+ features=datasets.Features(
56
+ {"text": datasets.Value("string"), "label": datasets.features.ClassLabel(names=names)}
57
+ ),
58
+ supervised_keys=None,
59
+ homepage=_HOMEPAGE,
60
+ citation=_CITATION
61
+ )
62
+
63
+ def _split_generators(self, dl_manager):
64
+ downloaded_file = dl_manager.download_and_extract(_URLS[self.config.type])
65
+ return [datasets.SplitGenerator(name=i, gen_kwargs={"filepath": downloaded_file[str(i)]})
66
+ for i in [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]]
67
+
68
+ def _generate_examples(self, filepath):
69
+ """This function returns the examples in the raw (text) form."""
70
+ _key = 0
71
+ logger.info("generating examples from = %s", filepath)
72
+ with open(filepath, encoding="utf-8") as f:
73
+ _list = f.read().split('\n')
74
+ if _list[-1] == '':
75
+ _list = _list[:-1]
76
+ for i in _list:
77
+ data = json.loads(i)
78
+ yield _key, data
79
+ _key += 1