Initial Commit
Browse files- collect_entity.py +49 -0
- collect_movieId2id.py +11 -0
- entityName2id.json +0 -0
- movieId2id.json +0 -0
- redial_unicrs.py +60 -75
- upload_to_hub.py +0 -0
collect_entity.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import re
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def extract_movie_ids(text):
|
| 7 |
+
res = []
|
| 8 |
+
for m in re.finditer(r'@(\d+)', text):
|
| 9 |
+
res.append(m.group(1))
|
| 10 |
+
return res
|
| 11 |
+
|
| 12 |
+
def collect_entity_mappings():
|
| 13 |
+
files = [
|
| 14 |
+
"./test_data_dbpedia.jsonl",
|
| 15 |
+
"./train_data_dbpedia.jsonl",
|
| 16 |
+
"valid_data_dbpedia.jsonl"
|
| 17 |
+
]
|
| 18 |
+
entityName2entity = {}
|
| 19 |
+
|
| 20 |
+
with open('entity2id.json', 'r', encoding='utf-8') as f:
|
| 21 |
+
entity2id = json.load(f)
|
| 22 |
+
|
| 23 |
+
movieId2entity = {}
|
| 24 |
+
for path in files:
|
| 25 |
+
with open(path, 'r') as f:
|
| 26 |
+
for line in f:
|
| 27 |
+
dialog = json.loads(line)
|
| 28 |
+
|
| 29 |
+
for message in dialog["messages"]:
|
| 30 |
+
movieIds = extract_movie_ids(message["text"])
|
| 31 |
+
for name, entity in zip(message['entity_name'], message['entity']):
|
| 32 |
+
entityName2entity[name] = entity
|
| 33 |
+
for movie_name, entity in zip(message['movie_name'], message['movie']):
|
| 34 |
+
entityName2entity[movie_name] = entity
|
| 35 |
+
for movieId, entity in zip(movieIds, message['movie']):
|
| 36 |
+
movieId2entity[movieId] = entity
|
| 37 |
+
|
| 38 |
+
entityName2id = {k: entity2id[entityName2entity[k]] for k in entityName2entity}
|
| 39 |
+
with open('entityName2id.json', 'w') as f:
|
| 40 |
+
json.dump(entityName2id, f)
|
| 41 |
+
|
| 42 |
+
movieId2id = {k: entity2id[movieId2entity[k]] for k in movieId2entity}
|
| 43 |
+
with open('movieId2id.json', 'w') as f:
|
| 44 |
+
json.dump(movieId2id, f)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
collect_entity_mappings()
|
collect_movieId2id.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json, csv
|
| 2 |
+
|
| 3 |
+
movieId2id = {}
|
| 4 |
+
|
| 5 |
+
with open("../redial/movies_merged.csv") as f:
|
| 6 |
+
for row in csv.reader(f):
|
| 7 |
+
if row[0] != "index":
|
| 8 |
+
movieId2id[row[3]] = row[2]
|
| 9 |
+
|
| 10 |
+
with open("movieId2id.json", 'w') as f2:
|
| 11 |
+
json.dump(movieId2id, f2)
|
entityName2id.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
movieId2id.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
redial_unicrs.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
import json
|
| 2 |
import re
|
| 3 |
-
from typing import List
|
| 4 |
import html
|
| 5 |
-
|
| 6 |
import datasets
|
| 7 |
-
import pyarrow as pa
|
| 8 |
|
| 9 |
logger = datasets.logging.get_logger(__name__)
|
| 10 |
|
|
@@ -12,7 +10,11 @@ logger = datasets.logging.get_logger(__name__)
|
|
| 12 |
class RedialConfig(datasets.BuilderConfig):
|
| 13 |
"""BuilderConfig for ReDIAL."""
|
| 14 |
|
| 15 |
-
def __init__(self, features,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
"""BuilderConfig for ReDIAL (used in UniCRS).
|
| 17 |
|
| 18 |
Args:
|
|
@@ -22,7 +24,9 @@ class RedialConfig(datasets.BuilderConfig):
|
|
| 22 |
"""
|
| 23 |
super().__init__(version=datasets.Version("0.0.1"), **kwargs)
|
| 24 |
self.features = features
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
_URL = "./"
|
| 28 |
_URLS = {
|
|
@@ -34,28 +38,9 @@ _URLS = {
|
|
| 34 |
|
| 35 |
|
| 36 |
class ReDIAL(datasets.GeneratorBasedBuilder):
|
| 37 |
-
DEFAULT_CONFIG_NAME = "
|
| 38 |
BUILDER_CONFIGS = [
|
| 39 |
-
|
| 40 |
-
name="multiturn",
|
| 41 |
-
description="The processed ReDIAL dataset in UniCRS. Each conversation yields multiple samples",
|
| 42 |
-
features={
|
| 43 |
-
"context": datasets.Sequence(datasets.Value("string")),
|
| 44 |
-
"resp": datasets.Value("string"),
|
| 45 |
-
"rec": datasets.Sequence(datasets.Value("int32")),
|
| 46 |
-
"entity": datasets.Sequence(datasets.Value("int32")),
|
| 47 |
-
},
|
| 48 |
-
),
|
| 49 |
-
RedialConfig(
|
| 50 |
-
name="multiturn_masked",
|
| 51 |
-
description="",
|
| 52 |
-
features={
|
| 53 |
-
"context": datasets.Sequence(datasets.Value("string")),
|
| 54 |
-
"resp": datasets.Value("string"),
|
| 55 |
-
"rec": datasets.Sequence(datasets.Value("int32")),
|
| 56 |
-
"entity": datasets.Sequence(datasets.Value("int32")),
|
| 57 |
-
},
|
| 58 |
-
),
|
| 59 |
RedialConfig(
|
| 60 |
name="compact",
|
| 61 |
description="Each conversation is one sample",
|
|
@@ -69,8 +54,18 @@ class ReDIAL(datasets.GeneratorBasedBuilder):
|
|
| 69 |
"movies": datasets.Sequence(datasets.Sequence(datasets.Value("string")))
|
| 70 |
},
|
| 71 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
]
|
| 73 |
-
|
| 74 |
|
| 75 |
def __init__(self, **kwargs):
|
| 76 |
super().__init__(**kwargs)
|
|
@@ -102,7 +97,7 @@ class ReDIAL(datasets.GeneratorBasedBuilder):
|
|
| 102 |
return '<movie>'
|
| 103 |
movie_name = movieid2name[movieid]
|
| 104 |
movie_name = ' '.join(movie_name.split())
|
| 105 |
-
return movie_name
|
| 106 |
else:
|
| 107 |
return match.group(0)
|
| 108 |
|
|
@@ -113,14 +108,32 @@ class ReDIAL(datasets.GeneratorBasedBuilder):
|
|
| 113 |
|
| 114 |
return utt
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
def _generate_examples(self, filepath, entity2id):
|
| 117 |
"""This function returns the examples in the raw (text) form."""
|
| 118 |
logger.info("generating examples from = %s", filepath)
|
| 119 |
|
| 120 |
with open(entity2id, 'r', encoding='utf-8') as f:
|
| 121 |
entity2id = json.load(f)
|
| 122 |
-
if
|
| 123 |
-
mask_flag = "mask" in self.config.name
|
| 124 |
Idx = 0
|
| 125 |
with open(filepath, encoding="utf-8") as f:
|
| 126 |
for line in f:
|
|
@@ -131,65 +144,37 @@ class ReDIAL(datasets.GeneratorBasedBuilder):
|
|
| 131 |
movieid2name = dialog['movieMentions']
|
| 132 |
user_id, resp_id = dialog['initiatorWorkerId'], dialog['respondentWorkerId']
|
| 133 |
context, resp = [], ''
|
| 134 |
-
entity_list = []
|
| 135 |
messages = dialog['messages']
|
| 136 |
turn_i = 0
|
| 137 |
while turn_i < len(messages):
|
| 138 |
worker_id = messages[turn_i]['senderWorkerId']
|
| 139 |
utt_turn = []
|
| 140 |
-
entity_turn = []
|
| 141 |
movie_turn = []
|
| 142 |
-
|
| 143 |
-
|
| 144 |
turn_j = turn_i
|
| 145 |
while turn_j < len(messages) and messages[turn_j]['senderWorkerId'] == worker_id:
|
| 146 |
utt = self._process_utt(messages[turn_j]['text'], movieid2name, replace_movieId=True)
|
|
|
|
| 147 |
utt_turn.append(utt)
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
mask_utt = self._process_utt(messages[turn_j]['text'], movieid2name,
|
| 151 |
-
replace_movieId=True,
|
| 152 |
-
remove_movie=True)
|
| 153 |
-
mask_utt_turn.append(mask_utt)
|
| 154 |
-
|
| 155 |
-
entity_ids = [entity2id[entity] for entity in messages[turn_j]['entity'] if
|
| 156 |
-
entity in entity2id]
|
| 157 |
-
entity_turn.extend(entity_ids)
|
| 158 |
-
|
| 159 |
-
movie_ids = [entity2id[movie] for movie in messages[turn_j]['movie'] if movie in entity2id]
|
| 160 |
movie_turn.extend(movie_ids)
|
| 161 |
-
|
| 162 |
turn_j += 1
|
| 163 |
|
| 164 |
utt = ' '.join(utt_turn)
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
context_entity_list_extend += context_entity_list
|
| 177 |
-
context_entity_list_extend = list(set(context_entity_list_extend))
|
| 178 |
-
|
| 179 |
-
if len(context) == 0:
|
| 180 |
-
context.append('')
|
| 181 |
-
yield Idx, {
|
| 182 |
-
'context': context,
|
| 183 |
-
'resp': mask_utt if mask_flag else resp,
|
| 184 |
-
'rec': movie_turn if mask_flag else list(set(movie_turn + entity_turn)),
|
| 185 |
-
'entity': context_entity_list_extend,
|
| 186 |
-
}
|
| 187 |
-
Idx += 1
|
| 188 |
-
|
| 189 |
-
context.append(resp)
|
| 190 |
-
entity_list.append(movie_turn + entity_turn)
|
| 191 |
-
|
| 192 |
turn_i = turn_j
|
|
|
|
| 193 |
elif self.config.name == "compact":
|
| 194 |
Idx = 0
|
| 195 |
with open(filepath, encoding="utf-8") as f:
|
|
@@ -212,4 +197,4 @@ class ReDIAL(datasets.GeneratorBasedBuilder):
|
|
| 212 |
messages]
|
| 213 |
}
|
| 214 |
|
| 215 |
-
Idx += 1
|
|
|
|
| 1 |
import json
|
| 2 |
import re
|
| 3 |
+
from typing import List
|
| 4 |
import html
|
|
|
|
| 5 |
import datasets
|
|
|
|
| 6 |
|
| 7 |
logger = datasets.logging.get_logger(__name__)
|
| 8 |
|
|
|
|
| 10 |
class RedialConfig(datasets.BuilderConfig):
|
| 11 |
"""BuilderConfig for ReDIAL."""
|
| 12 |
|
| 13 |
+
def __init__(self, features,
|
| 14 |
+
initiator_prefix='User: ',
|
| 15 |
+
respondent_prefix='System: ',
|
| 16 |
+
separator='<sep>',
|
| 17 |
+
**kwargs):
|
| 18 |
"""BuilderConfig for ReDIAL (used in UniCRS).
|
| 19 |
|
| 20 |
Args:
|
|
|
|
| 24 |
"""
|
| 25 |
super().__init__(version=datasets.Version("0.0.1"), **kwargs)
|
| 26 |
self.features = features
|
| 27 |
+
self.initiator_prefix = initiator_prefix
|
| 28 |
+
self.respondent_prefix = respondent_prefix
|
| 29 |
+
self.separator = separator
|
| 30 |
|
| 31 |
_URL = "./"
|
| 32 |
_URLS = {
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
class ReDIAL(datasets.GeneratorBasedBuilder):
|
| 41 |
+
DEFAULT_CONFIG_NAME = "unrolled"
|
| 42 |
BUILDER_CONFIGS = [
|
| 43 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
RedialConfig(
|
| 45 |
name="compact",
|
| 46 |
description="Each conversation is one sample",
|
|
|
|
| 54 |
"movies": datasets.Sequence(datasets.Sequence(datasets.Value("string")))
|
| 55 |
},
|
| 56 |
),
|
| 57 |
+
|
| 58 |
+
RedialConfig(
|
| 59 |
+
name="unrolled",
|
| 60 |
+
description="Formatted unrolled dialog messages. The `rec` and `recNames` are the movies from the last message",
|
| 61 |
+
features={
|
| 62 |
+
"messages": datasets.Sequence(datasets.Value("string")),
|
| 63 |
+
"rec": datasets.Sequence(datasets.Value("int32")),
|
| 64 |
+
"recNames": datasets.Sequence(datasets.Value("string")),
|
| 65 |
+
}
|
| 66 |
+
),
|
| 67 |
]
|
| 68 |
+
|
| 69 |
|
| 70 |
def __init__(self, **kwargs):
|
| 71 |
super().__init__(**kwargs)
|
|
|
|
| 97 |
return '<movie>'
|
| 98 |
movie_name = movieid2name[movieid]
|
| 99 |
movie_name = ' '.join(movie_name.split())
|
| 100 |
+
return self.entity_pattern.format(movie_name)
|
| 101 |
else:
|
| 102 |
return match.group(0)
|
| 103 |
|
|
|
|
| 108 |
|
| 109 |
return utt
|
| 110 |
|
| 111 |
+
def _mark_entity(self, utt: str, entities: List[str]):
|
| 112 |
+
# If entities like "action movie" and "action" appear at the same time, we only mark the longer one
|
| 113 |
+
entities = sorted(list(set(entities)), key=lambda x: len(x), reverse=True)
|
| 114 |
+
for i, entity in enumerate(entities):
|
| 115 |
+
valid = True
|
| 116 |
+
for prev in entities[:i]:
|
| 117 |
+
if entity in prev:
|
| 118 |
+
valid = False
|
| 119 |
+
if valid:
|
| 120 |
+
utt = re.sub(entity, self.entity_pattern.format(entity), utt)
|
| 121 |
+
return utt
|
| 122 |
+
|
| 123 |
+
movie_pattern = re.compile(r'@(\d+)')
|
| 124 |
+
default_movie_entity = '<movie>'
|
| 125 |
+
entity_pattern = "<entity>{}</entity>"
|
| 126 |
+
def map_movie_pattern_to_entity(self, id2entity, id: str):
|
| 127 |
+
entity = id2entity[id] if id in id2entity else self.default_movie_entity
|
| 128 |
+
return self.entity_pattern.format_map(entity)
|
| 129 |
+
|
| 130 |
def _generate_examples(self, filepath, entity2id):
|
| 131 |
"""This function returns the examples in the raw (text) form."""
|
| 132 |
logger.info("generating examples from = %s", filepath)
|
| 133 |
|
| 134 |
with open(entity2id, 'r', encoding='utf-8') as f:
|
| 135 |
entity2id = json.load(f)
|
| 136 |
+
if self.config.name == "unrolled":
|
|
|
|
| 137 |
Idx = 0
|
| 138 |
with open(filepath, encoding="utf-8") as f:
|
| 139 |
for line in f:
|
|
|
|
| 144 |
movieid2name = dialog['movieMentions']
|
| 145 |
user_id, resp_id = dialog['initiatorWorkerId'], dialog['respondentWorkerId']
|
| 146 |
context, resp = [], ''
|
|
|
|
| 147 |
messages = dialog['messages']
|
| 148 |
turn_i = 0
|
| 149 |
while turn_i < len(messages):
|
| 150 |
worker_id = messages[turn_i]['senderWorkerId']
|
| 151 |
utt_turn = []
|
|
|
|
| 152 |
movie_turn = []
|
| 153 |
+
movie_turn_names = []
|
|
|
|
| 154 |
turn_j = turn_i
|
| 155 |
while turn_j < len(messages) and messages[turn_j]['senderWorkerId'] == worker_id:
|
| 156 |
utt = self._process_utt(messages[turn_j]['text'], movieid2name, replace_movieId=True)
|
| 157 |
+
utt = self._mark_entity(utt, messages[turn_j]['entity_name'])
|
| 158 |
utt_turn.append(utt)
|
| 159 |
+
movie_ids = [entity2id[movie] for movie in messages[turn_j]['movie'] if
|
| 160 |
+
movie in entity2id]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
movie_turn.extend(movie_ids)
|
| 162 |
+
movie_turn_names.extend(messages[turn_j]['movie_name'])
|
| 163 |
turn_j += 1
|
| 164 |
|
| 165 |
utt = ' '.join(utt_turn)
|
| 166 |
+
prefix = self.config.initiator_prefix if worker_id == user_id else self.config.respondent_prefix
|
| 167 |
+
resp = prefix + utt
|
| 168 |
+
context.append(resp)
|
| 169 |
+
|
| 170 |
+
yield Idx, {
|
| 171 |
+
'messages': context,
|
| 172 |
+
'rec': movie_turn,
|
| 173 |
+
'recNames': movie_turn_names,
|
| 174 |
+
}
|
| 175 |
+
Idx += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
turn_i = turn_j
|
| 177 |
+
|
| 178 |
elif self.config.name == "compact":
|
| 179 |
Idx = 0
|
| 180 |
with open(filepath, encoding="utf-8") as f:
|
|
|
|
| 197 |
messages]
|
| 198 |
}
|
| 199 |
|
| 200 |
+
Idx += 1
|
upload_to_hub.py
ADDED
|
File without changes
|