2015-05-05 02:00:27 +03:00
|
|
|
"""Convert OntoNotes into a json format.
|
|
|
|
|
|
|
|
doc: {
|
|
|
|
id: string,
|
|
|
|
paragraphs: [{
|
|
|
|
raw: string,
|
|
|
|
sents: [int],
|
|
|
|
tokens: [{
|
|
|
|
start: int,
|
|
|
|
tag: string,
|
|
|
|
head: int,
|
2015-07-02 09:31:12 +03:00
|
|
|
dep: string,
|
|
|
|
ssenses: [int]}],
|
2015-05-26 20:28:29 +03:00
|
|
|
ner: [{
|
|
|
|
start: int,
|
|
|
|
end: int,
|
|
|
|
label: string}],
|
2015-05-05 02:00:27 +03:00
|
|
|
brackets: [{
|
|
|
|
start: int,
|
|
|
|
end: int,
|
2015-05-26 20:28:29 +03:00
|
|
|
label: string}]}]}
|
2015-05-06 17:31:00 +03:00
|
|
|
|
|
|
|
Consumes output of spacy/munge/align_raw.py
|
2015-05-05 02:00:27 +03:00
|
|
|
"""
|
2015-05-27 18:04:29 +03:00
|
|
|
from __future__ import unicode_literals
|
2015-05-05 02:00:27 +03:00
|
|
|
import plac
|
|
|
|
import json
|
|
|
|
from os import path
|
2015-05-27 18:04:29 +03:00
|
|
|
import os
|
2015-05-05 02:00:27 +03:00
|
|
|
import re
|
2015-05-27 18:04:29 +03:00
|
|
|
import codecs
|
2015-05-28 23:40:01 +03:00
|
|
|
from collections import defaultdict
|
2015-05-05 02:00:27 +03:00
|
|
|
|
|
|
|
from spacy.munge import read_ptb
|
|
|
|
from spacy.munge import read_conll
|
2015-05-26 20:28:29 +03:00
|
|
|
from spacy.munge import read_ner
|
2015-07-02 09:31:12 +03:00
|
|
|
from spacy.munge import read_wordnet
|
2015-05-05 02:00:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
def _iter_raw_files(raw_loc):
|
|
|
|
files = json.load(open(raw_loc))
|
|
|
|
for f in files:
|
|
|
|
yield f
|
|
|
|
|
|
|
|
|
2015-07-02 09:31:12 +03:00
|
|
|
def format_doc(file_id, raw_paras, ptb_text, dep_text, ner_text, senses):
|
2015-05-26 20:28:29 +03:00
|
|
|
ptb_sents = read_ptb.split(ptb_text)
|
|
|
|
dep_sents = read_conll.split(dep_text)
|
2015-05-27 18:04:29 +03:00
|
|
|
if len(ptb_sents) != len(dep_sents):
|
|
|
|
return None
|
|
|
|
if ner_text is not None:
|
|
|
|
ner_sents = read_ner.split(ner_text)
|
|
|
|
else:
|
|
|
|
ner_sents = [None] * len(ptb_sents)
|
2015-05-05 02:00:27 +03:00
|
|
|
|
|
|
|
i = 0
|
2015-05-27 18:04:29 +03:00
|
|
|
doc = {'id': file_id}
|
|
|
|
if raw_paras is None:
|
2015-07-02 09:31:12 +03:00
|
|
|
doc['paragraphs'] = [format_para(None, ptb_sents, dep_sents, ner_sents,
|
|
|
|
[senses[j] for j in range(len(ptb_sents))])]
|
2015-05-28 23:40:01 +03:00
|
|
|
#for ptb_sent, dep_sent, ner_sent in zip(ptb_sents, dep_sents, ner_sents):
|
|
|
|
# doc['paragraphs'].append(format_para(None, [ptb_sent], [dep_sent], [ner_sent]))
|
2015-05-27 18:04:29 +03:00
|
|
|
else:
|
|
|
|
doc['paragraphs'] = []
|
|
|
|
for raw_sents in raw_paras:
|
2015-05-31 02:12:46 +03:00
|
|
|
para = format_para(
|
|
|
|
' '.join(raw_sents).replace('<SEP>', ''),
|
|
|
|
ptb_sents[i:i+len(raw_sents)],
|
|
|
|
dep_sents[i:i+len(raw_sents)],
|
2015-07-02 09:31:12 +03:00
|
|
|
ner_sents[i:i+len(raw_sents)],
|
|
|
|
[senses[j] for j in range(i, i+len(raw_sents))]
|
|
|
|
)
|
2015-05-31 02:12:46 +03:00
|
|
|
if para['sentences']:
|
|
|
|
doc['paragraphs'].append(para)
|
2015-05-27 18:04:29 +03:00
|
|
|
i += len(raw_sents)
|
2015-05-05 02:00:27 +03:00
|
|
|
return doc
|
|
|
|
|
|
|
|
|
2015-07-02 09:31:12 +03:00
|
|
|
def format_para(raw_text, ptb_sents, dep_sents, ner_sents, ssenses):
|
2015-05-30 02:25:00 +03:00
|
|
|
para = {'raw': raw_text, 'sentences': []}
|
2015-05-27 18:04:29 +03:00
|
|
|
offset = 0
|
2015-07-02 09:31:12 +03:00
|
|
|
|
|
|
|
assert len(ptb_sents) == len(dep_sents) == len(ner_sents) == len(ssenses)
|
|
|
|
for ptb_text, dep_text, ner_text, sense_sent in zip(ptb_sents, dep_sents, ner_sents, ssenses):
|
2015-05-30 02:25:00 +03:00
|
|
|
_, deps = read_conll.parse(dep_text, strip_bad_periods=True)
|
|
|
|
if deps and 'VERB' in [t['tag'] for t in deps]:
|
2015-05-28 23:40:01 +03:00
|
|
|
continue
|
2015-05-27 18:04:29 +03:00
|
|
|
if ner_text is not None:
|
|
|
|
_, ner = read_ner.parse(ner_text, strip_bad_periods=True)
|
|
|
|
else:
|
2015-05-30 02:25:00 +03:00
|
|
|
ner = ['-' for _ in deps]
|
2015-05-27 18:04:29 +03:00
|
|
|
_, brackets = read_ptb.parse(ptb_text, strip_bad_periods=True)
|
2015-05-30 02:25:00 +03:00
|
|
|
# Necessary because the ClearNLP converter deletes EDITED words.
|
|
|
|
if len(ner) != len(deps):
|
|
|
|
ner = ['-' for _ in deps]
|
2015-07-02 09:31:12 +03:00
|
|
|
para['sentences'].append(format_sentence(deps, ner, brackets, sense_sent))
|
2015-05-27 18:04:29 +03:00
|
|
|
return para
|
|
|
|
|
|
|
|
|
2015-07-02 09:31:12 +03:00
|
|
|
def format_sentence(deps, ner, brackets, senses):
|
2015-05-30 02:25:00 +03:00
|
|
|
sent = {'tokens': [], 'brackets': []}
|
|
|
|
for token_id, (token, token_ent) in enumerate(zip(deps, ner)):
|
2015-07-02 09:31:12 +03:00
|
|
|
sent['tokens'].append(format_token(token_id, token, token_ent, senses))
|
2015-05-30 02:25:00 +03:00
|
|
|
|
|
|
|
for label, start, end in brackets:
|
|
|
|
if start != end:
|
|
|
|
sent['brackets'].append({
|
|
|
|
'label': label,
|
|
|
|
'first': start,
|
|
|
|
'last': (end-1)})
|
|
|
|
return sent
|
|
|
|
|
|
|
|
|
2015-07-02 09:31:12 +03:00
|
|
|
def format_token(token_id, token, ner, senses):
|
2015-05-29 04:54:06 +03:00
|
|
|
assert token_id == token['id']
|
2015-05-30 02:25:00 +03:00
|
|
|
head = (token['head'] - token_id) if token['head'] != -1 else 0
|
2015-05-27 18:04:29 +03:00
|
|
|
return {
|
2015-05-30 02:25:00 +03:00
|
|
|
'id': token_id,
|
2015-05-27 18:04:29 +03:00
|
|
|
'orth': token['word'],
|
|
|
|
'tag': token['tag'],
|
|
|
|
'head': head,
|
|
|
|
'dep': token['dep'],
|
2015-07-02 09:31:12 +03:00
|
|
|
'ner': ner,
|
|
|
|
'ssenses': senses[token_id]
|
|
|
|
}
|
2015-05-27 18:04:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
def read_file(*pieces):
|
|
|
|
loc = path.join(*pieces)
|
|
|
|
if not path.exists(loc):
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return codecs.open(loc, 'r', 'utf8').read().strip()
|
|
|
|
|
|
|
|
|
|
|
|
def get_file_names(section_dir, subsection):
|
|
|
|
filenames = []
|
|
|
|
for fn in os.listdir(path.join(section_dir, subsection)):
|
|
|
|
filenames.append(fn.rsplit('.', 1)[0])
|
|
|
|
return list(sorted(set(filenames)))
|
|
|
|
|
|
|
|
|
2015-07-02 09:31:12 +03:00
|
|
|
def read_wsj_with_source(onto_dir, raw_dir, wn_ssenses):
|
2015-05-27 18:04:29 +03:00
|
|
|
# Now do WSJ, with source alignment
|
|
|
|
onto_dir = path.join(onto_dir, 'data', 'english', 'annotations', 'nw', 'wsj')
|
2015-05-27 18:35:05 +03:00
|
|
|
docs = {}
|
2015-05-05 02:00:27 +03:00
|
|
|
for i in range(25):
|
|
|
|
section = str(i) if i >= 10 else ('0' + str(i))
|
|
|
|
raw_loc = path.join(raw_dir, 'wsj%s.json' % section)
|
2015-05-23 18:39:04 +03:00
|
|
|
for j, (filename, raw_paras) in enumerate(_iter_raw_files(raw_loc)):
|
2015-05-05 02:00:27 +03:00
|
|
|
if section == '00':
|
|
|
|
j += 1
|
|
|
|
if section == '04' and filename == '55':
|
|
|
|
continue
|
2015-05-27 18:04:29 +03:00
|
|
|
ptb = read_file(onto_dir, section, '%s.parse' % filename)
|
|
|
|
dep = read_file(onto_dir, section, '%s.parse.dep' % filename)
|
|
|
|
ner = read_file(onto_dir, section, '%s.name' % filename)
|
2015-07-02 09:31:12 +03:00
|
|
|
wsd = read_senses(path.join(onto_dir, section, '%s.sense' % filename), wn_ssenses)
|
|
|
|
if ptb is not None and dep is not None: # TODO: This is bad right?
|
|
|
|
wsd = [wsd[sent_id] for sent_id in range(len(ner))]
|
|
|
|
docs[filename] = format_doc(filename, raw_paras, ptb, dep, ner, wsd)
|
2015-05-27 18:35:05 +03:00
|
|
|
return docs
|
2015-05-27 18:04:29 +03:00
|
|
|
|
2015-05-05 02:00:27 +03:00
|
|
|
|
2015-07-02 09:31:12 +03:00
|
|
|
def get_doc(onto_dir, file_path, wsj_docs, wn_ssenses):
|
2015-05-27 18:35:05 +03:00
|
|
|
filename = file_path.rsplit('/', 1)[1]
|
|
|
|
if filename in wsj_docs:
|
|
|
|
return wsj_docs[filename]
|
|
|
|
else:
|
|
|
|
ptb = read_file(onto_dir, file_path + '.parse')
|
|
|
|
dep = read_file(onto_dir, file_path + '.parse.dep')
|
|
|
|
ner = read_file(onto_dir, file_path + '.name')
|
2015-07-02 09:31:12 +03:00
|
|
|
wsd = read_senses(file_path + '.sense', wn_ssenses)
|
2015-05-27 18:35:05 +03:00
|
|
|
if ptb is not None and dep is not None:
|
2015-07-02 09:31:12 +03:00
|
|
|
return format_doc(filename, None, ptb, dep, ner, wsd)
|
2015-05-27 18:35:05 +03:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2015-05-28 23:40:01 +03:00
|
|
|
|
2015-05-27 18:35:05 +03:00
|
|
|
def read_ids(loc):
|
|
|
|
return open(loc).read().strip().split('\n')
|
|
|
|
|
2015-05-28 23:40:01 +03:00
|
|
|
|
2015-07-02 09:31:12 +03:00
|
|
|
def read_senses(loc, og_to_ssense):
|
|
|
|
senses = defaultdict(lambda: defaultdict(list))
|
|
|
|
if not path.exists(loc):
|
|
|
|
return senses
|
|
|
|
for line in open(loc):
|
|
|
|
pieces = line.split()
|
|
|
|
sent_id = int(pieces[1])
|
|
|
|
tok_id = int(pieces[2])
|
|
|
|
lemma, pos = pieces[3].split('-')
|
|
|
|
group_num = int(float(pieces[-1]))
|
|
|
|
senses[sent_id][tok_id] = list(sorted(og_to_ssense[(lemma, pos, group_num)]))
|
|
|
|
return senses
|
|
|
|
|
|
|
|
|
|
|
|
def main(wordnet_dir, onto_dir, raw_dir, out_dir):
|
|
|
|
wn_ssenses = read_wordnet.get_og_to_ssenses(wordnet_dir, onto_dir)
|
|
|
|
wsj_docs = read_wsj_with_source(onto_dir, raw_dir, wn_ssenses)
|
2015-05-27 18:35:05 +03:00
|
|
|
|
|
|
|
for partition in ('train', 'test', 'development'):
|
|
|
|
ids = read_ids(path.join(onto_dir, '%s.id' % partition))
|
2015-05-28 23:40:01 +03:00
|
|
|
docs_by_genre = defaultdict(list)
|
2015-05-27 18:35:05 +03:00
|
|
|
for file_path in ids:
|
2015-07-02 09:31:12 +03:00
|
|
|
doc = get_doc(onto_dir, file_path, wsj_docs, wn_ssenses)
|
2015-05-27 18:35:05 +03:00
|
|
|
if doc is not None:
|
2015-05-28 23:40:01 +03:00
|
|
|
genre = file_path.split('/')[3]
|
|
|
|
docs_by_genre[genre].append(doc)
|
|
|
|
part_dir = path.join(out_dir, partition)
|
|
|
|
if not path.exists(part_dir):
|
|
|
|
os.mkdir(part_dir)
|
|
|
|
for genre, docs in sorted(docs_by_genre.items()):
|
|
|
|
out_loc = path.join(part_dir, genre + '.json')
|
|
|
|
with open(out_loc, 'w') as file_:
|
|
|
|
json.dump(docs, file_, indent=4)
|
2015-05-27 18:35:05 +03:00
|
|
|
|
2015-05-05 02:00:27 +03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
plac.call(main)
|