-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.py
164 lines (151 loc) · 6.92 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import logging
import pandas as pd
import tqdm
from overrides import overrides
from allennlp.data import dataset
from allennlp.data.tokenizers import Tokenizer
from allennlp.data.tokenizers import WordTokenizer
from allennlp.common import Params
from allennlp.common.checks import ConfigurationError
from allennlp.common.file_utils import cached_path
from allennlp.data.dataset_readers.dataset_reader import DatasetReader
from allennlp.data.fields import Field
from allennlp.data.fields import LabelField
from allennlp.data.fields import TextField
from allennlp.data.instance import Instance
from allennlp.data.token_indexers import SingleIdTokenIndexer
from allennlp.data.token_indexers import TokenIndexer
from allennlp.data.tokenizers import Token
from nltk.tree import Tree
from typing import Dict
from typing import List
logger = logging.getLogger(__name__) # pylint: disable=invalid-name
@DatasetReader.register("SSTReader")
class SSTReader(DatasetReader):
"""
Reads tokens and their sentiment labels from the Stanford Sentiment Treebank.
Parameters
----------
token_indexers : ``Dict[str, TokenIndexer]``, optional (default=``{"tokens": SingleIdTokenIndexer()}``)
We use this to define the input representation for the text. See :class:`TokenIndexer`.
Note that the `output` tags will always correspond to single token IDs based on how they
are pre-tokenised in the data file.
use_subtrees : ``bool``, optional, (default = ``False``)
Whether or not to use sentiment-tagged subtrees.
granularity : ``str``, optional (default = ``"5-class"``)
One of ``"5-class"``, ``"3-class"``, or ``"2-class"``, indicating the number
of sentiment labels to use.
lazy : ``bool``, optional, (default = ``False``)
Whether or not instances can be consumed lazily.
"""
def __init__(self,
token_indexers: Dict[str, TokenIndexer] = None,
use_subtrees: bool = False,
granularity: str = "5-class",
lazy: bool = False) -> None:
super().__init__(lazy=lazy)
self._token_indexers = token_indexers or {'tokens': SingleIdTokenIndexer(token_min_padding_length=5)}
self._use_subtrees = use_subtrees
allowed_granularities = ["5-class", "3-class", "2-class"]
if granularity not in allowed_granularities:
raise ConfigurationError("granularity is {}, but expected one of: {}".format(
granularity, allowed_granularities))
self._granularity = granularity
@overrides
def _read(self, file_path):
with open(cached_path(file_path), "r") as data_file:
logger.info("Reading instances from lines in file at: %s", file_path)
for line in data_file.readlines():
line = line.strip("\n")
if not line:
continue
parsed_line = Tree.fromstring(line)
if self._use_subtrees:
for subtree in parsed_line.subtrees():
instance = self.text_to_instance(subtree.leaves(), subtree.label())
if instance is None:
continue
yield instance
else:
instance = self.text_to_instance(parsed_line.leaves(), parsed_line.label())
if instance is None:
continue
yield instance
@overrides
def text_to_instance(self, tokens: List[str], sentiment: str = None) -> Instance: # type: ignore
"""
We take `pre-tokenized` input here, because we don't have a tokenizer in this class.
Parameters
----------
tokens : ``List[str]``, required.
The tokens in a given sentence.
sentiment ``str``, optional, (default = None).
The sentiment for this sentence.
Returns
-------
An ``Instance`` containing the following fields:
tokens : ``TextField``
The tokens in the sentence or phrase.
label : ``LabelField``
The sentiment label of the sentence or phrase.
"""
# pylint: disable=arguments-differ
text_field = TextField([Token(x) for x in tokens], token_indexers=self._token_indexers)
fields: Dict[str, Field] = {"tokens": text_field}
if sentiment is not None:
# Convert to 3-class.
if self._granularity == "3-class":
if int(sentiment) < 2:
sentiment = "0"
elif int(sentiment) == 2:
sentiment = "1"
else:
sentiment = "2"
elif self._granularity == "2-class":
if int(sentiment) < 2:
sentiment = "0"
elif int(sentiment) == 2:
return None
else:
sentiment = "1"
fields['adv'] = LabelField(0, skip_indexing=True)
fields['label'] = LabelField(int(sentiment), skip_indexing=True)
return Instance(fields)
@classmethod
def from_params(cls, params: Params) -> 'StanfordSentimentTreeBankTokensDatasetReader':
token_indexers = TokenIndexer.dict_from_params(params.pop('token_indexers', {}))
use_subtrees = params.pop('use_subtrees', False)
granularity = params.pop_choice('granularity', ["5-class", "3-class", "2-class"], True)
lazy = params.pop('lazy', False)
params.assert_empty(cls.__name__)
return StanfordSentimentTreeBankTokensDatasetReader(
token_indexers=token_indexers, use_subtrees=use_subtrees,
granularity=granularity, lazy=lazy)
@DatasetReader.register('CSVReader')
class CSVReader(DatasetReader):
def __init__(self, tokenizer: Tokenizer = WordTokenizer(), lazy: bool = False, max_length: int = 500) -> None:
super().__init__(lazy)
self._tokenizer = tokenizer
self._token_indexer = {"tokens": SingleIdTokenIndexer(lowercase_tokens=True, token_min_padding_length=5)}
self.max_length = max_length
@overrides
def _read(self, file: str):
df = pd.read_csv(file)
for _, row in df.iterrows():
text = row['text']
if 'label' in df.columns:
target = int(row['label'])
yield self.text_to_instance(text, target)
else:
yield self.text_to_instance(text)
@overrides
def text_to_instance(self, text: str, target: float = None) -> Instance:
text = self._tokenizer.tokenize(text)
text = text[:self.max_length]
fields = {}
# fields['idx'] = MetadataField({'idx': idx})
fields['adv'] = LabelField(0, skip_indexing=True)
fields['tokens'] = TextField(text, self._token_indexer)
if target is not None:
fields['label'] = LabelField(target, skip_indexing=True)
return Instance(fields)