IK participles Add punctuation

Source: Internet
Author: User

ik participle default word-breaker result is no punctuation, read a source. Find the direct modification of the source code to add punctuation needs to change multiple places.

So think of a strategy, from the outside package layer of code, first let IK participle, and then detect whether there are missing symbols before and after it, and then add the symbol to the word segmentation results.

Of course, if you have a good way, please hand in!

Below is the source code section:

public class Ikdemo {public static void main (string[] args) throws Exception {String input = "^_^ Hello, ik participle! "; Lexeme Beforeword = null; Lexeme Currentword = null; StringBuffer sb = new StringBuffer () iksegmenter ik = new Iksegmenter (new StringReader (input), true); System.out.println ("Original sentence:" +input); while ((Currentword = Ik.next ()) = null) {sb.append (Appendsymbol (input, Beforeword, Currentword)); Beforeword = Currentword;} Sb.append (Appendsymbol (input, Beforeword, Currentword)); System.out.println ("Participle:" +sb.tostring (). ReplaceAll ("+", ""). Trim ()); /** * Complement IK word missing symbol * @param line * @param before * @param cur * @return */public static string Appendsymbol (string line, L Exeme before, Lexeme cur) {String res = "", if (before = = null) {//The first word precedes the symbol res = Cur.getlexemetext () + ""; int start = CU R.getbegin (); if (Start > 0) {String left =appendwhitespace (line.substring (0, start)); res = left + res;}} else if (cur = = null) {//The last Word is followed by the symbol int end = Before.getendposition (); if (End < Line.length ()) {String Right =appendwhitespace (line.substring (Before.getendposition ())), res = right;}} else {//and the symbol between the previous word res = Cur.getlexemetext () + ""; int beforeend = Before.getendposition (); if (Cur.getbegin () > before END) {String mid = Appendwhitespace (line.substring (BeforeEnd, Cur.getbegin ())); res = mid + res;}} return res;} /** * How are you, hello? * @param src * @return */public static String appendwhitespace (string src) {string dst= ""; for (Char C:src.tochararray ()) {DST + = c + ";} return DST;}}

Output Result:

The original sentence: ^_^ Hello, ik participle! Participle: ^ _ ^ Hello, ik participle!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IK participles Add punctuation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.