📄 myfilereader.java
字号:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import javax.swing.JOptionPane;
/*
* Created on 20/03/2005
*/
/**
* @author Fernando Meyer
*/
public class MyFileReader {
private Attribute attribute;
private List attributesList;
private DataInstance data;
private List dataInstancesList;
private String relation;
public MyFileReader(GUIAntMinerJFrame caller, File file){
String lastWord = "";
boolean gotNext, error=false;
try{
attributesList = new LinkedList();
dataInstancesList = new LinkedList();
BufferedReader in = new BufferedReader(new FileReader(file));
StreamTokenizer st = new StreamTokenizer(in);
//st.resetSyntax();
//st.quoteChar(39);
st.commentChar('%');
st.ordinaryChars(45,62);
st.wordChars(95,95);
st.wordChars(45,63);
st.wordChars(38,39);
st.whitespaceChars(',',',');
st.nextToken();
while(st.ttype != StreamTokenizer.TT_EOF){
gotNext = false;
if(st.ttype == 64){ // (64 = @)
st.nextToken();
if(st.sval.trim().compareToIgnoreCase("RELATION") == 0){
st.nextToken();
relation = st.sval;
caller.setLabel2(relation);
}
else if(st.sval.trim().compareToIgnoreCase("ATTRIBUTE") == 0){
st.nextToken();
attribute = new Attribute();
attribute.setAttributeName(st.sval);
int linenum = st.lineno();
st.nextToken();
while(linenum == st.lineno()){
if(st.ttype == StreamTokenizer.TT_WORD){
attribute.addType(st.sval);
lastWord = st.sval;
}
st.nextToken();
if(linenum != st.lineno() && lastWord.trim().compareToIgnoreCase("REAL") == 0){
JOptionPane.showMessageDialog(null, "<html><font face=Dialog size=3>This version of GUI Ant-Miner can only handle nominal attributes.</font></html>", "Error", JOptionPane.ERROR_MESSAGE);
caller.setFileIsOk(false);
throw new IOException("File error");
}
gotNext = true;
}
attributesList.add(attribute);
}
else if(st.sval.trim().compareToIgnoreCase("DATA") == 0){
st.nextToken();
while(st.ttype != StreamTokenizer.TT_EOF){
data = new DataInstance();
int linenum = st.lineno();
while(linenum == st.lineno()){
if(st.ttype == StreamTokenizer.TT_WORD)
data.addValue(st.sval);
st.nextToken();
gotNext = true;
}
dataInstancesList.add(data);
}
}
}
if(!gotNext)
st.nextToken();
}
} catch (IOException e) {
error = true;
caller.setInitialState();
}
if(!error){
caller.setFileIsOk(true);
caller.setAttributes(attributesList.size());
caller.setInstances(dataInstancesList.size());
caller.setTableAtt1(attributesList);
}
//caller.setTableAtt2(generateStats("age"));
//caller.setTableAtt2();
//generateStats("contact-lenses");
}
public String getAttributeName(int attributePos){
return ((Attribute) attributesList.get(attributePos)).getAttributeName();
}
public List getdataInstancesList(){
return dataInstancesList;
}
public int calculateMissing(int attributePos){
int count;
ListIterator i_dataInstancesList = dataInstancesList.listIterator();
count = 0;
while(i_dataInstancesList.hasNext()){
if(((DataInstance) i_dataInstancesList.next()).getValues().get(attributePos).toString().compareTo("?") == 0){
count++;
}
}
return count;
}
public List generateStats(int attributePos){
List returnList;
int count;
ListIterator i_typesList = ((Attribute) attributesList.get(attributePos)).getTypes().listIterator();
returnList = new LinkedList();
while(i_typesList.hasNext()){
String type = i_typesList.next().toString();
ListIterator i_dataInstancesList = dataInstancesList.listIterator();
count = 0;
while(i_dataInstancesList.hasNext()){
if(((DataInstance) i_dataInstancesList.next()).getValues().get(attributePos).toString().compareTo(type) == 0){
count++;
}
}
String typeAndCount[] = new String[] {type, new Integer(count).toString()};
returnList.add(typeAndCount);
}
return returnList;
}
public List generateStats(String attributeName){
List returnList;
boolean found = false;
int attributePos=0;
ListIterator i_attributesList = attributesList.listIterator();
while(i_attributesList.hasNext() && (found == false)){
attributePos++;
if(((Attribute) i_attributesList.next()).getAttributeName().compareTo(attributeName) == 0){
found = true;
}
}
attributePos--;
int count;
ListIterator i_typesList = ((Attribute) attributesList.get(attributePos)).getTypes().listIterator();
returnList = new LinkedList();
while(i_typesList.hasNext()){
String type = i_typesList.next().toString();
ListIterator i_dataInstancesList = dataInstancesList.listIterator();
count = 0;
while(i_dataInstancesList.hasNext()){
if(((DataInstance) i_dataInstancesList.next()).getValues().get(attributePos).toString().compareTo(type) == 0){
count++;
}
}
String typeAndCount[] = new String[] {type, new Integer(count).toString()};
returnList.add(typeAndCount);
}
return returnList;
}
public int totalDistinct(){ //returns total number of possible attributes' values
ListIterator li = attributesList.listIterator();
int sum=0;
while(li.hasNext())
sum += ((Attribute) li.next()).getTypes().size();
return sum;
}
public List getAttributesList(){
return attributesList;
}
public List getDataInstancesList(){
return dataInstancesList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -