Gaëtan Caillaut
commited on
Commit
•
a57dc4a
1
Parent(s):
3df3d6c
CiteSeer dataset
Browse files- citeseer.py +192 -0
citeseer.py
ADDED
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
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 |
+
"""TODO: Add a description here."""
|
16 |
+
|
17 |
+
|
18 |
+
from datasets import features
|
19 |
+
import pandas
|
20 |
+
import os
|
21 |
+
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
|
25 |
+
# TODO: Add BibTeX citation
|
26 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
27 |
+
_CITATION = ""
|
28 |
+
|
29 |
+
# TODO: Add description of the dataset here
|
30 |
+
# You can copy an official description
|
31 |
+
_DESCRIPTION = """\
|
32 |
+
The CiteSeer dataset consists of 3312 scientific publications classified into one of six classes. The citation network consists of 4732 links. Each publication in the dataset is described by a 0/1-valued word vector indicating the absence/presence of the corresponding word from the dictionary. The dictionary consists of 3703 unique words. The README file in the dataset provides more details.
|
33 |
+
"""
|
34 |
+
|
35 |
+
# TODO: Add a link to an official homepage for the dataset here
|
36 |
+
_HOMEPAGE = "https://linqs.soe.ucsc.edu/data"
|
37 |
+
|
38 |
+
# TODO: Add the licence for the dataset here if you can find it
|
39 |
+
_LICENSE = ""
|
40 |
+
|
41 |
+
# TODO: Add link to the official dataset URLs here
|
42 |
+
# The HuggingFace dataset library don't host the datasets but only point to the original files
|
43 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
44 |
+
_URLs = {
|
45 |
+
"nodes": "https://linqs-data.soe.ucsc.edu/public/lbc/citeseer.tgz",
|
46 |
+
"edges": "https://linqs-data.soe.ucsc.edu/public/lbc/citeseer.tgz"
|
47 |
+
}
|
48 |
+
|
49 |
+
_CLASS_LABELS = [
|
50 |
+
"Agents",
|
51 |
+
"AI",
|
52 |
+
"DB",
|
53 |
+
"IR",
|
54 |
+
"ML",
|
55 |
+
"HCI"
|
56 |
+
]
|
57 |
+
|
58 |
+
|
59 |
+
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
|
60 |
+
class CiteseerDataset(datasets.GeneratorBasedBuilder):
|
61 |
+
VERSION = datasets.Version("1.0.0")
|
62 |
+
|
63 |
+
# This is an example of a dataset with multiple configurations.
|
64 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
65 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
66 |
+
|
67 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
68 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
69 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
70 |
+
|
71 |
+
# You will be able to load one or the other configurations in the following list with
|
72 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
73 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
74 |
+
BUILDER_CONFIGS = [
|
75 |
+
datasets.BuilderConfig(name="nodes", version=VERSION,
|
76 |
+
description="The CiteSeer dataset"),
|
77 |
+
datasets.BuilderConfig(name="edges", version=VERSION,
|
78 |
+
description="The CiteSeer network")
|
79 |
+
]
|
80 |
+
|
81 |
+
# It's not mandatory to have a default configuration. Just use one if it make sense.
|
82 |
+
DEFAULT_CONFIG_NAME = "nodes"
|
83 |
+
|
84 |
+
def _info(self):
|
85 |
+
if self.config.name == "nodes":
|
86 |
+
word_features = [f"word{i}" for i in range(3703)]
|
87 |
+
|
88 |
+
features_dict = {
|
89 |
+
w: datasets.Value("bool")
|
90 |
+
for w in word_features
|
91 |
+
}
|
92 |
+
features_dict["node"] = datasets.Value("string")
|
93 |
+
features_dict["label"] = datasets.ClassLabel(names=_CLASS_LABELS)
|
94 |
+
features_dict["neighbors"] = datasets.Sequence(
|
95 |
+
datasets.Value("string")
|
96 |
+
)
|
97 |
+
features = datasets.Features(features_dict)
|
98 |
+
elif self.config.name == "edges": # This is an example to show how to have different features for "first_domain" and "second_domain"
|
99 |
+
features = datasets.Features(
|
100 |
+
{
|
101 |
+
"source": datasets.Value("string"),
|
102 |
+
"target": datasets.Value("string")
|
103 |
+
}
|
104 |
+
)
|
105 |
+
return datasets.DatasetInfo(
|
106 |
+
# This is the description that will appear on the datasets page.
|
107 |
+
description=_DESCRIPTION,
|
108 |
+
# This defines the different columns of the dataset and their types
|
109 |
+
# Here we define them above because they are different between the two configurations
|
110 |
+
features=features,
|
111 |
+
# If there's a common (input, target) tuple from the features,
|
112 |
+
# specify them here. They'll be used if as_supervised=True in
|
113 |
+
# builder.as_dataset.
|
114 |
+
supervised_keys=None,
|
115 |
+
# Homepage of the dataset for documentation
|
116 |
+
homepage=_HOMEPAGE,
|
117 |
+
# License for the dataset if available
|
118 |
+
license=_LICENSE,
|
119 |
+
# Citation for the dataset
|
120 |
+
citation=_CITATION,
|
121 |
+
)
|
122 |
+
|
123 |
+
def _split_generators(self, dl_manager):
|
124 |
+
"""Returns SplitGenerators."""
|
125 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
126 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
127 |
+
|
128 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
|
129 |
+
# 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.
|
130 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
131 |
+
my_urls = _URLs[self.config.name]
|
132 |
+
data_dir = dl_manager.download_and_extract(my_urls)
|
133 |
+
data_dir = os.path.join(data_dir, "citeseer")
|
134 |
+
|
135 |
+
return [
|
136 |
+
datasets.SplitGenerator(
|
137 |
+
name=datasets.Split.TRAIN,
|
138 |
+
# These kwargs will be passed to _generate_examples
|
139 |
+
gen_kwargs={
|
140 |
+
"edges_path": os.path.join(data_dir, "citeseer.cites"),
|
141 |
+
"nodes_path": os.path.join(data_dir, "citeseer.content"),
|
142 |
+
"split": "train"
|
143 |
+
}
|
144 |
+
)
|
145 |
+
]
|
146 |
+
|
147 |
+
def _generate_examples(
|
148 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
149 |
+
self, edges_path, nodes_path, split
|
150 |
+
):
|
151 |
+
""" Yields examples as (key, example) tuples. """
|
152 |
+
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
153 |
+
# The `key` is here for legacy reason (tfds) and is not important in itself.
|
154 |
+
|
155 |
+
if self.config.name == "nodes":
|
156 |
+
neighbors = {}
|
157 |
+
with open(edges_path, "rt", encoding="UTF-8") as f:
|
158 |
+
for line in f:
|
159 |
+
target, src = line.strip().split()
|
160 |
+
for n in (target, src):
|
161 |
+
if n not in neighbors:
|
162 |
+
neighbors[n] = []
|
163 |
+
neighbors[src].append(target)
|
164 |
+
|
165 |
+
colnames = ["node"] + \
|
166 |
+
[f"word{i}" for i in range(3703)] + ["label"]
|
167 |
+
dtypes = [str] + [bool] * 3703 + [str]
|
168 |
+
nodes = pandas.read_csv(
|
169 |
+
nodes_path,
|
170 |
+
sep="\t",
|
171 |
+
header=None,
|
172 |
+
names=colnames,
|
173 |
+
dtype=dict(zip(colnames, dtypes))
|
174 |
+
)
|
175 |
+
col2idx = {col: i for i, col in enumerate(list(nodes))}
|
176 |
+
for id, row in enumerate(nodes.itertuples(index=False, name=None)):
|
177 |
+
n = row[col2idx["node"]]
|
178 |
+
features = {
|
179 |
+
"node": n,
|
180 |
+
"label": row[col2idx["label"]],
|
181 |
+
"neighbors": neighbors[n]
|
182 |
+
}
|
183 |
+
for i in range(3703):
|
184 |
+
feature_name = f"word{i}"
|
185 |
+
features[feature_name] = row[col2idx[feature_name]]
|
186 |
+
yield id, features
|
187 |
+
|
188 |
+
elif self.config.name == "edges":
|
189 |
+
with open(edges_path, "rt", encoding="UTF-8") as f:
|
190 |
+
for id, line in enumerate(f):
|
191 |
+
target, src = line.strip().split()
|
192 |
+
yield id, {"source": src, "target": target}
|