ruanchaves commited on
Commit
f426013
1 Parent(s): 94d5c8a

Update stan_small.py

Browse files
Files changed (1) hide show
  1. stan_small.py +37 -11
stan_small.py CHANGED
@@ -2,7 +2,6 @@
2
 
3
  import datasets
4
  import pandas as pd
5
- import pickle5 as pickle
6
 
7
  _CITATION = """
8
  @misc{bansal2015deep,
@@ -55,22 +54,49 @@ class StanSmall(datasets.GeneratorBasedBuilder):
55
  def _generate_examples(self, filepath):
56
 
57
  def get_segmentation(row):
58
- return row["goldtruths"][0]
59
-
60
- def get_alternatives(row):
61
- segmentations = [{
62
- "segmentation": x
63
- } for x in row["goldtruths"]]
 
 
 
 
 
 
 
 
 
 
64
 
65
- return segmentations[1:]
 
 
 
 
66
 
67
  with open(filepath, 'rb') as f:
68
- records = pickle.load(f)
 
 
 
 
 
 
 
 
 
 
 
69
  records = records.to_dict("records")
70
  for idx, row in enumerate(records):
 
 
71
  yield idx, {
72
  "index": idx,
73
  "hashtag": row["hashtags"],
74
- "segmentation": get_segmentation(row),
75
- "alternatives": get_alternatives(row)
76
  }
2
 
3
  import datasets
4
  import pandas as pd
 
5
 
6
  _CITATION = """
7
  @misc{bansal2015deep,
54
  def _generate_examples(self, filepath):
55
 
56
  def get_segmentation(row):
57
+ needle = row["hashtags"]
58
+ haystack = row["goldtruths"][0].strip()
59
+ output = ""
60
+ iterator = iter(haystack)
61
+ for char in needle:
62
+ output += char
63
+ while True:
64
+ try:
65
+ next_char = next(iterator)
66
+ if next_char.lower() == char.lower():
67
+ break
68
+ elif next_char.isspace():
69
+ output = output[0:-1] + next_char + output[-1]
70
+ except StopIteration:
71
+ break
72
+ return output
73
 
74
+ def get_alternatives(row, segmentation):
75
+ alts = list(set([x.strip() for x in row["goldtruths"]]))
76
+ alts = [x for x in alts if x != segmentation]
77
+ alts = [{"segmentation": x} for x in alts]
78
+ return alts
79
 
80
  with open(filepath, 'rb') as f:
81
+ try:
82
+ import pickle
83
+ records = pickle.load(f)
84
+ except ValueError:
85
+ try:
86
+ import pickle5 as pickle
87
+ records = pickle.load(f)
88
+ except ModuleNotFoundError:
89
+ raise ImportError(
90
+ """To be able to use stan_small, you need to install the following dependencies['pickle5']
91
+ using 'pip install pickle5' for instance"""
92
+ )
93
  records = records.to_dict("records")
94
  for idx, row in enumerate(records):
95
+ segmentation = get_segmentation(row)
96
+ alternatives = get_alternatives(row, segmentation)
97
  yield idx, {
98
  "index": idx,
99
  "hashtag": row["hashtags"],
100
+ "segmentation": segmentation,
101
+ "alternatives": alternatives
102
  }