Kriyans commited on
Commit
4477da2
1 Parent(s): 6502d83

Create kriyans.py

Browse files
Files changed (1) hide show
  1. kriyans.py +83 -0
kriyans.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+
3
+ _URL = "https://colab.research.google.com/drive/1aSWUrQFWU_RJOt9NmVDRnn9ci-11dC9x#scrollTo=LOncaJUnrl8O"
4
+
5
+ # Define the path to your CSV file
6
+ csv_file_path = '/indian-name-org.csv'
7
+
8
+ class YourCustomConfig(datasets.BuilderConfig):
9
+ def __init__(self, **kwargs):
10
+ super(YourCustomConfig, self).__init__(**kwargs)
11
+
12
+ class YourCustomDataset(datasets.GeneratorBasedBuilder):
13
+ BUILDER_CONFIGS = [
14
+ YourCustomConfig(
15
+ name="your_custom_dataset",
16
+ version=datasets.Version("1.0.0"),
17
+ description="Your Custom Dataset",
18
+ ),
19
+ ]
20
+
21
+ def _info(self):
22
+ return datasets.DatasetInfo(
23
+ description="Your custom dataset description",
24
+ features=datasets.Features(
25
+ {
26
+ "id": datasets.Value("string"),
27
+ "tokens": datasets.Sequence(datasets.Value("string")),
28
+ "ner_tags": datasets.Sequence(
29
+ datasets.features.ClassLabel(
30
+ names=[
31
+ "B-PER",
32
+ "I-ORG",
33
+ ]
34
+ )
35
+ ),
36
+ }
37
+ ),
38
+ supervised_keys=None,
39
+ )
40
+
41
+ def _split_generators(self, dl_manager):
42
+ urls_to_download = {
43
+ "train": f"{_URL}{csv_file_path}",
44
+ }
45
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
46
+
47
+ return [
48
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
49
+ ]
50
+
51
+ def _generate_examples(self, filepath):
52
+ with open(filepath, encoding="utf-8") as f:
53
+ current_tokens = []
54
+ current_labels = []
55
+ sentence_counter = 0
56
+ for row in f:
57
+ row = row.rstrip()
58
+ if row:
59
+ token, label = row.split(",")
60
+ current_tokens.append(token)
61
+ current_labels.append(label)
62
+ else:
63
+ if not current_tokens:
64
+ continue
65
+ assert len(current_tokens) == len(current_labels), "Mismatch between tokens and labels"
66
+ sentence = (
67
+ sentence_counter,
68
+ {
69
+ "id": str(sentence_counter),
70
+ "tokens": current_tokens,
71
+ "ner_tags": current_labels,
72
+ },
73
+ )
74
+ sentence_counter += 1
75
+ current_tokens = []
76
+ current_labels = []
77
+ yield sentence
78
+ if current_tokens:
79
+ yield sentence_counter, {
80
+ "id": str(sentence_counter),
81
+ "tokens": current_tokens,
82
+ "ner_tags": current_labels,
83
+ }