File size: 10,770 Bytes
33f74eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c7c54
33f74eb
63c7c54
 
 
 
 
 
 
33f74eb
63c7c54
33f74eb
 
 
 
 
63c7c54
33f74eb
 
 
 
 
 
 
63c7c54
 
 
33f74eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c7c54
33f74eb
 
63c7c54
 
33f74eb
 
 
 
 
 
63c7c54
33f74eb
 
63c7c54
33f74eb
 
 
 
 
 
 
 
 
 
63c7c54
 
 
 
 
33f74eb
63c7c54
33f74eb
 
 
 
 
63c7c54
33f74eb
 
 
 
 
 
 
63c7c54
33f74eb
 
 
 
 
 
 
63c7c54
33f74eb
 
 
 
 
 
 
63c7c54
 
 
 
 
 
33f74eb
63c7c54
33f74eb
63c7c54
33f74eb
63c7c54
 
 
 
 
 
 
 
 
 
 
 
 
33f74eb
 
 
63c7c54
33f74eb
 
63c7c54
 
 
33f74eb
 
 
 
 
63c7c54
33f74eb
 
63c7c54
 
 
33f74eb
 
 
 
 
 
 
 
 
 
 
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Copyright 2023 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json

import datasets

_DESCRIPTION = """\
The dataset contains the annual report of US public firms filing with the SEC EDGAR system.
Each annual report (10K filing) is broken into 20 sections. Each section is split into individual sentences.
Sentiment labels are provided on a per filing basis from the market reaction around the filing data.
Additional metadata for each filing is included in the dataset.
"""

_LICENSE = "apache-2.0"

_NOS_SHARDS = 10

_URLS = {item+'_'+config: ["data/"+config+"/"+item+"/shard_"+str(shard)+".jsonl" for shard in range(_NOS_SHARDS)] for item in ['test', 'train', 'validate'] for config in ["large", "small"]}

_REPORT_KEYS = ['section_1', 'section_1A', 'section_1B', 
                 'section_2', 'section_3', 'section_4', 
                 'section_5', 'section_6', 'section_7', 
                 'section_7A', 'section_8', 'section_9', 
                 'section_9A', 'section_9B', 'section_10', 
                 'section_11', 'section_12', 'section_13', 
                 'section_14', 'section_15']

_LITE_FEATURES = ["cik", "sentence", "section", "labels", "filingDate", "docID", "sentenceID", "sentenceCount"]

_ALL_FEATURES = {
    "cik": datasets.Value("string"),
    "sentence": datasets.Value("string"),
    "section": datasets.ClassLabel(num_classes=20, 
                                   names=_REPORT_KEYS),
    "labels": {
            "1d": datasets.ClassLabel(num_classes=2, names=["positive", "negative"]),
            "5d": datasets.ClassLabel(num_classes=2, names=["positive", "negative"]),
            "30d": datasets.ClassLabel(num_classes=2, names=["positive", "negative"]),
        },
    "filingDate": datasets.Value("string"),
    "name": datasets.Value("string"), 
    "docID": datasets.Value("string"), 
    "sentenceID": datasets.Value("string"), 
    "sentenceCount": datasets.Value("int64"), 
    "tickers": [datasets.Value("string")],  
    "exchanges": [datasets.Value("string")],
    "entityType": datasets.Value("string"),
    "sic": datasets.Value("string"),
    "stateOfIncorporation": datasets.Value("string"),
    "tickerCount": datasets.Value("int32"),
    "acceptanceDateTime": datasets.Value("string"),
    "form": datasets.Value("string"),
    "reportDate": datasets.Value("string"),  
    "returns": {
            "1d": {
                    "closePriceEndDate": datasets.Value("float32"),
                    "closePriceStartDate": datasets.Value("float32"),
                    "endDate": datasets.Value("string"),
                    "startDate": datasets.Value("string"),
                    "ret": datasets.Value("float32"),
                },
            "5d": {
                    "closePriceEndDate": datasets.Value("float32"),
                    "closePriceStartDate": datasets.Value("float32"),
                    "endDate": datasets.Value("string"),
                    "startDate": datasets.Value("string"),
                    "ret": datasets.Value("float32"),
                },
            "30d": {
                    "closePriceEndDate": datasets.Value("float32"),
                    "closePriceStartDate": datasets.Value("float32"),
                    "endDate": datasets.Value("string"),
                    "startDate": datasets.Value("string"),
                    "ret": datasets.Value("float32"),
                }
        },
}


class FinancialReportsSec(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.1.1")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="large_lite", version=VERSION, description="This returns the dataset with only the critical data needed for analysis."),
        datasets.BuilderConfig(name="large_full", version=VERSION, description="This returns the dataset with all metadata included."),
        datasets.BuilderConfig(name="small_lite", version=VERSION, description="This returns a smaller version of the dataset with only the critical data needed for analysis."),
        datasets.BuilderConfig(name="small_full", version=VERSION, description="This returns a smaller version of the dataset with all metadata included."),
    ]

    def _info(self):        
        
        lite_features = datasets.Features({k: v for k, v in _ALL_FEATURES.items() if k in _LITE_FEATURES})
        full_features = datasets.Features(_ALL_FEATURES)
        
        features = full_features if self.config.name.endswith('full') else lite_features
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,
            # License for the dataset if available
            license=_LICENSE,
        )

    def _split_generators(self, dl_manager):
        if self.config.name.split('_')[0] == 'large':
            urls = {k: v for k, v in _URLS.items() if k.endswith('large')}
        else:
            urls = {k: v for k, v in _URLS.items() if k.endswith('small')}
            
        data_dir = dl_manager.download_and_extract(urls)
        
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepaths": data_dir["train_large"] if self.config.name.startswith('large') else data_dir["train_small"],
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepaths": data_dir["validate_large"] if self.config.name.startswith('large') else data_dir["validate_small"],
                    "split": "validate",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepaths": data_dir["test_large"] if self.config.name.startswith('large') else data_dir["test_small"],
                    "split": "test"
                },
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepaths, split):
        # Store the sentence count for the current config. Not unique across configs.
        sentenceCount = 0
        
        # filepath is expected to be already parsed for config and split.
        for filepath in filepaths:           
            with open(filepath, encoding="utf-8") as f:                
                for firmIdx, row in enumerate(f):
                    # Each row is a single firm observation.
                    data = json.loads(row)                    
                    # Iterate the filings.
                    for filing in data["filings"]:
                        # Iterate over _REPORT_KEYS to ensure that the order of sections
                        # is consistent.
                        for section_id in _REPORT_KEYS:
                            # For the small versions of the dataset, some of the
                            # sections will be cut-off and hence None.
                            if filing["report"][section_id] is None:
                                return None
                            # Finally, iterate the sentences in the section of the filing.
                            for idx, sentence in enumerate(filing["report"][section_id]):  
                                sentenceCount += 1
                                key = data["cik"]+'_'+filing["form"]+'_'+filing["reportDate"].split('-')[0]+'_'+section_id+'_'+str(idx)
                                
                                if self.config.name.endswith('lite'):                                    
                                    yield key, {
                                        "cik": data["cik"],
                                        "sentence": sentence,
                                        "section": section_id,
                                        "labels": filing["labels"],
                                        "filingDate": filing["filingDate"],
                                        "docID": data["cik"]+'_'+filing["form"]+'_'+filing["reportDate"].split('-')[0],
                                        "sentenceID": key,
                                        "sentenceCount": sentenceCount,
                                    }
                                else:
                                    yield key, {
                                        "cik": data["cik"],
                                        "sentence": sentence,
                                        "section": section_id,
                                        "labels": filing["labels"],
                                        "filingDate": filing["filingDate"],
                                        "docID": data["cik"]+'_'+filing["form"]+'_'+filing["reportDate"].split('-')[0],
                                        "sentenceID": key,
                                        "sentenceCount": sentenceCount,
                                        "name": data["name"],
                                        "tickers": data["tickers"],
                                        "exchanges": data["exchanges"],
                                        "entityType": data["entityType"],
                                        "sic": data["sic"],
                                        "stateOfIncorporation": data["stateOfIncorporation"],
                                        "tickerCount": data["tickerCount"],
                                        "acceptanceDateTime": filing["acceptanceDateTime"],
                                        "form": filing["form"],
                                        "reportDate": filing["reportDate"],
                                        "returns": filing["returns"],
                                    }