-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrect_text.py
60 lines (51 loc) · 4.38 KB
/
correct_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import re
import os
from tqdm import tqdm # ใช้สำหรับแสดงแถบความคืบหน้าในกระบวนการประมวลผล
import thaispellcheck # ไลบรารีสำหรับตรวจสอบคำผิดในภาษาไทย
from attacut import tokenize # ไลบรารีสำหรับการตัดคำในภาษาไทย
from pdf2thaitext.check_n_shift_tone import check_tone_vowel_sentence, check_tone_vowel_word # ฟังก์ชันตรวจสอบและแก้ไขวรรณยุกต์และสระ
from pdf2thaitext.html_text_utils import replace_patterns # ฟังก์ชันแทนที่รูปแบบที่ผิดด้วยรูปแบบที่ถูก
def correct_text(txt_file):
"""
ฟังก์ชันนี้ใช้สำหรับแก้ไขข้อความในไฟล์ .txt โดยตรวจสอบและแก้ไขคำที่มีการใช้วรรณยุกต์และสระที่ผิด
และนับจำนวนคำที่ผิด
Args:
txt_file (str): เส้นทางไปยังไฟล์ข้อความที่ต้องการแก้ไข
Returns:
None: ผลลัพธ์จะถูกบันทึกลงในไฟล์ข้อความใหม่ในโฟลเดอร์ 'corrected_txt_output'
"""
count_wrong_word = 0 # ตัวนับจำนวนคำที่ผิด
all_word = 0 # ตัวนับจำนวนคำทั้งหมด
all_lines = [] # รายการเก็บบรรทัดทั้งหมดหลังจากแก้ไขแล้ว
# อ่านไฟล์ข้อความที่กำหนด
with open(txt_file, 'r', encoding='utf-8') as f:
txt = f.read()
# ประมวลผลข้อความทีละบรรทัด
for line in tqdm(txt.split("\n"), desc="Processing lines"):
fix_text_line = ""
if line.strip() != "": # ถ้าบรรทัดไม่ว่างเปล่า
new = line.replace("ำ", "า") # แทนที่ "ำ" ด้วย "า" ชั่วคราวเพื่อประมวลผล
new = replace_patterns(new) # แทนที่รูปแบบที่ผิดด้วยรูปแบบที่ถูก
new = check_tone_vowel_sentence(new) # ตรวจสอบและแก้ไขวรรณยุกต์และสระในประโยค
new_line = tokenize(new) # ตัดคำในประโยค
fix_line = []
for text in new_line:
all_word += 1
if "คำผิด" in thaispellcheck.check(text): # ตรวจสอบว่ามีคำผิดหรือไม่
text = check_tone_vowel_word(text) # แก้ไขคำที่ผิด
fix_line.append(text)
if "คำผิด" in thaispellcheck.check(text):
count_wrong_word += 1 # นับจำนวนคำที่ยังผิดหลังจากแก้ไข
else:
fix_line.append(text)
fix_text_line = "".join(fix_line)
all_lines.append(fix_text_line)
if all_word == 0:
print(f"can't read this file {txt_file}") # แสดงข้อความเมื่อไม่สามารถอ่านไฟล์ได้
else:
print(f"Count wrong word in {txt_file} : {count_wrong_word}/{all_word} % = {count_wrong_word/all_word*100}") # แสดงจำนวนคำที่ผิดและเปอร์เซ็นต์
all_text_lines = "\n".join(all_lines)
correct_txt_output = "./corrected_txt_output/"
os.makedirs(correct_txt_output, exist_ok=True) # สร้างโฟลเดอร์หากยังไม่มี
with open(f'{correct_txt_output}{txt_file.split("/")[-1]}', 'w', encoding='utf-8') as f:
f.write(all_text_lines) # บันทึกผลลัพธ์ที่แก้ไขแล้วลงในไฟล์ใหม่