入試問題構造情報ライブラリ - WordファイルとJSON構造データを統合処理
Project description
qc-structure
qc-structure は、Word の段落と structure.json を結びつけて、品質管理コードを書きやすくするための Python ライブラリです。
このライブラリ自身は「連番チェック」や「太字チェック」を実装しません。代わりに、異常判定に必要な情報を段落オブジェクトへ付与します。
想定用途
- 異常がある段落の
indexを返す roleやlabel_textを使って番号・体裁チェックを書くancestorsやstructure_by_idを使って親子関係を辿る- 必要に応じて
para_idを使って Word XML にコメントを付ける
インストール
pip install qc-structure
または:
pip install git+https://github.com/AI-Part-A/auto_structuring.git#subdirectory=qc_structure
基本方針
qc_structure では、3 種類の ID を役割分担して扱います。
index: Word 内の段落順を表す番号。異常出力の主キーpid:structure.json側の論理段落 IDpara_id: Word XML (w14:paraId) の内部段落 ID
通常の品質管理コードでは index を主に使い、Word へ直接コメントを打ちたいときだけ para_id を使います。
最小利用例
from qc_structure import load_exam_document
doc = load_exam_document("exam.docx", "structure.json")
findings = []
for p in doc.paragraphs:
if p.role == "小問" and p.label_text is None:
findings.append(
{
"index": p.index,
"pid": p.pid,
"para_id": p.para_id,
"reason": "小問ラベルがありません",
}
)
print(findings)
使い方
from qc_structure import load_exam_document
doc = load_exam_document("exam.docx", "structure.json")
for p in doc.paragraphs:
print(
p.index,
p.pid,
p.role,
p.label_text,
[node.id for node in p.ancestors],
p.text[:40],
)
親子関係を直接見たい場合:
node = doc.structure_by_id["q1-1"]
print(node.id)
print(node.parent.id if node.parent else None)
print([child.id for child in node.children])
print(node.paragraph_indexes)
主な API
load_exam_document(docx_path, structure_json_path) -> ExamDocument
.docx と structure.json を読み込みます。
ExamDocument
paragraphs:list[EnhancedParagraph]structure_by_id:dict[str, StructureNode]root_nodes: ルート構造ノード一覧structure_nodes: 全構造ノード一覧
EnhancedParagraph
index: 段落順の番号pid: 論理段落 IDpara_id: Word 内部段落 IDrole: 構造上の役割id: 構造 IDparent_id: 親構造 IDlabel_text: 見出しラベルstructure: 生の構造 dictnode: 対応するStructureNodeparent: 親ノードchildren: 子ノード一覧ancestors: 祖先ノード一覧structure_path: root -> self のノード列text,runs,style:python-docxの段落 API
StructureNode
idparentchildrenrolelabel_textstart_pidend_pidparagraphsparagraph_indexesancestorsstructure
structure.json の前提
structure.json には少なくとも次が必要です。
questionsparagraph_mapping
paragraph_mapping の最低要件:
{
"p0001": {
"index": 0,
"para_id": "00000001"
}
}
indexは必須para_idは任意
questions はツリー形式に対応します。
{
"questions": [
{
"id": "q1",
"role": "大問",
"start_pid": "p0001",
"end_pid": "p0037",
"label_text": "I",
"children": [
{
"id": "q1-1",
"role": "小問",
"parent_id": "q1",
"start_pid": "p0002",
"end_pid": "p0004",
"label_text": "(1)",
"children": []
}
]
}
],
"paragraph_mapping": {
"p0001": {"index": 0, "para_id": "00000001"},
"p0002": {"index": 1, "para_id": "00000002"}
}
}
マッピング仕様
- 構造ノードは
start_pidからend_pidの範囲で段落に対応します - 段落解決は
indexを優先します indexが使えない場合のみpara_idにフォールバックします- 親子範囲が重なるときは、より深い子ノードが親ノードを上書きします
ライセンス
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qc_structure-0.2.0.tar.gz.
File metadata
- Download URL: qc_structure-0.2.0.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da0a9337035d64e40778063957f26ac515dc23836ea2bf1e91765ced7103a40f
|
|
| MD5 |
8c7a0c67afb47cad3d19ecb9d51bbe10
|
|
| BLAKE2b-256 |
c0caaa356f26d8f94fcbbe5b843b61788d5c8a935452a98907666d9343e1e989
|
File details
Details for the file qc_structure-0.2.0-py3-none-any.whl.
File metadata
- Download URL: qc_structure-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f8f97bd5b7718e6b3adbd2fff9e5a67f61c7669b4b054046f29c83ee98abb3e
|
|
| MD5 |
1b4bf22f3d0d1c8e3e96a48e4b67e59f
|
|
| BLAKE2b-256 |
cd0a8058824370881981b7ef944f4d516719af29eaff1a2e45a9ecec890e1bde
|