1. 程式人生 > >Java機械分詞

Java機械分詞

一段 .get for tac adl oid label 位置 equal

這是我們做的一個小作業,不多說

直接附上我寫的代碼:

 

    public void Zheng() {
        try {
            BufferedReader bre = null;
            //String file = "d.txt";
            String file = "Dict.txt";
            bre = new BufferedReader(new FileReader(file));
            String line;
            try {
                
while ((line = bre.readLine()) != null) // 判斷最後一行不存在,為空結束循環 { temp.add(line); // System.out.println(line);// 原樣輸出讀到的內容 } bre.close(); } catch (IOException e) { e.printStackTrace(); } }
catch (FileNotFoundException e) { System.out.println("未成功讀取文件"); e.printStackTrace(); } String sub_str; while (start < total_len) { boolean flag = false; tmp_len = start + max_len <= total_len ? max_len : total_len - start; label:
while (tmp_len > 0) { // System.out.println(1); sub_str = receive.substring(start, start + tmp_len); // System.out.println(sub_str); for (i = 0; i < temp.size(); i++) { if (sub_str.equals(temp.get(i))) { flag = true; } } if (flag == true) { words.add(receive.substring(start, start + tmp_len)); start = start + tmp_len; break label; } else { tmp_len = tmp_len - 1; // System.out.println(tmp_len); } /* * if sub_str in dict: words.append(setence[start : start + * tmp_len]) start = start + tmp_len break else: tmp_len = * tmp_len -1 */ } if (tmp_len == 0) { words.add(receive.substring(start, start + 1)); start = start + 1; } }

效果就是按照詞庫中的詞將一段語句分割開來。

附上詞庫的位置:

https://pan.baidu.com/s/1pLBKer5

Java機械分詞