Skip to content

Commit

Permalink
Merge pull request #1 from OpenPecha/feat-prepare_data
Browse files Browse the repository at this point in the history
Feat prepare data
  • Loading branch information
tenzin3 authored Oct 21, 2024
2 parents 9267593 + ddd271b commit ffb6cab
Show file tree
Hide file tree
Showing 17 changed files with 264 additions and 18 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Set up Python 3.8
uses: actions/setup-python@v3
with:
python-version: "3.8"

- name: Install dependencies
run: |
pip install -U pip
pip install .
pip install .[dev]
- name: Test with pytest
run: PYTHONPATH=src pytest

- name: Test Coverage
run: PYTHONPATH=src pytest --cov project_name
run: PYTHONPATH=src pytest --cov bo_rag_prep_tool
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
output/
data/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@

<!-- Replace with 1-sentence description about what this tool is or does.-->

<h3 align="center">Use this repo template for all new Python projects.</h3>
<h3 align="center">BO RAG PREP TOOL</h3>

## Description

Project description goes here.
Tool to prepare tibetan data for RAG.

## Project owner(s)

<!-- Link to the repo owners' github profiles -->

- [@10zinten](https://github.com/10zinten)
- [@evanyerburgh](https://github.com/evanyerburgh)
- [@tenzin3](https://github.com/tenzin3)

## Integrations

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "project_name"
name = "bo_rag_prep_tool"
version = "0.0.1"
authors = [
{ name="OpenPecha", email="[email protected]" },
]
description = "A small example package"
description = "Tool to prepare data for rag prep tool"
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
Expand Down
File renamed without changes.
41 changes: 41 additions & 0 deletions src/bo_rag_prep_tool/prepare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from pathlib import Path

import yaml

from bo_rag_prep_tool.utils import write_json


def prepare(opf_path: Path, output_path: Path):
book_metadata = []
ann_files = list(Path(opf_path / "layers").rglob("*.yml"))
ann_files = sorted(ann_files, key=lambda x: x.name)

pecha_id = opf_path.name.split(".")[0]
for ann_file in ann_files:
with open(ann_file) as f:
layer = yaml.load(f, Loader=yaml.FullLoader)
# Get base
base_path = opf_path / "base" / f"{ann_file.parent.name}.txt"
base_content = base_path.read_text(encoding="utf-8")
# Get annotations
anns = layer["annotations"]
for ann in anns.values():
start = ann["span"]["start"]
end = ann["span"]["end"]
base_span = base_content[start:end]
curr_book_metadata = {
"pecha_id": pecha_id,
"base": ann_file.parent.name,
"start": start,
"end": end,
"content": base_span,
}
for k, v in ann.items():
if k != "span":
curr_book_metadata[k] = v
book_metadata.append(curr_book_metadata)

output_path.mkdir(exist_ok=True, parents=True)
json_output_path = output_path / f"{pecha_id}.json"
write_json(json_output_path, book_metadata)
return json_output_path
12 changes: 12 additions & 0 deletions src/bo_rag_prep_tool/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import json


def read_json(file_path):
with open(file_path) as f:
data = json.load(f)
return data


def write_json(file_path, data):
with open(file_path, "w") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
2 changes: 0 additions & 2 deletions src/project_name/example.py

This file was deleted.

14 changes: 14 additions & 0 deletions tests/prepare/data/I46409446.opf/base/I1PD108815.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
༄༅། །ཨོ་རྒྱན་རྩ་གསུམ་གླིང་པའི་རིང་ལུགས་ཟབ་གཏེར་པོད་ཉེར་བདུན་པ་གྲོལ་བྱེད་ཁྲིད་ཀྱི་
རིམ་པ་ཕྱོགས་གཅིག་ཏུ་བསྡུས་པ་ཐེག་མཆོག་རྩེ་མོར་འཇུག་པའི་ཐེམ་སྐས་ཞེས་བྱ་བ་བཞུགས་སོ།།

༄༅། །ཨོ་རྒྱན་རྩ་གསུམ་གླིང་པའི་རིང་ལུགས་གྲོལ་བྱེད་ཁྲིད་ཀྱི་རིམ་པ་ཕྱོགས་གཅིག་ཏུ་བསྡུས་པ་ཐེག་མཆོག་རྩེ་མོར་འཇུག་པའི་ཐེམ་སྐས་ཞེས་བྱ་བ་བཞུགས་སོ།། ཨོཾ་སྭ་སྟི། གཏེར་ཆེན་རྩ་
གསུམ་གླིང་པའི་ཟབ་མོའི་གཏེར། །བདུན་ཅུའི་ཡང་སྙིང་གྲོལ་བྱེད་ཁྲིད་རིམ་ཕལ། །དུས་རླུང་དྲག་པོས་ཁྱེར་བའི་འཐོར་ལུས་རིགས། །འབད་བརྒྱས་ཕྱོགས་བསྡུས་གང་ནུས་སྲི་
ཞུ་བཅས། །ཐེག་རྩེར་འཇུག་པའི་ཐེམ་སྐས་འདིར་སློང་བྱ། །དེ་ལ་ཨོ་རྒྱན་རྩ་གསུམ་གླིང་པའི་ཟབ་གཏེར་རྗེས་འབྲངས་དང་བཅས་པའི་ཕྱོགས་གཏོགས་གྲོལ་བྱེད་ཁྲིད་ཀྱི་རིམ་པ་
ཟབ་པ་གཏེར་བྱོན་སྐོར། བཀའ་བརྒྱད་གསང་བ་ཟིལ་གནོན་ལས། ཕྱི་རྒྱུད་ཐེག་པ་རིམ་དགུ་དང་། རྫོགས་ཆེན་ཡང་གསང་བླ་མེད། དེའི་དོན་ཁྲིད་དཀོན་སེང་པཎྜི་ཏས་མཛད་པ་
དང་༑ རྒ་རྒྱལ་གཏེར་བྱོན་རྫོགས་ཆེན་འཁོར་འདས་རང་གྲོལ་ལས་ཚོགས་ཆ་ལག་དང་བཅས་པ། གསང་བ་ཡེ་ཤེས་རྩ་རླུང་སྙན་བརྒྱུད་གསང་མཛོད་སོགས་ཆོས་སྡེ་ཕལ་ཆེ་བར་

རྒྱབ་ཆོས་ཁྲིད་གཞུང་དང་། སྤྲ་ཁྲིད་མན་ངག་ཟབ་ཁྱད་མང་དུ་བཞུགས་པ་ཕལ་ཆེར་གངས་ལྗོངས་སྤྱི་མཐུན་བཞིན་དུས་འགྱུར་ཆུ་ལོག་དྲག་པོས་ཁྱེར་ནས་མི་མངོན་གདོད་མའི་
དབྱིངས་སུ་གཤེགས་ཟིན་པར་མངོན་ལ། ཡང་དར་ཆོས་རིགས་སླར་གསོའི་སྐབས། དཔེ་རྒྱན་གང་རྙེད་ཕྱོགས་བསྡུས་བྱས་པའི་ནང༌། པདྨ་ཁྲག་འཐུང་གི་ཁྲིད་སྐོར་སོགས་
འཐོར་བུ་འགའ་རྙེད་པ་དང་། དེ་དག་ལ་ཁོ་བོས་སྲི་ཞུར་དམིགས་པའི་དགོངས་འགྲེལ་དང་ཟིན་བྲིས་ཅི་རིགས་པ་བཅས་ཁྲིད་ཡིག་ཕྱོགས་བསྡུས་ཀྱི་ཐེམ་བྱང་འགོད་པ་ལ། ཐོག་མ
མར་ཀ༽ ལོ་གསུམ་བསྙེན་སྒྲུབ་མཚམས་ཀྱི་ཁོག་དབུབ་མཁྱེན་བརྩེའི་དགོངས་རྒྱན་སྒེག་མོའི་མེ་ལོང་། 1-32 ཁ༽ ཞི་གནས་དང་ལྷག་མཐོང་སྒབ་པའི་ཚུལ་འཇམ་མགོན་
བླ་མའི་ཞལ་གྱི་བདུད་རྩིའི་ཉིང་ཁུ། 33-70 ག༽ ཐུན་མོང་ཕྱི་ཡི་སྔོན་འགྲོའི་ལག་ཁྲིད་གདབ་པ་མན་ངག་གནད་ཀྱི་གཟེར་བུ། 71-126 ང༽ རྫོགས་པ་ཆེན་པོ་འཁོར་
8 changes: 8 additions & 0 deletions tests/prepare/data/I46409446.opf/base/I1PD108816.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
༄༅། །ཨོ་རྒྱན་རྩ་གསུམ་གླིང་པའི་རིང་ལུགས་ཟབ་གཏེར་པོད་ཉེར་བརྒྱད་པ་རྗེ་གོང་མའི་གཏེར་མཐའ་སྐྱོང་བ་པད་རྒྱལ་
གླིང་པའི་ཟབ་གཏེར་ལས། ཆ་ཤས་ཁ་སྐོང་གི་ཚུལ་དུ་བཀོད་པ་དང་གནམ་ལྕགས་ཐོར་བུ་ཐེམ་སྐས་བཅས་བཞུགས་སོ།།

༄༅། །ཨོ་རྒྱན་རྩ་གསུམ་གླིང་པའི་གཏེར་གྱི་མཐའ་སྐྱོང་བ་སྤྲུལ་པའི་གཏེར་ཆེན་པདྨ་རྒྱལ་པོ་གར་དབང་འགྲོ་འདུལ་གླིང་པ་རྩལ་གྱི་ཟབ་གཏེར་ལས། ཆ་ཤས་ཁ་སྐོང་གི་ཚུལ་དུ་བཞུགས་པའི་ཐེམ་བྱང་བཞུགས་སོ།
། ཨོཾ་སྭཱ་སྟི། སྡེ་གསུམ་ཡོངས་ཀྱི་སྲོལ་འབྱེད་གནུབས་ཆེན་ཉིད། །དེ་ཡང་གཙུག་ལག་དཔལ་དགེ་ཞང་ཁྲོམ་དང་། །དྲི་མེད་ཀུན་དགའ་རྩ་གསུམ་གླིང་པ་སོགས། །སྲོལ་
འབྱེད་གཏེར་ཆེན་དུ་མའི་རོལ་གར་བསྒྱུར། །ཕྱི་མོ་གར་དབང་གནམ་ལྕགས་རྡོ་རྗེ་རྩལ། །དེང་དུས་གཏེར་ཆེན་པད་རྒྱལ་གླིང་པར་ཤར། །དེ་ལྟར་གོང་འོག་ཟབ་གཏེར་འཕྲོ་མཐུད་
ཕྱིར། །གོང་གི་བཀའ་བཞིན་ཆ་ཤས་འདིར་བཞུགས་གསོལ། །དེ་ལ་སྤྲུལ་པའི་གཏེར་ཆེན་རྩ་གསུམ་གླིང་པའི་ཟབ་གཏེར་མཐའ་སྐྱོང་བ་གཏེར་ཆེན་པདྨ་རྒྱལ་པོ་གར་དབང་འགྲོ་
འདུལ་རྩལ་འདི་ཉིད་ཡིན་ལ། དེས་ན་གཏེར་ཁ་གོང་མ་གནམ་ལྕགས་དང༌། ཕྱི་མ་སྐུ་གསུམ་དགོངས་འདུས་འདི་ཉིད་ཡིན་ཕྱིར་གོང་འོག་འཕྲོ་མི་ཆད་པའི་ཆེད་དུ་གནམ་ལྕགས་
22 changes: 22 additions & 0 deletions tests/prepare/data/I46409446.opf/layers/I1PD108815/Pagination.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
id: 6cc17bb2bb8a41a586d506ead87d7ce7
annotation_type: Pagination
revision: '00001'
annotations:
5cc9b1e74a7441e89d33100c956422a2:
span:
start: 0
end: 163
imgnum: 3
reference: I1PD1088150003.tif
e6a42a5e463540d4876be4263ca656de:
span:
start: 165
end: 905
imgnum: 4
reference: I1PD1088150004.tif
62e8c55cc3d74085a13f8dd165590093:
span:
start: 907
end: 1602
imgnum: 5
reference: I1PD1088150005.tif
16 changes: 16 additions & 0 deletions tests/prepare/data/I46409446.opf/layers/I1PD108816/Pagination.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
id: 9d07b7acefe543029e062e3098395bce
annotation_type: Pagination
revision: '00001'
annotations:
a870d803d8264254b8af5b8dc4c7bf53:
span:
start: 0
end: 192
imgnum: 3
reference: I1PD1088160003.tif
d21a7d7373294f1ea75b09996df80d8e:
span:
start: 194
end: 937
imgnum: 4
reference: I1PD1088160004.tif
68 changes: 68 additions & 0 deletions tests/prepare/data/I46409446.opf/meta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
id: I46409446
ocr_import_info:
bdrc_scan_id: W4CZ1042
source: bdrc
ocr_info:
timestamp: '2023-02-01T00:00:00Z'
batch_id: batch-0001
software_id: norbuketaka
expected_default_language: bo
op_import_options: null
default_language: bo
source: https://library.bdrc.io
initial_creation_type: ocr
imported: 2024-09-12 14:18:06.326672
last_modified: 2023-02-01 00:00:00+00:00
parser: https://github.com/OpenPecha/Norbu-Keta-eText-import/blob/main/norbu_ketaka_parser.py
source_metadata:
id: http://purl.bdrc.io/resource/W4CZ1042
status: http://purl.bdrc.io/admindata/StatusReleased
access: http://purl.bdrc.io/admindata/AccessOpen
geo_restriction:
- CN
outline: http://purl.bdrc.io/resource/O8LS1837
reproduction_of: http://purl.bdrc.io/resource/MW4CZ1042
copyright_status: http://purl.bdrc.io/resource/CopyrightPublicDomain
title: གཏེར་ཆོས། རྩ་གསུམ་གླིང་པ།
languages:
- bo
author: གནམ་ལྕགས་རྩ་གསུམ་གཏེར་བདག་གླིང་པ།
bases:
I1PD108815:
source_metadata:
id: http://purl.bdrc.io/resource/I1PD108815
total_pages: 687
volume_number: 27
volume_pages_bdrc_intro: 2
base_file: I1PD108815.txt
order: 1
I1PD108816:
source_metadata:
id: http://purl.bdrc.io/resource/I1PD108816
total_pages: 665
volume_number: 28
volume_pages_bdrc_intro: 2
base_file: I1PD108816.txt
order: 2
I1PD108817:
source_metadata:
id: http://purl.bdrc.io/resource/I1PD108817
total_pages: 324
volume_number: 29
volume_pages_bdrc_intro: 2
base_file: I1PD108817.txt
order: 3
I1PD108818:
source_metadata:
id: http://purl.bdrc.io/resource/I1PD108818
total_pages: 764
volume_number: 30
volume_pages_bdrc_intro: 2
base_file: I1PD108818.txt
order: 4
copyright:
status: Public domain
notice: The proofread texts are donated by the Norbu Ketaka project led by Queenie
Luo (Harvard University), Zhiying Li (Sichuan University) and Leonard van der
Kuijp (Harvard University). Correspondence should be directed to [email protected]
license: CC0
Loading

0 comments on commit ffb6cab

Please sign in to comment.