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