Skip to content

Commit

Permalink
optimize: use StringBuilder
Browse files Browse the repository at this point in the history
Long continuious Chinese text will cost java lots of time if concat
string using '+='
  • Loading branch information
Menghan authored and menghan committed Nov 7, 2014
1 parent 99f4009 commit ed9e6c6
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/main/java/com/huaban/analysis/jieba/JiebaSegmenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,45 +173,45 @@ public List<String> sentenceProcess(String sentence) {

int x = 0;
int y = 0;
String buf = "";
String buf;
StringBuilder sb = new StringBuilder();
while (x < N) {
y = route.get(x).key + 1;
String lWord = sentence.substring(x, y);
if (y - x == 1)
buf += lWord;
sb.append(lWord);
else {
if (buf.length() > 0) {
if (sb.length() > 0) {
buf = sb.toString();
sb = new StringBuilder();
if (buf.length() == 1) {
tokens.add(buf.toString());
buf = "";
tokens.add(buf);
}
else {
if (wordDict.containsWord(buf)) {
tokens.add(buf.toString());
tokens.add(buf);
}
else {
finalSeg.cut(buf, tokens);
}
buf = "";
}
}
tokens.add(lWord);
}
x = y;
}
buf = sb.toString();
if (buf.length() > 0) {
if (buf.length() == 1) {
tokens.add(buf.toString());
buf = "";
tokens.add(buf);
}
else {
if (wordDict.containsWord(buf)) {
tokens.add(buf.toString());
tokens.add(buf);
}
else {
finalSeg.cut(buf, tokens);
}
buf = "";
}

}
Expand Down

0 comments on commit ed9e6c6

Please sign in to comment.