134 lines
3.8 KiB
TypeScript
134 lines
3.8 KiB
TypeScript
import { useState } from 'react'
|
|
import { useToast } from '@/components/shared/Toast'
|
|
import { getLevelIndex } from '@/lib/utils/cefrUtils'
|
|
import { API_CONFIG } from '@/lib/config/api'
|
|
|
|
interface AnalysisResult {
|
|
originalText: string
|
|
sentenceMeaning: string
|
|
grammarCorrection: any
|
|
vocabularyAnalysis: Record<string, any>
|
|
idioms: any[]
|
|
[key: string]: any
|
|
}
|
|
|
|
export function useSentenceAnalysis() {
|
|
const [isAnalyzing, setIsAnalyzing] = useState(false)
|
|
const toast = useToast()
|
|
|
|
const analyzeSentence = async (textInput: string) => {
|
|
setIsAnalyzing(true)
|
|
|
|
try {
|
|
const response = await fetch(`${API_CONFIG.BASE_URL}/api/ai/analyze-sentence`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
inputText: textInput,
|
|
analysisMode: 'full',
|
|
options: {
|
|
includeGrammarCheck: true,
|
|
includeVocabularyAnalysis: true,
|
|
includeTranslation: true,
|
|
includeIdiomDetection: true,
|
|
includeExamples: true
|
|
}
|
|
})
|
|
})
|
|
|
|
if (!response.ok) {
|
|
let errorMessage = `API請求失敗: ${response.status}`
|
|
try {
|
|
const errorData = await response.json()
|
|
errorMessage = errorData.error?.message || errorData.message || errorMessage
|
|
} catch (e) {
|
|
console.warn('無法解析錯誤回應:', e)
|
|
}
|
|
throw new Error(errorMessage)
|
|
}
|
|
|
|
const result = await response.json()
|
|
|
|
if (!result.success || !result.data) {
|
|
throw new Error('API回應格式錯誤')
|
|
}
|
|
|
|
// 處理API回應 - 適配新的後端格式
|
|
const apiData = result.data.data
|
|
|
|
// 設定完整的分析結果
|
|
const analysisData: AnalysisResult = {
|
|
originalText: apiData.originalText,
|
|
sentenceMeaning: apiData.sentenceMeaning,
|
|
grammarCorrection: apiData.grammarCorrection,
|
|
vocabularyAnalysis: apiData.vocabularyAnalysis,
|
|
idioms: apiData.idioms || []
|
|
}
|
|
|
|
// 計算詞彙統計
|
|
const vocabularyStats = calculateVocabularyStats(apiData.vocabularyAnalysis)
|
|
|
|
toast.success('句子分析完成!')
|
|
return {
|
|
success: true,
|
|
data: {
|
|
analysis: analysisData,
|
|
stats: vocabularyStats,
|
|
sentenceMeaning: apiData.sentenceMeaning,
|
|
grammarCorrection: apiData.grammarCorrection
|
|
}
|
|
}
|
|
|
|
} catch (error: any) {
|
|
const errorMessage = error.message || '分析失敗,請重試'
|
|
toast.error(errorMessage)
|
|
return {
|
|
success: false,
|
|
error: errorMessage
|
|
}
|
|
} finally {
|
|
setIsAnalyzing(false)
|
|
}
|
|
}
|
|
|
|
// 詞彙統計計算邏輯
|
|
const calculateVocabularyStats = (vocabularyAnalysis: Record<string, any>) => {
|
|
if (!vocabularyAnalysis) {
|
|
return { simpleCount: 0, moderateCount: 0, difficultCount: 0, idiomCount: 0 }
|
|
}
|
|
|
|
const userLevel = localStorage.getItem('userEnglishLevel') || 'A2'
|
|
const userLevelIndex = getLevelIndex(userLevel)
|
|
|
|
let simpleCount = 0
|
|
let moderateCount = 0
|
|
let difficultCount = 0
|
|
let idiomCount = 0
|
|
|
|
Object.values(vocabularyAnalysis).forEach((wordData: any) => {
|
|
if (wordData.isIdiom) {
|
|
idiomCount++
|
|
return
|
|
}
|
|
|
|
const wordLevelIndex = getLevelIndex(wordData.cefr || 'A1')
|
|
|
|
if (wordLevelIndex < userLevelIndex) {
|
|
simpleCount++
|
|
} else if (wordLevelIndex === userLevelIndex || wordLevelIndex === userLevelIndex + 1) {
|
|
moderateCount++
|
|
} else {
|
|
difficultCount++
|
|
}
|
|
})
|
|
|
|
return { simpleCount, moderateCount, difficultCount, idiomCount }
|
|
}
|
|
|
|
return {
|
|
analyzeSentence,
|
|
isAnalyzing
|
|
}
|
|
} |