fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
  15.  
Success #stdin #stdout 0.09s 54564KB
stdin
import cv2
import pytesseract
import random
import re
from janome.tokenizer import Tokenizer

# ----------------------------
# OCRで画像から文字抽出
# ----------------------------
def ocr_image(image_path):
    img = cv2.imread(image_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    text = pytesseract.image_to_string(gray, lang='jpn')
    return text

# ----------------------------
# 文を分割
# ----------------------------
def split_sentences(text):
    sentences = re.split('[。!?\n]', text)
    sentences = [s.strip() for s in sentences if s.strip()]
    return sentences

# ----------------------------
# 漢字を含む文を抽出
# ----------------------------
def get_sentences_with_kanji(sentences):
    return [s for s in sentences if re.search(r'[一-龥]', s)]

# ----------------------------
# ひらがな変換
# ----------------------------
def to_hiragana(text):
    result = ""
    for char in text:
        # カタカナ→ひらがな
        if 'ァ' <= char <= 'ン':
            result += chr(ord(char) - 0x60)
        else:
            result += char
    return result

# ----------------------------
# 漢字抽出
# ----------------------------
def extract_kanji(text):
    return "".join(re.findall(r'[一-龥]', text))

# ----------------------------
# クイズ作成
# ----------------------------
def create_quiz(sentences, num_questions=10):
    selected = random.sample(sentences, min(num_questions, len(sentences)))

    for i, sentence in enumerate(selected, 1):
        hiragana_sentence = to_hiragana(sentence)
        kanji_answer = extract_kanji(sentence)

        print(f"\n【第{i}問】")
        print("つぎの ぶんを よみましょう。")
        print(hiragana_sentence)
        print("\n★ もとの かんじは なんでしたか?")
        print("(答え) :", kanji_answer)
        print("-" * 40)

# ----------------------------
# メイン
# ----------------------------
if __name__ == "__main__":
    image_path = "kyokasho.jpg"  # 撮影した写真

    print("画像を読み込み中...")
    text = ocr_image(image_path)

    sentences = split_sentences(text)
    kanji_sentences = get_sentences_with_kanji(sentences)

    if len(kanji_sentences) == 0:
        print("漢字を含む文が見つかりませんでした。")
    else:
        create_quiz(kanji_sentences, 10)
stdout
Standard output is empty