1111import neologdn
1212from bs4 import BeautifulSoup
1313from fastapi import APIRouter , Depends
14- from httpx import (
15- ConnectError ,
16- HTTPStatusError ,
17- ReadTimeout ,
18- TooManyRedirects ,
19- )
2014from pydantic import BaseModel , Field
2115
2216from api .dependencies import get_http_client
2317from api .furigana_marker import Request , SingleWordResultObject , mark_furigana
2418
25- logging .basicConfig (level = logging .INFO , format = '%(asctime)s - %(levelname)s - %(message)s' )
19+ logging .basicConfig (
20+ level = logging .INFO , format = '%(asctime)s - %(levelname)s - %(message)s'
21+ )
2622logger = logging .getLogger (__name__ )
2723
2824tags_metadata = [
@@ -277,7 +273,7 @@ async def mark_accent(
277273 logger .warning (f"Yahoo Response Empty or Invalid: { furigana_response } " )
278274 # 如果 Yahoo 沒東西,這裡就會報錯或導致後面空值
279275
280- furigana_results : list [dict [str , str ]] = furigana_response .get ("result" , [])
276+ furigana_results : list [dict [str , Any ]] = furigana_response .get ("result" , [])
281277 logger .debug (f"Yahoo Results Count: { len (furigana_results )} " )
282278
283279 # 2. 呼叫 OJAD
@@ -287,7 +283,10 @@ async def mark_accent(
287283 ojad_idx_cnt = 0
288284
289285 logger .debug (f"🔍 [Type Check] furigana_results type: { type (furigana_results )} " )
290- logger .debug (f"🔍 [Data Check] furigana_results sample (first item): { furigana_results [0 ] if furigana_results else 'Empty' } " )
286+ logger .debug (
287+ "[Data Check] furigana_results sample (first item): "
288+ f"{ furigana_results [0 ] if furigana_results else 'Empty' } "
289+ )
291290
292291 for i , furigana_result in enumerate (furigana_results ):
293292 logger .debug (f" 🔍 [Type Check] Item [{ i } ] type: { type (furigana_result )} " )
@@ -297,7 +296,9 @@ async def mark_accent(
297296 yahoo_surface = furigana_result ["surface" ]
298297 accents : list [AccentInfo ] = []
299298
300- logger .debug (f"--- Processing Yahoo Word [{ i } ]: { yahoo_surface } ({ yahoo_furigana } ) ---" )
299+ logger .debug (
300+ f"Processing Yahoo Word [{ i } ]: { yahoo_surface } ({ yahoo_furigana } )"
301+ )
301302
302303 # If query sub-text contains non-kana and non-kanji words, ignore it
303304 if "subword" not in furigana_result and any (
@@ -324,7 +325,10 @@ async def mark_accent(
324325 # DEBUG matching logic
325326 if ojad_idx < len (ojad_results ):
326327 ojad_current_hira = jaconv .kata2hira (ojad_results [ojad_idx ]["text" ])
327- logger .debug (f" -> Comparing Yahoo '{ yahoo_furigana_hira } ' vs OJAD '{ ojad_current_hira } '" )
328+ logger .debug (
329+ "-> Comparing Yahoo '{yahoo_furigana_hira}'"
330+ f" vs OJAD '{ ojad_current_hira } '"
331+ )
328332 else :
329333 logger .warning (f" -> OJAD Index Out of Bounds ({ ojad_idx } )" )
330334
@@ -340,7 +344,8 @@ async def mark_accent(
340344 # Backup index
341345 temp_ojad_idx = ojad_idx
342346
343- while len (ojad_furigana ) < len (yahoo_furigana ) and temp_ojad_idx < len (ojad_results ):
347+ while (len (ojad_furigana ) < len (yahoo_furigana ) and
348+ temp_ojad_idx < len (ojad_results )):
344349 ojad_text = ojad_results [temp_ojad_idx ]["text" ]
345350 ojad_furigana += ojad_text
346351 temp_accents .append (
@@ -388,22 +393,32 @@ async def mark_accent(
388393 else :
389394 yahoo_subword = furigana_result ["subword" ]
390395 if len (yahoo_subword ) > 0 :
391- logger .debug (f" 🔍 [Type Check] yahoo_subword element type: { type (yahoo_subword [0 ])} " )
392- logger .debug (f" 🔍 [Data Check] yahoo_subword content: { yahoo_subword } " )
396+ logger .debug (
397+ "[Type Check] yahoo_subword element type: "
398+ f"{ type (yahoo_subword [0 ])} "
399+ )
400+ logger .debug (
401+ f"[Data Check] yahoo_subword content: { yahoo_subword } "
402+ )
393403 final_response_results .append (
394404 MultiWordAccentResultObject (
395405 furigana = yahoo_furigana ,
396406 surface = yahoo_surface ,
397407 accent = accent_info_list ,
398408 subword = [
399- SingleWordResultObject (furigana = s ['furigana' ], surface = s ['surface' ])
409+ SingleWordResultObject (
410+ furigana = s ['furigana' ], surface = s ['surface' ]
411+ )
400412 for s in yahoo_subword
401413 ],
402414 )
403415 )
404416 else :
405417 # [ERROR BLOCK]
406- logger .error (f" -> MATCH FAILED. Yahoo: { yahoo_furigana } vs OJAD Assembly: { ojad_furigana } " )
418+ logger .error (
419+ "-> MATCH FAILED."
420+ f"Yahoo: { yahoo_furigana } vs OJAD Assembly: { ojad_furigana } "
421+ )
407422 # 即使失敗,也要讓迴圈繼續,或者考慮怎麼處理錯誤
408423 # 目前程式碼遇到錯誤就沒有 append result,所以回傳會少東西
409424
0 commit comments