changeset 241:02783f3699b1

add word count
author sugi
date Tue, 16 Apr 2013 15:09:47 +0900
parents d9c9076d6b47
children 3bcaf12cf877
files .settings/org.eclipse.core.resources.prefs README src/alice/datasegment/Receiver.java src/alice/test/codesegment/local/wordcount/CorrectResult.java src/alice/test/codesegment/local/wordcount/Result.java src/alice/test/codesegment/local/wordcount/SetTask.java src/alice/test/codesegment/local/wordcount/StartWordCount.java src/alice/test/codesegment/local/wordcount/WordConfig.java src/alice/test/codesegment/local/wordcount/WordCount.java
diffstat 9 files changed, 183 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.settings/org.eclipse.core.resources.prefs	Tue Apr 09 11:26:17 2013 +0900
+++ b/.settings/org.eclipse.core.resources.prefs	Tue Apr 16 15:09:47 2013 +0900
@@ -1,3 +1,4 @@
-#Wed Mar 27 14:38:59 JST 2013
 eclipse.preferences.version=1
 encoding//src/alice/test/codesegment/local/bitonicsort/SortTest.java=UTF-8
+encoding//src/alice/test/codesegment/local/wordcount/SetTask.java=UTF-8
+encoding//src/alice/test/codesegment/local/wordcount/WordCount.java=UTF-8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README	Tue Apr 16 15:09:47 2013 +0900
@@ -0,0 +1,3 @@
+HOW TO USE TEST PROGRAM
+
+"-level info" is for log off
\ No newline at end of file
--- a/src/alice/datasegment/Receiver.java	Tue Apr 09 11:26:17 2013 +0900
+++ b/src/alice/datasegment/Receiver.java	Tue Apr 16 15:09:47 2013 +0900
@@ -65,7 +65,6 @@
 
 	public void setData(ReceiverData r) {
 		data = r;
-		
 	}
 
 	public int asInteger() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/codesegment/local/wordcount/CorrectResult.java	Tue Apr 16 15:09:47 2013 +0900
@@ -0,0 +1,34 @@
+package alice.test.codesegment.local.wordcount;
+
+import alice.codesegment.CodeSegment;
+import alice.datasegment.CommandType;
+import alice.datasegment.Receiver;
+
+public class CorrectResult extends CodeSegment{
+
+	private Receiver[] array;
+	
+	public CorrectResult(int size) {
+		array = new Receiver[size];
+		for (int i=0;i<size;i++)
+			array[i] = ids.create(CommandType.TAKE);
+		for (int i=0;i<size;i++)
+			array[i].setKey("result");
+	}
+
+	@Override
+	public void run() {
+		int line_num = 0;
+		int word_num = 0;
+		Result result;
+		for (int i=0;i<array.length;i++){
+			result = array[i].asClass(Result.class);
+			line_num +=result.line_num;
+			word_num +=result.word_num;
+		}
+		System.out.println(line_num+" "+word_num);
+		System.exit(0);
+		
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/codesegment/local/wordcount/Result.java	Tue Apr 16 15:09:47 2013 +0900
@@ -0,0 +1,14 @@
+package alice.test.codesegment.local.wordcount;
+
+import org.msgpack.annotation.Message;
+
+@Message
+public class Result {
+	public int line_num;
+	public int word_num;
+	
+	public Result(int line,int word){
+		line_num = line;
+		word_num = word;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/codesegment/local/wordcount/SetTask.java	Tue Apr 16 15:09:47 2013 +0900
@@ -0,0 +1,48 @@
+package alice.test.codesegment.local.wordcount;
+
+import alice.codesegment.CodeSegment;
+import java.io.*;
+
+public class SetTask extends CodeSegment {
+	
+	private WordConfig conf;
+
+	public SetTask(WordConfig conf) {
+		this.conf = conf;
+	}
+
+	@Override
+	public void run() {
+		BufferedReader br = null;
+		int i = 0;
+		try {
+			br= new BufferedReader(
+                    new InputStreamReader(
+                       new FileInputStream(
+                          new File(conf.filename)
+                          )));
+			int size = conf.division * 1024 * 1024; // 16Kbyte
+			char[] buf;
+			for (;;i++){
+				buf = new char[size];
+				int check = br.read(buf);
+				ods.put("array", buf);
+				if (check==-1) break;
+				new WordCount();
+			}
+			new CorrectResult(i);
+		} catch (FileNotFoundException e) {
+			System.out.println("file not found");
+			System.exit(0);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		
+		try {
+			br.close();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/codesegment/local/wordcount/StartWordCount.java	Tue Apr 16 15:09:47 2013 +0900
@@ -0,0 +1,14 @@
+package alice.test.codesegment.local.wordcount;
+
+import alice.daemon.AliceDaemon;
+import alice.daemon.Config;
+
+
+public class StartWordCount {
+	public static void main(String[] args){
+		new AliceDaemon(new Config(args)).listen(); // logger off
+		
+		WordConfig conf = new WordConfig(args);
+		new SetTask(conf).execute();
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/codesegment/local/wordcount/WordConfig.java	Tue Apr 16 15:09:47 2013 +0900
@@ -0,0 +1,31 @@
+package alice.test.codesegment.local.wordcount;
+
+public class WordConfig {
+	public String filename;
+	public int block = 1;
+	public static String[] array;
+	public int division = 16;
+	
+	public WordConfig(String[] args) {
+		for (int i=0;i<args.length; i++){
+			if ("-f".equals(args[i])){
+				filename = args[++i];	
+			} else if ("-b".equals(args[i])){
+				block = Integer.parseInt(args[++i]);	
+			}
+		}
+		
+		if (filename==null){
+			System.out.println("Usage: WordCount -f FILENAME");
+			System.exit(0);
+		}
+		createKey();
+	}
+
+	public void createKey(){
+		array = new String[block];
+		for(int i = 0 ; i < block ; i++) {
+			array[i] = "array" + i;
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/alice/test/codesegment/local/wordcount/WordCount.java	Tue Apr 16 15:09:47 2013 +0900
@@ -0,0 +1,37 @@
+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 r = ids.create(CommandType.TAKE); 
+	
+	public WordCount(){
+		r.setKey("array");
+	}
+
+	@Override
+	public void run() {
+		char[] a = (char[]) r.getObj();
+		int word_flag = 0;
+	    int word_num = 0;
+	    int line_num = 0;
+	    int i = 0;
+		for (; i < a.length; i++) {
+			if (a[i] == 0x20) { // 空白                                                                                                                                       
+				word_flag = 1;
+			} else if (a[i] == 0x0A) { // 改行                                                                                                                                
+				line_num += 1;
+				word_flag = 1;
+			} else {
+				word_num += word_flag;
+				word_flag = 0;
+			}
+		}
+		Result result = new Result(line_num,word_num);
+		ods.put("result", result);
+	}
+
+}