Update entity marking
Browse files- collect_entity.py +3 -18
- redial_unicrs.py +18 -20
collect_entity.py
CHANGED
@@ -3,16 +3,10 @@ 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 |
-
"
|
15 |
-
"
|
16 |
"valid_data_dbpedia.jsonl"
|
17 |
]
|
18 |
entityName2entity = {}
|
@@ -20,30 +14,21 @@ def collect_entity_mappings():
|
|
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 |
-
|
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()
|
|
|
3 |
import os
|
4 |
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def collect_entity_mappings():
|
7 |
files = [
|
8 |
+
"test_data_dbpedia.jsonl",
|
9 |
+
"train_data_dbpedia.jsonl",
|
10 |
"valid_data_dbpedia.jsonl"
|
11 |
]
|
12 |
entityName2entity = {}
|
|
|
14 |
with open('entity2id.json', 'r', encoding='utf-8') as f:
|
15 |
entity2id = json.load(f)
|
16 |
|
|
|
17 |
for path in files:
|
18 |
with open(path, 'r') as f:
|
19 |
for line in f:
|
20 |
dialog = json.loads(line)
|
|
|
21 |
for message in dialog["messages"]:
|
|
|
22 |
for name, entity in zip(message['entity_name'], message['entity']):
|
23 |
entityName2entity[name] = entity
|
24 |
for movie_name, entity in zip(message['movie_name'], message['movie']):
|
25 |
entityName2entity[movie_name] = entity
|
26 |
+
|
|
|
27 |
|
28 |
entityName2id = {k: entity2id[entityName2entity[k]] for k in entityName2entity}
|
29 |
with open('entityName2id.json', 'w') as f:
|
30 |
json.dump(entityName2id, f)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
if __name__ == "__main__":
|
34 |
collect_entity_mappings()
|
redial_unicrs.py
CHANGED
@@ -4,6 +4,22 @@ from typing import List
|
|
4 |
import html
|
5 |
import datasets
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
logger = datasets.logging.get_logger(__name__)
|
8 |
|
9 |
|
@@ -13,7 +29,6 @@ class RedialConfig(datasets.BuilderConfig):
|
|
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 |
|
@@ -26,7 +41,6 @@ class RedialConfig(datasets.BuilderConfig):
|
|
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 = {
|
@@ -97,7 +111,7 @@ class ReDIAL(datasets.GeneratorBasedBuilder):
|
|
97 |
return '<movie>'
|
98 |
movie_name = movieid2name[movieid]
|
99 |
movie_name = ' '.join(movie_name.split())
|
100 |
-
return
|
101 |
else:
|
102 |
return match.group(0)
|
103 |
|
@@ -108,24 +122,8 @@ class ReDIAL(datasets.GeneratorBasedBuilder):
|
|
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."""
|
@@ -154,7 +152,7 @@ class ReDIAL(datasets.GeneratorBasedBuilder):
|
|
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 =
|
158 |
utt_turn.append(utt)
|
159 |
movie_ids = [entity2id[movie] for movie in messages[turn_j]['movie'] if
|
160 |
movie in entity2id]
|
|
|
4 |
import html
|
5 |
import datasets
|
6 |
|
7 |
+
ENTITY = 'entity'
|
8 |
+
ENTITY_PATTERN = r'<entity> {} </entity>'
|
9 |
+
|
10 |
+
|
11 |
+
def markup_entity(utt: str, entities: List[str]):
|
12 |
+
# If entities like "action movie" and "action" appear at the same time, we only mark the longer one
|
13 |
+
entities = sorted(list(set(entities)), key=lambda x: len(x), reverse=True)
|
14 |
+
for i, entity in enumerate(entities):
|
15 |
+
valid = entity not in ENTITY
|
16 |
+
for prev in entities[:i]:
|
17 |
+
if entity in prev:
|
18 |
+
valid = False
|
19 |
+
if valid:
|
20 |
+
utt = re.sub(entity, ENTITY_PATTERN.format(entity), utt)
|
21 |
+
return utt
|
22 |
+
|
23 |
logger = datasets.logging.get_logger(__name__)
|
24 |
|
25 |
|
|
|
29 |
def __init__(self, features,
|
30 |
initiator_prefix='User: ',
|
31 |
respondent_prefix='System: ',
|
|
|
32 |
**kwargs):
|
33 |
"""BuilderConfig for ReDIAL (used in UniCRS).
|
34 |
|
|
|
41 |
self.features = features
|
42 |
self.initiator_prefix = initiator_prefix
|
43 |
self.respondent_prefix = respondent_prefix
|
|
|
44 |
|
45 |
_URL = "./"
|
46 |
_URLS = {
|
|
|
111 |
return '<movie>'
|
112 |
movie_name = movieid2name[movieid]
|
113 |
movie_name = ' '.join(movie_name.split())
|
114 |
+
return movie_name
|
115 |
else:
|
116 |
return match.group(0)
|
117 |
|
|
|
122 |
|
123 |
return utt
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
movie_pattern = re.compile(r'@(\d+)')
|
126 |
default_movie_entity = '<movie>'
|
|
|
|
|
|
|
|
|
127 |
|
128 |
def _generate_examples(self, filepath, entity2id):
|
129 |
"""This function returns the examples in the raw (text) form."""
|
|
|
152 |
turn_j = turn_i
|
153 |
while turn_j < len(messages) and messages[turn_j]['senderWorkerId'] == worker_id:
|
154 |
utt = self._process_utt(messages[turn_j]['text'], movieid2name, replace_movieId=True)
|
155 |
+
utt = markup_entity(utt, messages[turn_j]['entity_name']+messages[turn_j]['movie_name'])
|
156 |
utt_turn.append(utt)
|
157 |
movie_ids = [entity2id[movie] for movie in messages[turn_j]['movie'] if
|
158 |
movie in entity2id]
|