Commit ·
d3df06b
1
Parent(s): 36bcc6b
Update stan_large.py
Browse files- stan_large.py +38 -11
stan_large.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
"""STAN large dataset"""
|
| 2 |
|
|
|
|
| 3 |
import datasets
|
| 4 |
import pandas as pd
|
| 5 |
-
import pickle5 as pickle
|
| 6 |
|
| 7 |
_CITATION = """
|
| 8 |
@inproceedings{maddela-etal-2019-multi,
|
|
@@ -77,22 +77,49 @@ class StanLarge(datasets.GeneratorBasedBuilder):
|
|
| 77 |
def _generate_examples(self, filepath):
|
| 78 |
|
| 79 |
def get_segmentation(row):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
with open(filepath, 'rb') as f:
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
records = records.to_dict("records")
|
| 92 |
for idx, row in enumerate(records):
|
|
|
|
|
|
|
| 93 |
yield idx, {
|
| 94 |
"index": idx,
|
| 95 |
"hashtag": row["hashtags"],
|
| 96 |
-
"segmentation":
|
| 97 |
-
"alternatives":
|
| 98 |
}
|
|
|
|
| 1 |
"""STAN large dataset"""
|
| 2 |
|
| 3 |
+
from multiprocessing.sharedctypes import Value
|
| 4 |
import datasets
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
_CITATION = """
|
| 8 |
@inproceedings{maddela-etal-2019-multi,
|
|
|
|
| 77 |
def _generate_examples(self, filepath):
|
| 78 |
|
| 79 |
def get_segmentation(row):
|
| 80 |
+
needle = row["hashtags"]
|
| 81 |
+
haystack = row["goldtruths"][0].strip()
|
| 82 |
+
output = ""
|
| 83 |
+
iterator = iter(haystack)
|
| 84 |
+
for char in needle:
|
| 85 |
+
output += char
|
| 86 |
+
while True:
|
| 87 |
+
try:
|
| 88 |
+
next_char = next(iterator)
|
| 89 |
+
if next_char.lower() == char.lower():
|
| 90 |
+
break
|
| 91 |
+
elif next_char.isspace():
|
| 92 |
+
output = output[0:-1] + next_char + output[-1]
|
| 93 |
+
except StopIteration:
|
| 94 |
+
break
|
| 95 |
+
return output
|
| 96 |
|
| 97 |
+
def get_alternatives(row, segmentation):
|
| 98 |
+
alts = list(set([x.strip() for x in row["goldtruths"]]))
|
| 99 |
+
alts = [x for x in alts if x != segmentation]
|
| 100 |
+
alts = [{"segmentation": x} for x in alts]
|
| 101 |
+
return alts
|
| 102 |
|
| 103 |
with open(filepath, 'rb') as f:
|
| 104 |
+
try:
|
| 105 |
+
import pickle
|
| 106 |
+
records = pickle.load(f)
|
| 107 |
+
except ValueError:
|
| 108 |
+
try:
|
| 109 |
+
import pickle5 as pickle
|
| 110 |
+
records = pickle.load(f)
|
| 111 |
+
except ModuleNotFoundError:
|
| 112 |
+
raise ImportError(
|
| 113 |
+
"""To be able to use stan_large, you need to install the following dependencies['pickle5']
|
| 114 |
+
using 'pip install pickle5' for instance"""
|
| 115 |
+
)
|
| 116 |
records = records.to_dict("records")
|
| 117 |
for idx, row in enumerate(records):
|
| 118 |
+
segmentation = get_segmentation(row)
|
| 119 |
+
alternatives = get_alternatives(row, segmentation)
|
| 120 |
yield idx, {
|
| 121 |
"index": idx,
|
| 122 |
"hashtag": row["hashtags"],
|
| 123 |
+
"segmentation": segmentation,
|
| 124 |
+
"alternatives": alternatives
|
| 125 |
}
|