zhsy commited on
Commit
01a28b3
·
1 Parent(s): 8966f0a

Upload Walmart-Amazon-SM.py

Browse files
Files changed (1) hide show
  1. Walmart-Amazon-SM.py +101 -0
Walmart-Amazon-SM.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import pandas as pd
3
+
4
+ class WalmartAmazonSMConfig(datasets.BuilderConfig):
5
+
6
+ def __init__(self, features, data_url, **kwargs):
7
+ super(WalmartAmazonSMConfig, self).__init__(**kwargs)
8
+ self.features = features
9
+ self.data_url = data_url
10
+
11
+ class WalmartAmazonSM(datasets.GeneratorBasedBuilder):
12
+
13
+ BUILDER_CONFIGS = [
14
+ WalmartAmazonSMConfig(
15
+ name="pairs",
16
+ features={
17
+ "column1": datasets.Value("string"),
18
+ "column2": datasets.Value("string"),
19
+ "label" : datasets.Value("bool")
20
+ },
21
+ data_url="https://huggingface.co/datasets/matchbench/Walmart-Amazon-SM/resolve/main/matches.txt"
22
+ ),
23
+ WalmartAmazonSMConfig(
24
+ name="source",
25
+ features={
26
+ "json": datasets.Value("string")
27
+ },
28
+ data_url="https://huggingface.co/datasets/matchbench/Walmart-Amazon-SM/resolve/main/walmart_amazon-sm-1.csv"
29
+ ),
30
+ WalmartAmazonSMConfig(
31
+ name="target",
32
+ features={
33
+ "json": datasets.Value("string")
34
+ },
35
+ data_url="https://huggingface.co/datasets/matchbench/Walmart-Amazon-SM/resolve/main/walmart_amazon-sm-2.csv"
36
+ ),
37
+ ]
38
+
39
+ def _info(self):
40
+ return datasets.DatasetInfo(
41
+ features=datasets.Features(self.config.features)
42
+ )
43
+
44
+ def _split_generators(self, dl_manager):
45
+ path = dl_manager.download(self.config.data_url)
46
+ if self.config.name == "pairs":
47
+ return [
48
+ datasets.SplitGenerator(
49
+ name=split,
50
+ gen_kwargs={
51
+ "file_path": path
52
+ }
53
+ )
54
+ for split in [
55
+ datasets.Split.TEST
56
+ ]
57
+ ]
58
+ elif self.config.name == "source":
59
+ return [
60
+ datasets.SplitGenerator(
61
+ name="source",
62
+ gen_kwargs={
63
+ "file_path": path
64
+ }
65
+ )
66
+ ]
67
+ elif self.config.name == "target":
68
+ return [
69
+ datasets.SplitGenerator(
70
+ name="target",
71
+ gen_kwargs={
72
+ "file_path": path
73
+ }
74
+ )
75
+ ]
76
+
77
+ def _generate_examples(self, file_path):
78
+ if self.config.name == "pairs":
79
+ with open(file_path, "r") as f:
80
+ md = {}
81
+ for _, line in enumerate(f):
82
+ t = line.strip().split(',')
83
+ if t[0] not in md:
84
+ md[t[0]] = {t[1]}
85
+ else:
86
+ md[t[0]].add(t[1])
87
+ id = -1
88
+ for k, v in md.items():
89
+ for i in v:
90
+ id = id + 1
91
+ yield id, {"column1": k, "column2": i, "label": True}
92
+ elif self.config.name == "source":
93
+ with open(file_path, "r") as f:
94
+ source = pd.read_csv(file_path)
95
+ source = source.to_json()
96
+ yield 0, {"json": source}
97
+ elif self.config.name == "target":
98
+ with open(file_path, "r") as f:
99
+ target = pd.read_csv(file_path)
100
+ target = target.to_json()
101
+ yield 0, {"json": target}