Kevin99z commited on
Commit
0103198
1 Parent(s): 4f290ba

update utils

Browse files
Files changed (2) hide show
  1. inspired_unicrs.py +3 -1
  2. utils.py +54 -0
inspired_unicrs.py CHANGED
@@ -1,7 +1,9 @@
1
  import json
2
  from typing import List, Dict
3
  import datasets
4
- from dataset.utils import markup_entity
 
 
5
 
6
  logger = datasets.logging.get_logger(__name__)
7
 
 
1
  import json
2
  from typing import List, Dict
3
  import datasets
4
+ import sys
5
+ sys.path.append('../')
6
+ from utils import markup_entity
7
 
8
  logger = datasets.logging.get_logger(__name__)
9
 
utils.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import sys
3
+ from typing import List
4
+
5
+ from datasets import load_dataset, Value, Sequence
6
+ import json
7
+
8
+ ENTITY = 'entity'
9
+ ENTITY_PATTERN = r'<entity> (.*?) </entity>'
10
+
11
+
12
+ def markup_entity(utt: str, entities: List[str]):
13
+ # If entities like "action movie" and "action" appear at the same time, we only mark the longer one
14
+ entities = sorted(list(set(entities)), key=lambda x: len(x), reverse=True)
15
+ for i, entity in enumerate(entities):
16
+ valid = entity not in ENTITY
17
+ for prev in entities[:i]:
18
+ if entity in prev:
19
+ valid = False
20
+ if valid:
21
+ utt = re.sub(entity, ENTITY_PATTERN.format(entity), utt)
22
+ return utt
23
+
24
+ def print_dataset_info(dataset):
25
+ # Print the features
26
+ print("features:")
27
+ for feature_name, feature in dataset.features.items():
28
+ if True:
29
+ print(f" - name: {feature_name}")
30
+ if isinstance(feature, Value):
31
+ print(f" dtype: {feature.dtype}")
32
+ elif isinstance(feature, Sequence):
33
+ print(f" sequence: {feature.feature.dtype}")
34
+
35
+ # Print the splits information
36
+ print("splits:")
37
+ for split_name, split_info in dataset.info.splits.items():
38
+ print(f" - name: {split_name}")
39
+ print(f" num_bytes: {split_info.num_bytes}")
40
+ print(f" num_examples: {split_info.num_examples}")
41
+
42
+ # Print the download size and dataset size
43
+ print(f"download_size: {dataset.info.download_size}")
44
+ print(f"dataset_size: {dataset.info.dataset_size}")
45
+
46
+
47
+ if __name__ == "__main__":
48
+ dataset = load_dataset(sys.argv[1], sys.argv[2])
49
+ print_dataset_info(dataset["train"])
50
+
51
+ print("An example of 'test' looks as follows.")
52
+ print("```")
53
+ print(json.dumps(dataset["test"][0], indent=2, sort_keys=True))
54
+ print("```")