/*
* Created on 2004/08/06
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.io;
import java.io.IOException;
import java.io.InputStream;
/**
*
*
*/
public class WordCountInputStream extends LineSeparatorCountInputStream{
private int word;
public int getCountWord() {
return word;
}
public int getCountLineSeparator(){
return getR()+getN()+getRn();
}
/**
* @param input
*/
public WordCountInputStream(InputStream input) {
super(input);
// TODO Auto-generated constructor stub
}
public int read() throws IOException {
int ret=-1;
if(datas!=null){
ret=(int)datas.charAt(0);
datas=datas.substring(1);
if(datas.length()==0)
datas=null;
}else{
datas=lineRead();
countWord(datas);
if(datas!=null)
return read();
}
// TODO Auto-generated method stub
return ret;
}
/**
* @param datas
*/
private void countWord(String line) {
int start=0;
int end=0;
if(line!=null){
for(int i=0;i<line.length();i++){
if(isWord(line.charAt(i))){
end=i+1;
}else{
if(end-start>0){
//count word
parseWord(line.substring(start,end));
}
parseNotWord(line.charAt(i));
start=i;
end=i;
}
}
if(start!=end){
parseWord(line.substring(start,end));
}
}
}
/**
* @param c
*/
private void parseNotWord(char c) {
// TODO Auto-generated method stub
}
/**
* @param string
*/
private void parseWord(String string) {
word++;
}
/**
* @param c
* @return
*/
private boolean isWord(char c) {
return Character.isDigit(c) || isAZ(c) || c=='-' || c=='_';
}
/**
* @param c
* @return
*/
private boolean isAZ(char c) {
int num=(int)c;
return (num>=65 && num<=90) || (num>=97 && num<=122);
}
}
|