Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
asi commited on
Commit
6fa947a
1 Parent(s): 68f869a

:books: add documentation

Browse files
Files changed (2) hide show
  1. README.md +137 -0
  2. stackexchange_math_jsonl.py +158 -0
README.md ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ annotations_creators:
3
+ - found
4
+ language_creators:
5
+ - found
6
+ languages:
7
+ - en
8
+ licenses:
9
+ - cc-by-nc-sa-4.0
10
+ multilinguality:
11
+ - multilingual
12
+ pretty_name: stackexchange
13
+ size_categories:
14
+ - unknown
15
+ source_datasets:
16
+ - original
17
+ task_categories:
18
+ - question-answering
19
+ task_ids:
20
+ - closed-domain-qa
21
+ ---
22
+
23
+ # Dataset Card Creation Guide
24
+
25
+ ## Table of Contents
26
+ - [Dataset Card Creation Guide](#dataset-card-creation-guide)
27
+ - [Table of Contents](#table-of-contents)
28
+ - [Dataset Description](#dataset-description)
29
+ - [Dataset Summary](#dataset-summary)
30
+ - [Languages](#languages)
31
+ - [Dataset Structure](#dataset-structure)
32
+ - [Data Instances](#data-instances)
33
+ - [Data Fields](#data-fields)
34
+ - [Data Splits](#data-splits)
35
+ - [Dataset Creation](#dataset-creation)
36
+ - [Curation Rationale](#curation-rationale)
37
+ - [Source Data](#source-data)
38
+ - [Initial Data Collection and Normalization](#initial-data-collection-and-normalization)
39
+ - [Who are the source language producers?](#who-are-the-source-language-producers)s
40
+ - [Additional Information](#additional-information)
41
+ - [Licensing Information](#licensing-information)
42
+ - [Citation Information](#citation-information)
43
+ - [Contributions](#contributions)
44
+
45
+ ## Dataset Description
46
+
47
+ - **Homepage:** [stackexchange](https://archive.org/details/stackexchange)
48
+ - **Repository:** [flax-sentence-embeddings](https://github.com/nreimers/flax-sentence-embeddings)
49
+
50
+ ### Dataset Summary
51
+
52
+ We automatically extracted question and answer (Q&A) pairs from [Stack Exchange](https://stackexchange.com/) network. Stack Exchange gather many Q&A communities across 50 online plateform, including the well known Stack Overflow and other technical sites. 100 millon developpers consult Stack Exchange every month. The dataset is a parallel corpus with each question mapped to the top rated answer. The dataset is split given communities which cover a variety of domains from 3d printing, economics, raspberry pi or emacs. An exhaustive list of all communities is available [here](https://stackexchange.com/sites).
53
+
54
+ ### Languages
55
+
56
+ Stack Exchange mainly consist of english language (en).
57
+
58
+ ## Dataset Structure
59
+
60
+ ### Data Instances
61
+
62
+ Each data samples is presented as follow:
63
+
64
+ ```
65
+ {'title_body': 'How to determine if 3 points on a 3-D graph are collinear? Let the points $A, B$ and $C$ be $(x_1, y_1, z_1), (x_2, y_2, z_2)$ and $(x_3, y_3, z_3)$ respectively. How do I prove that the 3 points are collinear? What is the formula?',
66
+ 'upvoted_answer': 'From $A(x_1,y_1,z_1),B(x_2,y_2,z_2),C(x_3,y_3,z_3)$ we can get their position vectors.\n\n$\\vec{AB}=(x_2-x_1,y_2-y_1,z_2-z_1)$ and $\\vec{AC}=(x_3-x_1,y_3-y_1,z_3-z_1)$.\n\nThen $||\\vec{AB}\\times\\vec{AC}||=0\\implies A,B,C$ collinear.',
67
+ 'downvoted_answer': 'If the distance between |AB|+|BC|=|AC| then A,B,C are collinear.'}
68
+ ```
69
+
70
+ This particular exampe corresponds to the [following page](https://math.stackexchange.com/questions/947555/how-to-determine-if-3-points-on-a-3-d-graph-are-collinear)
71
+
72
+ ### Data Fields
73
+
74
+ The fields present in the dataset contain the following informations:
75
+
76
+ - `title_body`: This is the concatenation of the title and body from the question
77
+ - `upvoted_answer`: This is the body from the most upvoted answer
78
+ - `downvoted_answer`: This is the body from most downvoted answer
79
+ - `title`: This is the title from the question
80
+
81
+ ### Data Splits
82
+
83
+ We provide three splits for this dataset, which only differs by the structure of the fieds which are retrieved:
84
+
85
+ - `titlebody_upvoted_downvoted_answer`: Includes title and body from the question as well as most upvoted and downvoted answer.
86
+ - `title_answer`: Includes title from the question as well as most upvoted answer.
87
+ - `titlebody_answer`: Includes title and body from the question as well as most upvoted answer.
88
+
89
+
90
+ | | Number of pairs |
91
+ | ----- | ------ |
92
+ | `titlebody_upvoted_downvoted_answer` | 17,083 |
93
+ | `title_answer` | 1,100,953 |
94
+ | `titlebody_answer` | 1,100,953 |
95
+
96
+ ## Dataset Creation
97
+
98
+ ### Curation Rationale
99
+
100
+ We primary designed this dataset for sentence embeddings training. Indeed sentence embeddings may be trained using a contrastive learning setup for which the model is trained to associate each sentence with its corresponding pair out of multiple proposition. Such models require many examples to be efficient and thus the dataset creation may be tedious. Community networks such as Stack Exchange allow us to build many examples semi-automatically.
101
+
102
+ ### Source Data
103
+
104
+ The source data are dumps from [Stack Exchange](https://archive.org/details/stackexchange)
105
+
106
+ #### Initial Data Collection and Normalization
107
+
108
+ We collected the data from the math community.
109
+
110
+ We filtered out questions which title or body length is bellow 20 characters and questions for which body length is above 4096 characters.
111
+ When extracting most upvoted answer, we filtered to pairs for which their is at least 100 votes gap between most upvoted and downvoted answers.
112
+
113
+ #### Who are the source language producers?
114
+
115
+ Questions and answers are written by the community developpers of Stack Exchange.
116
+
117
+ ## Additional Information
118
+
119
+ ### Licensing Information
120
+
121
+ Please see the license information at: https://archive.org/details/stackexchange
122
+
123
+ ### Citation Information
124
+
125
+ ```
126
+ @misc{StackExchangeDataset,
127
+ author = {Flax Sentence Embeddings Team},
128
+ title = {Stack Exchange question pairs},
129
+ year = {2021},
130
+ howpublished = {https://huggingface.co/datasets/flax-sentence-embeddings/},
131
+ }
132
+ ```
133
+
134
+
135
+ ### Contributions
136
+
137
+ Thanks to the Flax Sentence Embeddings team for adding this dataset.
stackexchange_math_jsonl.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the Flax Sentence Embeddings team.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """The dataset is a collection of Question and Answer automatically extracted from Stack Exchange community network."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+
25
+ _CITATION = """\
26
+ @misc{StackExchangeDataset,
27
+ author = {Flax Sentence Embeddings Team},
28
+ title = {Stack Exchange question pairs},
29
+ year = {2021},
30
+ howpublished = {https://huggingface.co/datasets/flax-sentence-embeddings/},
31
+ }
32
+ """
33
+
34
+ # TODO: Add description of the dataset here
35
+ # You can copy an official description
36
+ _DESCRIPTION = """\
37
+ This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
38
+ """
39
+
40
+ # TODO: Add a link to an official homepage for the dataset here
41
+ _HOMEPAGE = "https://huggingface.co/datasets/flax-sentence-embeddings/"
42
+
43
+ # TODO: Add the licence for the dataset here if you can find it
44
+ _LICENSE = "https://archive.org/details/stackexchange"
45
+
46
+ # TODO: Add link to the official dataset URLs here
47
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
48
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
49
+ _URLs = {
50
+ 'titlebody_upvoted_downvoted_answer': "titlebody_upvoted_downvoted_answer.jsonl.gz",
51
+ 'title_answer': "title_answer.jsonl.gz",
52
+ 'titlebody_answer': "titlebody_answer.jsonl.gz",
53
+ }
54
+
55
+
56
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
57
+ class StackExchangeMatch(datasets.GeneratorBasedBuilder):
58
+ """The dataset is a collection of Question and Answer automatically extracted from match Stack Exchange community."""
59
+
60
+ VERSION = datasets.Version("1.1.0")
61
+
62
+ BUILDER_CONFIGS = [
63
+ datasets.BuilderConfig(name="titlebody_upvoted_downvoted_answer", version=VERSION,
64
+ description="Includes title and body from the question as well as most upvoted and downvoted answer."),
65
+ datasets.BuilderConfig(name="title_answer", version=VERSION,
66
+ description="Includes title from the question as well as most upvoted answer."),
67
+ datasets.BuilderConfig(name="titlebody_answer", version=VERSION,
68
+ description="Includes title and body from the question as well as most upvoted answer.")
69
+ ]
70
+
71
+ DEFAULT_CONFIG_NAME = "title_answer" # It's not mandatory to have a default configuration. Just use one if it make sense.
72
+
73
+ def _info(self):
74
+ if self.config.name == "titlebody_upvoted_downvoted_answer": # This is the name of the configuration selected in BUILDER_CONFIGS above
75
+ features = datasets.Features(
76
+ {
77
+ "title_body": datasets.Value("string"),
78
+ "upvoted_answer": datasets.Value("string"),
79
+ "downvoted_answer": datasets.Value("string")
80
+ }
81
+ )
82
+ elif self.config.name == "titlebody_answer": # This is the name of the configuration selected in BUILDER_CONFIGS above
83
+ features = datasets.Features(
84
+ {
85
+ "title_body": datasets.Value("string"),
86
+ "upvoted_answer": datasets.Value("string"),
87
+ }
88
+ )
89
+ else: # This is an example to show how to have different features for "first_domain" and "second_domain"
90
+ features = datasets.Features(
91
+ {
92
+ "title": datasets.Value("string"),
93
+ "upvoted_answer": datasets.Value("string"),
94
+ }
95
+ )
96
+ return datasets.DatasetInfo(
97
+ # This is the description that will appear on the datasets page.
98
+ description=_DESCRIPTION,
99
+ # This defines the different columns of the dataset and their types
100
+ features=features, # Here we define them above because they are different between the two configurations
101
+ # If there's a common (input, target) tuple from the features,
102
+ # specify them here. They'll be used if as_supervised=True in
103
+ # builder.as_dataset.
104
+ supervised_keys=None,
105
+ # Homepage of the dataset for documentation
106
+ homepage=_HOMEPAGE,
107
+ # License for the dataset if available
108
+ license=_LICENSE,
109
+ # Citation for the dataset
110
+ citation=_CITATION,
111
+ )
112
+
113
+ def _split_generators(self, dl_manager):
114
+ """Returns SplitGenerators."""
115
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
116
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
117
+
118
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
119
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
120
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
121
+ my_urls = _URLs[self.config.name]
122
+ data_file = dl_manager.download_and_extract(my_urls)
123
+ return [
124
+ datasets.SplitGenerator(
125
+ name=datasets.Split.TRAIN,
126
+ # These kwargs will be passed to _generate_examples
127
+ gen_kwargs={
128
+ "filepath": data_file,
129
+ },
130
+ )
131
+ ]
132
+
133
+ def _generate_examples(
134
+ self, filepath # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
135
+ ):
136
+ """ Yields examples as (key, example) tuples. """
137
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
138
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
139
+
140
+ with open(filepath, encoding="utf-8") as f:
141
+ for id_, row in enumerate(f):
142
+ data = json.loads(row)
143
+ if self.config.name == "titlebody_upvoted_downvoted_answer":
144
+ yield id_, {
145
+ "title_body": data[0],
146
+ "upvoted_answer": data[1],
147
+ "downvoted_answer": data[2],
148
+ }
149
+ elif self.config.name == "titlebody_answer":
150
+ yield id_, {
151
+ "title_body": data[0],
152
+ "upvoted_answer": data[1],
153
+ }
154
+ else:
155
+ yield id_, {
156
+ "title": data[0],
157
+ "upvoted_answer": data[1],
158
+ }