зеркало из https://github.com/microsoft/genalog.git
182 строки
4.0 KiB
Plaintext
182 строки
4.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## `genalog.text` module: \n",
|
|
"This module is responsible for:\n",
|
|
"1. Text alignment\n",
|
|
"1. NER label propagation using text alignment results"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from genalog.text import ner_label\n",
|
|
"from genalog.text import preprocess\n",
|
|
"\n",
|
|
"gt_txt = \"New York is big\"\n",
|
|
"ocr_txt = \"New Yo rkis big\"\n",
|
|
"\n",
|
|
"# Input to the method\n",
|
|
"gt_labels = [\"B-P\", \"I-P\", \"O\", \"O\"]\n",
|
|
"gt_tokens = preprocess.tokenize(gt_txt) # tokenize into list of tokens\n",
|
|
"ocr_tokens = preprocess.tokenize(ocr_txt)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"['B-P', 'I-P', 'O', 'O']\n",
|
|
"['New', 'York', 'is', 'big']\n",
|
|
"['New', 'Yo', 'rkis', 'big']\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Inputs to the method\n",
|
|
"print(gt_labels)\n",
|
|
"print(gt_tokens)\n",
|
|
"print(ocr_tokens)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Method returns a tuple of 4 elements (gt_tokens, gt_labels, ocr_tokens, ocr_labels, gap_char)\n",
|
|
"ocr_labels, aligned_gt, aligned_ocr, gap_char = ner_label.propagate_label_to_ocr(gt_labels, gt_tokens, ocr_tokens)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"OCR labels: ['B-P', 'I-P', 'I-P', 'O']\n",
|
|
"Aligned ground truth: New Yo@rk is big\n",
|
|
"Alinged OCR text: New Yo rk@is big\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Outputs\n",
|
|
"print(f\"OCR labels: {ocr_labels}\")\n",
|
|
"print(f\"Aligned ground truth: {aligned_gt}\")\n",
|
|
"print(f\"Alinged OCR text: {aligned_ocr}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"B-P I-P O O \n",
|
|
"New York is big \n",
|
|
"New Yo@rk is big\n",
|
|
"||||||.||.||||||\n",
|
|
"New Yo rk@is big\n",
|
|
"New Yo rkis big \n",
|
|
"B-P I-P I-P O \n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Format result for display\n",
|
|
"print(ner_label.format_label_propagation(gt_tokens, gt_labels, ocr_tokens, ocr_labels, aligned_gt, aligned_ocr))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"B-P I-P O O \n",
|
|
"New York is big \n",
|
|
"New Yo rkis big \n",
|
|
"B-P I-P I-P O \n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# To turn off alignment information:\n",
|
|
"print(ner_label.format_label_propagation(gt_tokens, gt_labels, ocr_tokens, ocr_labels, aligned_gt, aligned_ocr, show_alignment=False))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"B-P I-P I-P O \n",
|
|
"New Yo rkis big \n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Format tokens and labels\n",
|
|
"print(ner_label.format_labels(ocr_tokens, ocr_labels))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.6.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|