#include "c_NlpNgramGraph.h" #include #include #define RUN_TEST(test_case, name) \ do { \ printf("[RUN] %s... ", name); \ if (test_case) { \ printf("\033[32mPASSED\033[0m\n"); \ } else { \ printf("\033[31mFAILED\033[0m (%s:%d)\n", __FILE__, __LINE__); \ return C_ERR_FAIL; \ } \ } while(0) static c_err_t test(void) { /* 33. 多元馬爾可夫鏈(Trigram)核心通路驗證 */ c_NlpNgramGraph_t trigram; // 建立 3元 模型:需要根據 2 个字的历史預測第 3 个字 c_err_t err = c_NlpNgramGraph_Init(&trigram, 3, 512, 8); RUN_TEST(err == C_ERR_OK, "Trigram Graph initialized cleanly"); // 載入預測語料:"自然语言、自然界、自然语言" // 當已知歷史短语是 "自然" 时,"语" 出现了 2 次,"界" 出现了 1 次 c_ucs4_t corpus_stream[] = { 0x81EA, 0x7136, 0x8BED, 0x8A00, // 自然语言 0x81EA, 0x7136, 0x754C, // 自然界 0x81EA, 0x7136, 0x8BED, 0x8A00 // 自然语言 }; err = c_NlpNgramGraph_Process(&trigram, corpus_stream, 11); RUN_TEST(err == C_ERR_OK && trigram.total_states > 0, "Trigram processed training data stream"); // 構建查詢歷史:[0x81EA, 0x7136] ➔ 代表 "自然" c_ucs4_t query_history[] = { 0x81EA, 0x7136 }; c_ucs4_t pred_res = 0; double pred_conf = 0.0; err = c_NlpNgramGraph_PredictNext(&trigram, query_history, &pred_res, &pred_conf); RUN_TEST(err == C_ERR_OK, "PredictNext completed high-order context traversal paths"); // 預期:準確命中 "语" (0x8BED),置信度為 2 / 3 = 66.67% RUN_TEST(pred_res == 0x8BED, "Trigram language model predicts '语' given advanced context '自然'"); RUN_TEST(pred_conf > 0.66 && pred_conf < 0.67, "Context-bound model confidence targets 66.67%% correctly"); c_NlpNgramGraph_Destroy(&trigram); return C_ERR_OK; } int main(int argc, char** argv){ return test(); }