天天看點

hanlp中文智能分詞自動識别文字提取執行個體

需求:客戶給銷售員自己的個人資訊,銷售幫助客戶下單,此過程需要銷售人員手動複制粘貼收獲位址,電話,姓名等等,一個智能的分詞系統可以讓銷售人員一鍵識别以上各種資訊

經過調研,找到了一下開源項目

1、word 分詞器

2、ansj 分詞器

3、mmseg4j 分詞器

4、ik-analyzer 分詞器

5、jcseg 分詞器

6、fudannlp 分詞器

7、smartcn 分詞器

8、jieba 分詞器

9、stanford 分詞器

10、hanlp 分詞器

最後選擇了hanlp,步驟官網都有,下面示範智能比對位址

1   List<Term> list = HanLP.newSegment().seg("湯姆江西省南昌市紅谷灘新區111号電話12023232323");

2    System.out.println(list);

輸出

1   [湯姆/nrf, 江西省/ns, 南昌市/ns, 紅谷灘/nz, 新區/n, 111/m, 号/q, 電話/n, 12023232323/m]

大公告成,不過前提必須下載下傳那個600多M的data包并導入,才可以識别位址,否則隻是做了初步的識别

附上完整代碼

   1     String str = "湯姆   江西省南昌市紅谷灘新區111号     12023232323";

   2     String address = "";

   3     String phone = "";

   4     String name = "";

   5     List<Term> terms = NLPTokenizer.segment(str);

   6     System.out.println(terms);

   7     for (Term term : terms) {

   8         if (term.nature.startsWith("nr")){

   9             //nr代表人名

   10             name = term.word;

   11             System.out.println("name: " + term.word);

   12         }else if (term.nature.startsWith("m") && term.word.length() == 11){

   13             //m代表數字

    14            phone = term.word;

    15            System.out.println("電話: " + term.word);

    16        }

    17    }

    18    //由于位址包含了數字,解析的時候數字成為單獨的個體,與實際不符,是以通過差集求出位址

    19    address = str.replace(phone, "").replace(name, "").trim();

    20    System.out.println("address: " + address);

運作結果

1    name: 湯姆

2    電話: 12023232323

3    address: 江西省南昌市紅谷灘新區111号

---------------------

繼續閱讀