[英]How to read attributes from an arff file from scratch in java?
I'm trying to read an arff file from scratch for this assignment.我正在尝试从头开始读取此作业的 arff 文件。 I feel like I'm doing it wrong.
我觉得我做错了。 So like for example let's say I have this line
所以就像例如让我们说我有这条线
@attribute 'habitat' { 'd', 'g', 'l', 'm', 'p', 'u', 'w'}
I want to check if it's an attribute, then take the attribute name, and then add the instances to a list.我想检查它是否是一个属性,然后获取属性名称,然后将实例添加到列表中。 So far I'm doing this.
到目前为止,我正在这样做。 (st is the current line that i'm reading from the file)
(st 是我从文件中读取的当前行)
st=st.replaceAll("'", "");
String[] token = st.split(" ");
if(token[0].trim().equals("@attribute")) {
Attribute a=new Attribute(token[1]);
}
I'm not sure how to read the instances, since I split it according to spaces, the instances were split wrong.我不确定如何读取实例,因为我根据空格拆分它,所以实例拆分错误。 I feel like I'm going about reading this file wrong This is my attribute class
我觉得我要读错这个文件 这是我的属性 class
public class Attribute {
public String name;
public LinkedList<Instance> instanceList=new LinkedList<Instance>();
}
Any ideas?有任何想法吗?
Here is how to do it:这是如何做到的:
your Attribute class你的属性 class
class Attribute {
private String name;
private LinkedList<String> instanceList;
public Attribute(String name, LinkedList<String> instanceList) {
this.name = name;
this.instanceList = instanceList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LinkedList<String> getInstanceList() {
return instanceList;
}
public void setInstanceList(LinkedList<String> instanceList) {
this.instanceList = instanceList;
}
@Override
public String toString() {
return "Attribute{" +
"name='" + name + '\'' +
", instanceList=" + instanceList +
'}';
}
}
your main class:您的主要 class:
String str = "@attribute 'habitat' { 'd', 'g', 'l', 'm', 'p', 'u', 'w'}";
Pattern pattern = Pattern.compile("@attribute '(.*?)' \\{\\s*(.*?)\\s*\\}");
List<Attribute> list = new ArrayList<>();
// in case you would like to ignore case sensitivity,
// you could use this statement:
// Pattern pattern = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
// check all occurance
while (matcher.find()) {
list.add(new Attribute(matcher.group(1),
new LinkedList<>(Arrays.asList(matcher.group(2).replaceAll("[',]", "").trim().split("\\s+"))))
);
}
System.out.println(list);
output: output:
[Attribute{name='habitat', instanceList=[d, g, l, m, p, u, w]}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.