File size: 2,923 Bytes
5b07cee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
import pandas as pd
import tempfile
import os
import re
from chatgpt_api import get_chatgpt_response2
from voice_create import text_to_speech
from select_question import create_choice_question
from manuscript_conversion import manuscript_conversion

def check_text(text, radio_option):
    errors = []
    error_details = []

    # Split the text into sentences for individual checks
    # This regex splits on punctuation but keeps the punctuation with the previous sentence
    sentences = re.split(r'(?<=[。!??!.])\s*|\n', text)
    
    for sentence in sentences:
        sentence = sentence.strip()
        if not sentence:
            continue

        if radio_option == "日本語":
            if not re.search(r'[。!]$', sentence):
                errors.append("文末に句点がありません。")
                error_details.append(f"文末に「。」または「!」がない: '{sentence}'")
        else:
            # Check for multiple spaces
            if re.search(r'  +', sentence):
                errors.append("半角スペースが2つ以上入っています。")
                multiple_spaces_parts = re.findall(r'[^ ]*  +[^ ]*', sentence)
                for part in multiple_spaces_parts:
                    error_details.append(f"スペースが2つある部分: '{part.strip()}'")
            
            # Check for punctuation at the end of the sentence
            if not re.search(r'[.!?]$', sentence):
                errors.append("文末にピリオドや?や!のいずれかがついていません。")
                error_details.append(f"文末に「.」または「!」または「?」がない: '{sentence}'")
            
    if errors:
        return "チェック観点:\n" + "\n".join(errors) + "\n\n詳細:\n" + "\n".join(error_details)
    else:
        return "全てのチェックをクリアしました。"





def kousei2(csv_file, input_text,radio_option):
    prompt_text = "#Instructions\n" + input_text +" If there is no problem, please reply with only 2 letters 'OK' and DON'T put any other extra words. \n #Target sentence\n" 
    # CSVファイルを読み込む
    df = pd.read_csv(csv_file)
    # 'id'列のデータ型を文字列に変換
    df['id'] = df['id'].astype(str)

    df["prompt"] = prompt_text + df["原稿"] 

    df["GPT校正結果"] = df["prompt"].apply(get_chatgpt_response2)
    print("radio_option",radio_option)
    df["タイプミス校正結果"] = df["原稿"].apply(check_text, args=(radio_option,))
    
    
    
    # ファイル出力
    with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
        df.to_csv(tmp.name, index=False, encoding='cp932', errors='ignore')
        output_path = tmp.name
    
    # ファイル名を変更
    new_path = os.path.join(os.path.dirname(output_path), "output.csv")
    os.rename(output_path, new_path)
    return new_path