dhammawatthumpra commited on
Commit
37aecec
·
1 Parent(s): 7392299

wiki: add ai-popup-and-rag-status concept, update log/index/overview for 2026-05-01 changes

Browse files
Files changed (2) hide show
  1. analyze_headings.py +60 -0
  2. webapp/_inspect_db.py +39 -0
analyze_headings.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tipitaka_query import TipitakaDB, clean_text
2
+ import re
3
+ from collections import Counter
4
+
5
+ def analyze_subheadings():
6
+ print("กำลังสแกนวิเคราะห์เนื้อหาจากทั้ง 45 เล่ม... (อาจใช้เวลา 1-2 นาที)")
7
+ heading_counter = Counter()
8
+ last_word_counter = Counter()
9
+
10
+ # Pattern ดักจับเลขข้อ (เหมือนใน build_rag)
11
+ ref_re = re.compile(r'\[[\u0E50-\u0E590-9]+\]')
12
+
13
+ # ใช้ TipitakaDB ดึงข้อมูลแบบเดียวกับระบบ RAG ของจริง
14
+ with TipitakaDB("tipitaka_mcu.db") as db:
15
+ for vol in range(1, 46):
16
+ toc = db.get_toc(vol)
17
+ if not toc: continue
18
+
19
+ vol_info = db.get_volume_info(vol)
20
+ max_page = vol_info["total_pages"]
21
+
22
+ for i, section in enumerate(toc):
23
+ start_page = section.page_number
24
+ end_page = toc[i + 1].page_number - 1 if i + 1 < len(toc) else max_page
25
+
26
+ try:
27
+ # ดึงข้อความมา Clean เพื่อขจัด Noise แบบเดียวกับ Data Pipeline
28
+ raw = db.get_section_text(vol, start_page, end_page, for_tts=False)
29
+ text = clean_text(raw, remove_line_numbers=True, remove_footnotes=True)
30
+ except Exception:
31
+ continue
32
+
33
+ if not text: continue
34
+
35
+ for line in text.split('\n'):
36
+ l = line.strip()
37
+
38
+ # กรอง: ความยาว 2-40 และใช้ Regex เช็คอักษรไทย ป้องกันอักขระล่องหน
39
+ if 2 <= len(l) <= 40 and re.search(r'[ก-๙]', l):
40
+ # ต้องไม่มีเลขข้อในบรรทัด และไม่ลงท้ายด้วยเครื่องหมายจบประโยค
41
+ if not ref_re.search(l) and not l.endswith(('”', '"', '.', '!', '?', 'ๆ', 'ฯ')):
42
+ heading_counter[l] += 1
43
+
44
+ # หาคำลงท้าย (Suffix) ที่เป็นภาษาไทยล้วน ตัดตัวเลขทิ้ง (เช่น "วิชชา ๓" -> "วิชชา")
45
+ thai_words = [re.sub(r'[^ก-๙]', '', w) for w in l.split()]
46
+ thai_words = [w for w in thai_words if len(w) >= 2]
47
+
48
+ if thai_words:
49
+ last_word_counter[thai_words[-1]] += 1
50
+
51
+ print("\n=== Top 50 ประโยคหัวข้อที่พบบ่อย ===")
52
+ for h, c in heading_counter.most_common(50):
53
+ print(f"{c:5d} ครั้ง : {h}")
54
+
55
+ print("\n=== Top 50 คำลงท้าย (Suffix) ที่พบบ่อย ===")
56
+ for w, c in last_word_counter.most_common(50):
57
+ print(f"{c:5d} ครั้ง : {w}")
58
+
59
+ if __name__ == "__main__":
60
+ analyze_subheadings()
webapp/_inspect_db.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ conn = sqlite3.connect(r"f:\_Ai\Tipitaka-AI-Expert\RAG\tipitaka_mcu.db")
4
+ cursor = conn.cursor()
5
+
6
+ # Volumes
7
+ print("=== VOLUMES ===")
8
+ cursor.execute("SELECT * FROM volumes ORDER BY volume_number LIMIT 5")
9
+ for row in cursor.fetchall():
10
+ print(f" id={row[0]}, vol={row[1]}, title={row[2]}, pitaka={row[3]}, pages={row[4]}")
11
+
12
+ cursor.execute("SELECT COUNT(*) FROM volumes")
13
+ print(f"\nTotal volumes: {cursor.fetchone()[0]}")
14
+
15
+ # Pages sample
16
+ print("\n=== PAGES (sample vol_id=1) ===")
17
+ cursor.execute("SELECT id, volume_id, page_number, LENGTH(content_text), LENGTH(content_html) FROM pages WHERE volume_id=1 LIMIT 3")
18
+ for row in cursor.fetchall():
19
+ print(f" id={row[0]}, vol_id={row[1]}, page={row[2]}, text_len={row[3]}, html_len={row[4]}")
20
+
21
+ cursor.execute("SELECT COUNT(*) FROM pages")
22
+ print(f"Total pages: {cursor.fetchone()[0]}")
23
+
24
+ # Contents (TOC)
25
+ print("\n=== CONTENTS/TOC (sample vol_id=1) ===")
26
+ cursor.execute("SELECT * FROM contents WHERE volume_id=1 ORDER BY page_number LIMIT 5")
27
+ for row in cursor.fetchall():
28
+ print(f" id={row[0]}, vol_id={row[1]}, title={row[2][:60]}, page={row[3]}, level={row[4]}")
29
+
30
+ cursor.execute("SELECT COUNT(*) FROM contents")
31
+ print(f"Total TOC entries: {cursor.fetchone()[0]}")
32
+
33
+ # Pitaka distribution
34
+ print("\n=== PITAKA DISTRIBUTION ===")
35
+ cursor.execute("SELECT pitaka, COUNT(*), SUM(total_pages) FROM volumes GROUP BY pitaka")
36
+ for row in cursor.fetchall():
37
+ print(f" {row[0]}: {row[1]} volumes, {row[2]} pages")
38
+
39
+ conn.close()