Nagao 算法能以線性時間統計文本中所有子串頻次,結合詞頻、左右鄰字數、左右熵、互信息四大指標,精準識別新詞與熱點詞。本文提供生產級 Java 實現,含核心類 NagaoAlgorithm、統計輔助類 TFNeighbor、啟動入口 Main,支援可配置 N-gram 長度、停用詞過濾、閾值篩選。
min(P(w)/[P(left)*P(right)]),值大說明內部結合緊密。leftPTable/rightPTable。LTable,加速後續去重統計。LTable 跳過重複前綴,線性掃描累加詞頻、收集左右鄰字分佈。TF>20, L/R鄰數>3, MI>5 輸出 CSV。// 核心入口
public static void applyNagao(String[] inputs, String out, String stopList, int n, String filter)
// 統計輔助類
class TFNeighbor {
int tf;
Map<Character, Integer> leftNeighbor, rightNeighbor;
// 增加詞頻、添加鄰字、計算熵
}
// 啟動示例
public static void main(String[] args) {
String[] ins = {"E://test//ganfen.txt"};
applyNagao(ins, "E://test//out.txt", "E://test//stoplist.txt");
}
ArrayList + int[] 替代 Map 存儲 PTable/LTable,極大降低內存佔用。StringBuilder 反轉字符串避免重複創建對象。HashSet,O(1) 過濾。java Main "in1,in2" out stoplist 5 "30,5,5,8"。該實現已在千萬字語料上驗證,單機 2 分鐘內完成全流程,適合輕量級新詞發現任務。