Dannong Wang
commited on
Commit
·
d642557
1
Parent(s):
da76932
add dataset
Browse files- README.md +29 -0
- formula_calculation_test.csv +0 -0
- formula_calculation_train.csv +0 -0
- formula_formatted_with_tags_test.csv +0 -0
- formula_formatted_with_tags_train.csv +0 -0
- generate_xbrl_extract_hf_split.py +119 -0
- value_test.csv +0 -0
- value_train.csv +0 -0
- xbrl_tags_test.csv +0 -0
- xbrl_tags_train.csv +0 -0
README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
configs:
|
| 3 |
+
- config_name: tags
|
| 4 |
+
data_files:
|
| 5 |
+
- split: train
|
| 6 |
+
path: "xbrl_tags_train.csv"
|
| 7 |
+
- split: test
|
| 8 |
+
path: "xbrl_tags_test.csv"
|
| 9 |
+
- config_name: value
|
| 10 |
+
data_files:
|
| 11 |
+
- split: train
|
| 12 |
+
path: "value_train.csv"
|
| 13 |
+
- split: test
|
| 14 |
+
path: "value_test.csv"
|
| 15 |
+
- config_name: formula
|
| 16 |
+
data_files:
|
| 17 |
+
- split: train
|
| 18 |
+
path: "formula_formatted_with_tags_train.csv"
|
| 19 |
+
- split: test
|
| 20 |
+
path: "formula_formatted_with_tags_test.csv"
|
| 21 |
+
- config_name: formula_calculations
|
| 22 |
+
data_files:
|
| 23 |
+
- split: train
|
| 24 |
+
path: "formula_calculation_train.csv"
|
| 25 |
+
- split: test
|
| 26 |
+
path: "formula_calculation_test.csv"
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
# XBRL Extraction Dataset
|
formula_calculation_test.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
formula_calculation_train.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
formula_formatted_with_tags_test.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
formula_formatted_with_tags_train.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
generate_xbrl_extract_hf_split.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import xml.etree.ElementTree as ET
|
| 2 |
+
import re
|
| 3 |
+
import json
|
| 4 |
+
from typing import List, Dict
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
import random
|
| 7 |
+
import os.path
|
| 8 |
+
from huggingface_hub import HfApi
|
| 9 |
+
import csv
|
| 10 |
+
|
| 11 |
+
random.seed(42)
|
| 12 |
+
import subprocess
|
| 13 |
+
|
| 14 |
+
example_qa_dict = {
|
| 15 |
+
"xbrl_tags": {
|
| 16 |
+
"q": "What is the US GAAP XBRL tag for Cash and Cash Equivalents as reported by Example Company Inc for the Fiscal Year ending in FY 2022",
|
| 17 |
+
"a": "us-gaap:AnExampleTagName"
|
| 18 |
+
},
|
| 19 |
+
"value": {
|
| 20 |
+
"q": "What is the value of Exapmle company's income for the Fiscal year ending in FY 2020?",
|
| 21 |
+
"a": "80000000"
|
| 22 |
+
},
|
| 23 |
+
"formula_calculation": {
|
| 24 |
+
"q": "Can you provide the formula for Operating Profit Margin from Example Corp for the Fiscal Year ending in FY 2022?",
|
| 25 |
+
"a": "(50000000 / 3590000000) * 100"
|
| 26 |
+
},
|
| 27 |
+
"formula_formatted_with_tags": {
|
| 28 |
+
"q": "What is the formula for the Gross Profit Margin of Example Inc, formatted with the relevant US GAAP XBRL tags",
|
| 29 |
+
"a": "us-gaap:ExampleTag / us-gaap:AnotherExampleTag) * 100"
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def get_xbrl_dataset(data: List[Dict], cat):
|
| 35 |
+
"""
|
| 36 |
+
Saves entries with matching category1 or category2 in the format for fine-tuning.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
data (List[Dict]): The input JSON data.
|
| 40 |
+
category (str): The category name to match.
|
| 41 |
+
"""
|
| 42 |
+
results = []
|
| 43 |
+
for entry in data:
|
| 44 |
+
question = entry["query"]
|
| 45 |
+
question = re.sub(r"\(.*?\)", "", question)
|
| 46 |
+
context_ids = entry["contextID"]
|
| 47 |
+
|
| 48 |
+
# if not os.path.isfile('train/DowJones30/' + doc_path):
|
| 49 |
+
# print(f"missing file {doc_path}")
|
| 50 |
+
# continue
|
| 51 |
+
|
| 52 |
+
example_qa = f"Example question: {example_qa_dict[cat]['q']}\nExample answer: {example_qa_dict[cat]['a']}"
|
| 53 |
+
output = entry["raw_answer"]
|
| 54 |
+
|
| 55 |
+
if cat == 'formula_calculation':
|
| 56 |
+
question += " Answer with a formula substituted with values. "
|
| 57 |
+
output = entry["value_formula_answer"]
|
| 58 |
+
|
| 59 |
+
output = str(output)
|
| 60 |
+
instruction = (f"You are a knowledgeable XBRL assistant. Your task is to analyze the XBRL context and provide an"
|
| 61 |
+
f" accurate and very concise answer to the question, The example question can help you to learn the "
|
| 62 |
+
f"answer format. DO NOT output xml, code, explanation or create new question. \n{example_qa}\n")
|
| 63 |
+
|
| 64 |
+
input = f"Question: {question}\nAnswer:"
|
| 65 |
+
year = int(entry["{fiscal year/quarter}"].replace("FY ", ""))
|
| 66 |
+
|
| 67 |
+
# context_xml = add_xml(instruction + input, doc_path}, context_ids[0])
|
| 68 |
+
# if len(context_xml) > 24000:
|
| 69 |
+
# continue
|
| 70 |
+
|
| 71 |
+
# print(entry["answer"])
|
| 72 |
+
# entry["doc_path"], entry["answer"], entry["contextID"][0]
|
| 73 |
+
results.append({
|
| 74 |
+
"instruction": instruction,
|
| 75 |
+
"input": input,
|
| 76 |
+
"output": output,
|
| 77 |
+
"year": year,
|
| 78 |
+
"company": entry["ticker"],
|
| 79 |
+
"doc_path": entry['doc_path'],
|
| 80 |
+
"context_id": context_ids[0],
|
| 81 |
+
})
|
| 82 |
+
|
| 83 |
+
print("final length", len(results))
|
| 84 |
+
return results
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def gen_xbrl(cat):
|
| 88 |
+
with open("xbrl_bench_34020.json", "r") as f:
|
| 89 |
+
data = json.load(f)
|
| 90 |
+
filtered_data = [entry for entry in data if entry['category1'] == cat or entry['category2'] == cat]
|
| 91 |
+
|
| 92 |
+
all_doc_path = list(set([entry['doc_path'] for entry in filtered_data]))
|
| 93 |
+
print(f"Total data size for {cat}: {len(filtered_data)}, total number of filings {len(all_doc_path)}")
|
| 94 |
+
random.shuffle(filtered_data)
|
| 95 |
+
|
| 96 |
+
dataset = get_xbrl_dataset(filtered_data, cat)
|
| 97 |
+
|
| 98 |
+
test_data = [x for x in dataset if x['year'] == 2023]
|
| 99 |
+
train_data = [x for x in dataset if x['year'] != 2023]
|
| 100 |
+
|
| 101 |
+
print(f"train size: {len(train_data)}, test size: {len(test_data)}\n")
|
| 102 |
+
|
| 103 |
+
with open(f"{cat}_test.csv", "w", newline="") as f:
|
| 104 |
+
w = csv.DictWriter(f, test_data[0].keys(), quoting=csv.QUOTE_ALL)
|
| 105 |
+
w.writeheader()
|
| 106 |
+
w.writerows(test_data)
|
| 107 |
+
with open(f"{cat}_train.csv", "w", newline="") as f:
|
| 108 |
+
w = csv.DictWriter(f, train_data[0].keys(), quoting=csv.QUOTE_ALL)
|
| 109 |
+
w.writeheader()
|
| 110 |
+
w.writerows(train_data)
|
| 111 |
+
|
| 112 |
+
return train_data, test_data
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
if __name__ == '__main__':
|
| 116 |
+
tags_train, tags_test = gen_xbrl("xbrl_tags")
|
| 117 |
+
value_train, value_test = gen_xbrl("value")
|
| 118 |
+
formula_train, formula_test = gen_xbrl("formula_formatted_with_tags")
|
| 119 |
+
formula_calc_train, formula_calc_test = gen_xbrl("formula_calculation")
|
value_test.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
value_train.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
xbrl_tags_test.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
xbrl_tags_train.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|