Mercurial > hg > Database > Alice
view src/alice/test/codesegment/local/wordcount/WordCount.java @ 255:f213bccfe1db
add comment
author | sugi |
---|---|
date | Mon, 01 Jul 2013 20:00:40 +0900 |
parents | 913ee9dfec4f |
children |
line wrap: on
line source
package alice.test.codesegment.local.wordcount; import alice.codesegment.CodeSegment; import alice.datasegment.CommandType; import alice.datasegment.Receiver; public class WordCount extends CodeSegment{ private Receiver info1 = ids.create(CommandType.TAKE); public WordCount(){ info1.setKey("array"); } @Override public void run() { Range r = info1.asClass(Range.class); int word_flag = 0; int word_num = 0; int line_num = 0; int i = 0; for (; i < r.array.length; i++) { if (r.array[i] == 0x20) { // 空白 word_flag = 1; } else if (r.array[i] == 0x0A) { // 改行 line_num += 1; word_flag = 1; } else { word_num += word_flag; word_flag = 0; } } word_num += word_flag; if (r.nextchar!='\0'){ // null means last block if ((r.array[i-1] == 0x0A||r.array[i-1] == 0x20) && (r.nextchar == 0x0A||r.nextchar == 0x20)){ word_num--; } } Result result = new Result(line_num,word_num); ods.put("result", result); recycle();//new WordCount(); } }