#ifndef INCLUDED_C_NLPNGRAMGRAPH_H #define INCLUDED_C_NLPNGRAMGRAPH_H #ifndef INCLUDED_C_UTF8_H #include #endif /*INCLUDED_C_UTF8_H*/ /* ------------------------------------------------------------------------------------------------------------------ */ /* */ /* --- 1. N-gram 預測邊(記錄後續可能出現的單個字及頻次) --- */ typedef struct c_NgramEdge_t { c_ucs4_t target_cp; /* 後續預測出的第 N 個字 */ c_size_t co_count; /* 該字在當前上下文後出現的次數 */ struct c_NgramEdge_t* next; /* 鄰接邊哈希衝突拉鏈 */ } c_NgramEdge_t; /* --- 2. N-gram 上下文節點(代表長度為 N-1 的歷史狀態) --- */ typedef struct c_NgramNode_t { c_ucs4_t* context; /* 歷史上下文陣列,長度為 N-1 */ c_size_t frequency; /* 該上下文總出現次數 */ c_NgramEdge_t** next_chars; /* 後續字哈希桶(動態分配) */ c_size_t relation_capacity; /* 邊哈希桶容量 */ struct c_NgramNode_t* next; /* 主哈希表衝突拉鏈指针(解決主桶碰撞) */ } c_NgramNode_t; /* --- 3. 多元馬爾可夫鏈模型主控結構体 --- */ typedef struct { c_NgramNode_t** buckets; /* 主哈希表(儲存所有不重複的歷史上下文狀態) */ c_size_t bucket_count; /* 主哈希桶大小 */ c_size_t n_value; /* 模型階數 N (例如:3 代表 Trigram 模型) */ c_size_t relation_default_capacity; /* 子節點邊哈希桶默認容量 */ c_size_t total_states; /* 記錄圖中唯一存在的歷史狀態總數 */ } c_NlpNgramGraph_t; /* ------------------------------------------------------------------------------------------------------------------ */ /* */ c_err_t c_NlpNgramGraph_Init(c_NlpNgramGraph_t* self, c_size_t n_value, c_size_t bucket_count, c_size_t relation_default_capacity); void c_NlpNgramGraph_Destroy(c_NlpNgramGraph_t* self); c_err_t c_NlpNgramGraph_Process(c_NlpNgramGraph_t* self, const c_ucs4_t* unicode_array, c_size_t length); c_err_t c_NlpNgramGraph_PredictNext(c_NlpNgramGraph_t* self, const c_ucs4_t* history_context, c_ucs4_t* out_next_char, double* out_confidence); #endif /*INCLUDED_C_NLPNGRAMGRAPH_H*/