|
import os |
|
import re |
|
|
|
|
|
def parse_annotated_text(text): |
|
|
|
yedda_pattern = re.compile(r'(\[\@(.*?)\#(\w+)\*\])', re.DOTALL) |
|
|
|
|
|
chars_removed = 0 |
|
|
|
|
|
spans_in_original_text = [] |
|
|
|
|
|
buffer = [] |
|
|
|
|
|
last_end = 0 |
|
|
|
|
|
labels = [] |
|
|
|
|
|
for match in yedda_pattern.finditer(text): |
|
|
|
full_match = match.group(0) |
|
|
|
entity = match.group(2) |
|
|
|
label = match.group(3) |
|
|
|
start = match.start() |
|
|
|
end = match.end() |
|
|
|
labels.append(label) |
|
|
|
|
|
buffer.append(text[last_end:start]) |
|
|
|
|
|
entity = entity.rstrip() |
|
buffer.append(entity) |
|
|
|
|
|
original_start = start - chars_removed |
|
original_end = original_start + len(entity) |
|
|
|
assert original_end > original_start, text |
|
|
|
|
|
spans_in_original_text.append((original_start, original_end)) |
|
|
|
|
|
chars_removed += len(full_match) - len(entity) |
|
|
|
|
|
last_end = end |
|
|
|
|
|
buffer.append(text[last_end:]) |
|
|
|
|
|
content_without_annotations = "".join(buffer) |
|
|
|
return { |
|
'text': content_without_annotations, |
|
'spans': spans_in_original_text, |
|
'labels': labels |
|
} |
|
|
|
|
|
def preprocess_text(text: str): |
|
|
|
text = text.strip() |
|
text = re.sub(r'\n+', '\n', text) |
|
text = re.sub(r' +', ' ', text) |
|
text = text.replace(' \n', '\n') |
|
text = text.replace('\n ', '\n') |
|
return text |
|
|
|
|
|
def load_yedda_annotations(directory): |
|
|
|
|
|
all_annotations = [] |
|
|
|
|
|
for filename in os.listdir(directory): |
|
|
|
if filename.endswith(".ann"): |
|
|
|
file_path = os.path.join(directory, filename) |
|
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as file: |
|
content = file.read() |
|
|
|
|
|
content = preprocess_text(content) |
|
|
|
parsed = parse_annotated_text(content) |
|
file_annotations = { |
|
'file': filename, |
|
'annotated_text': content, |
|
'text': parsed['text'], |
|
'spans': parsed['spans'], |
|
'labels': parsed['labels'], |
|
} |
|
all_annotations.append(file_annotations) |
|
|
|
return all_annotations |
|
|
|
|
|
def convert_to_ann(annotatations): |
|
text = annotatations['text'] |
|
buffer = [] |
|
i = 0 |
|
for (j_start, j_end), label in zip(annotatations['spans'], annotatations['labels']): |
|
|
|
buffer += text[i:j_start] |
|
buffer += [f'[@{text[j_start:j_end]}#{label}*]'] |
|
i = j_end |
|
|
|
buffer += [text[i:]] |
|
|
|
return ''.join(buffer) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
directory_path = 'annotations' |
|
annotations = load_yedda_annotations(directory_path) |
|
|
|
counter = 0 |
|
for file_annotation in annotations: |
|
counter += len(file_annotation['labels']) |
|
print('File:', file_annotation['file']) |
|
print('Text[:100]:', repr(file_annotation['text'][:100])) |
|
print('Number of labels:', len(file_annotation['labels'])) |
|
assert len(file_annotation['labels']) == len(file_annotation['spans']) |
|
print('Average labeled sentence length:', sum(end-start for start,end in file_annotation['spans']) / len(file_annotation['spans'])) |
|
print('--------------------------------') |
|
|
|
print('Total number of files:', len(annotations)) |
|
print('Total label count:', counter) |
|
|
|
|