44
55import logging
66import string
7- from typing import Any
7+ from typing import Any , cast
88
99import httpx
1010import jaconv
1515
1616from api .dependencies import get_http_client
1717from api .furigana_marker import (
18+ MultiWordResultObject ,
1819 RequestBody ,
1920 SingleWordResultObject ,
2021 mark_furigana_service ,
@@ -267,40 +268,41 @@ async def mark_accent(
267268 try :
268269 query_text = neologdn .normalize (request .text , tilde = "normalize" )
269270
270- furigana_response = await mark_furigana_service (query_text , client )
271+ furigana_response = cast (
272+ Response , await mark_furigana_service (query_text , client )
273+ )
271274
272275 # 檢查 Yahoo 回傳
273- if not furigana_response or "result" not in furigana_response :
276+ if furigana_response . status != 200 or not furigana_response . result :
274277 logger .warning (f"Yahoo Response Empty or Invalid: { furigana_response } " )
275278
276- furigana_results : list [ dict [ str , Any ]] = furigana_response .get ( " result" , [])
277- logger .debug (f"Yahoo Results Count: { len (furigana_results )} " )
279+ furigana_results = furigana_response .result or []
280+ logger .debug (f"Yahoo Results Count: { len (furigana_results or [] )} " )
278281
279282 ojad_surface , ojad_results = await get_ojad_result (query_text , client )
280283
281284 final_response_results = []
282285 ojad_idx_cnt = 0
283286
284- logger .debug (f"🔍 [Type Check] furigana_results type: { type (furigana_results )} " )
285287 logger .debug (
286- "[Data Check] furigana_results sample (first item): "
288+ "🔍 [Data Check] First item: "
287289 f"{ furigana_results [0 ] if furigana_results else 'Empty' } "
288290 )
289291
290292 for i , furigana_result in enumerate (furigana_results ):
291- logger .debug (f" 🔍 [Type Check] Item [{ i } ] type: { type (furigana_result )} " )
292- logger .debug (f" 🔍 [Data Check] Item keys: { furigana_result .keys ()} " )
293- yahoo_furigana = furigana_result ["furigana" ]
293+ yahoo_furigana = furigana_result .furigana
294+ yahoo_surface = furigana_result .surface
295+
296+ is_multi_word = isinstance (furigana_result , MultiWordResultObject )
294297 yahoo_furigana_hira = jaconv .kata2hira (yahoo_furigana )
295- yahoo_surface = furigana_result ["surface" ]
296298 accents : list [AccentInfo ] = []
297299
298300 logger .debug (
299301 f"Processing Yahoo Word [{ i } ]: { yahoo_surface } ({ yahoo_furigana } )"
300302 )
301303
302304 # If query sub-text contains non-kana and non-kanji words, ignore it
303- if "subword" not in furigana_result and any (
305+ if not is_multi_word and any (
304306 not is_kana_or_kanji (chr ) for chr in yahoo_furigana
305307 ):
306308 logger .debug (" -> Skipped (Not Kana/Kanji)" )
@@ -382,16 +384,8 @@ async def mark_accent(
382384
383385 ojad_idx_cnt = temp_ojad_idx # Update global index
384386
385- if "subword" not in furigana_result :
386- final_response_results .append (
387- SingleWordAccentResultObject (
388- furigana = yahoo_furigana ,
389- surface = yahoo_surface ,
390- accent = accent_info_list ,
391- )
392- )
393- else :
394- yahoo_subword : list [dict [str , str ]] = furigana_result ["subword" ]
387+ if isinstance (furigana_result , MultiWordAccentResultObject ):
388+ yahoo_subword = furigana_result .subword # ignore
395389 if len (yahoo_subword ) > 0 :
396390 logger .debug (
397391 "[Type Check] yahoo_subword element type: "
@@ -407,12 +401,20 @@ async def mark_accent(
407401 accent = accent_info_list ,
408402 subword = [
409403 SingleWordResultObject (
410- furigana = s [ " furigana" ] , surface = s [ " surface" ]
404+ furigana = s . furigana , surface = s . surface
411405 )
412406 for s in yahoo_subword
413407 ],
414408 )
415409 )
410+ else :
411+ final_response_results .append (
412+ SingleWordAccentResultObject (
413+ furigana = yahoo_furigana ,
414+ surface = yahoo_surface ,
415+ accent = accent_info_list ,
416+ )
417+ )
416418 else :
417419 # [ERROR BLOCK]
418420 logger .error (
0 commit comments