345
|
1 package alice.test.codesegment.local.wordcount;
|
|
2
|
|
3 import alice.codesegment.CodeSegment;
|
|
4 import alice.datasegment.CommandType;
|
|
5 import alice.datasegment.Receiver;
|
|
6
|
|
7 public class WordCount extends CodeSegment{
|
|
8
|
|
9 private Receiver info1 = ids.create(CommandType.TAKE);
|
|
10
|
|
11 public WordCount(){
|
|
12 info1.setKey("array");
|
|
13 }
|
|
14
|
|
15 @Override
|
|
16 public void run() {
|
|
17 Range r = info1.asClass(Range.class);
|
|
18 int word_flag = 0;
|
|
19 int word_num = 0;
|
|
20 int line_num = 0;
|
|
21 int i = 0;
|
|
22 for (; i < r.array.length; i++) {
|
|
23 if (r.array[i] == 0x20) { // 空白
|
|
24 word_flag = 1;
|
|
25 } else if (r.array[i] == 0x0A) { // 改行
|
|
26 line_num += 1;
|
|
27 word_flag = 1;
|
|
28 } else {
|
|
29 word_num += word_flag;
|
|
30 word_flag = 0;
|
|
31 }
|
|
32 }
|
|
33 word_num += word_flag;
|
|
34 if (r.nextchar!='\0'){ // null means last block
|
|
35 if ((r.array[i-1] == 0x0A||r.array[i-1] == 0x20) &&
|
|
36 (r.nextchar == 0x0A||r.nextchar == 0x20)){
|
|
37 word_num--;
|
|
38 }
|
|
39 }
|
|
40
|
|
41 Result result = new Result(line_num,word_num);
|
|
42 ods.put("result", result);
|
|
43 recycle();//new WordCount();
|
|
44
|
|
45 }
|
|
46
|
|
47 }
|