1
import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class FileReader 
{
    public static final String PATH_TO_DATA_FILE = "playtennis.data";
    public static ArrayList<Record> buildRecords() {
    BufferedReader reader = null;
    DataInputStream dis = null;
    ArrayList<Record> records = new ArrayList<Record>();

    try { 
       File f = new File(PATH_TO_DATA_FILE);
       FileInputStream fis = new FileInputStream(f); 
       reader = new BufferedReader(new InputStreamReader(fis));;

       // read the first record of the file
       String line;
       Record r = null;
       ArrayLAist<DiscreteAttribute> attributes;
       while ((line = reader.readLine()) != null) {
          StringTokenizer st = new StringTokenizer(line, ",");
          attributes = new ArrayList<DiscreteAttribute>();
          r = new Record();

          if(Hw1.NUM_ATTRS != st.countTokens()) {
              throw new Exception("Unknown number of attributes!");
          }

          @SuppressWarnings("unused")
          String day = st.nextToken();
          String outlook = st.nextToken();
          String temperature = st.nextToken();
          String humidity = st.nextToken();
          String wind = st.nextToken();
          String playTennis = st.nextToken();

          if(outlook.equalsIgnoreCase("overcast")) {
              attributes.add(new DiscreteAttribute("Outlook", DiscreteAttribute.Overcast));
          }
          else if(outlook.equalsIgnoreCase("sunny")) {
              attributes.add(new DiscreteAttribute("Outlook", DiscreteAttribute.Sunny));
          }
          else if(outlook.equalsIgnoreCase("rain")) {
              attributes.add(new DiscreteAttribute("Outlook", DiscreteAttribute.Rain));
          }

          if(temperature.equalsIgnoreCase("hot")) {
              attributes.add(new DiscreteAttribute("Temperature", DiscreteAttribute.Hot));
          }
          else if(temperature.equalsIgnoreCase("mild")) {
              attributes.add(new DiscreteAttribute("Temperature", DiscreteAttribute.Mild));
          }
          else if(temperature.equalsIgnoreCase("cool")) {
              attributes.add(new DiscreteAttribute("Temperature", DiscreteAttribute.Cool));
          }

          if(humidity.equalsIgnoreCase("high")) {
              attributes.add(new DiscreteAttribute("Humidity", DiscreteAttribute.High));
          }
          else if(humidity.equalsIgnoreCase("normal")) {
               attributes.add(new DiscreteAttribute("Humidity", DiscreteAttribute.Normal));
          }

          if(wind.equalsIgnoreCase("weak")) {
               attributes.add(new DiscreteAttribute("Wind", DiscreteAttribute.Weak));
          }
          else if(wind.equalsIgnoreCase("strong")) {
               attributes.add(new DiscreteAttribute("Wind", DiscreteAttribute.Strong));
          }

          if(playTennis.equalsIgnoreCase("no")) {
               attributes.add(new DiscreteAttribute("PlayTennis", DiscreteAttribute.PlayNo));
          }
          else if(playTennis.equalsIgnoreCase("yes")) {
              attributes.add(new DiscreteAttribute("PlayTennis", DiscreteAttribute.PlayYes));
          }

          r.setAttributes(attributes);
          records.add(r);
      }
   }
}

我已将文件名指定为 FileReader

  1. 我在无法归档符号上遇到错误
  2. 找不到符号符号:类离散属性,位置:类分类器.FileReader
  3. 找不到符号符号:变量离散属性位置:类分类器.FileReader
4

1 回答 1

0

这里是 DiscreteAttribute 类。您需要将该类添加到您的项目中。

public class DiscreteAttribute extends Attribute { public static final int Sunny = 0; public static final int Overcast = 1; public static final int Rain = 2; public static final int Hot = 0; public static final int Mild = 1; public static final int Cool = 2; public static final int High = 0; public static final int Normal = 1; public static final int Weak = 0; public static final int Strong = 1; public static final int PlayNo = 0; public static final int PlayYes = 1; enum PlayTennis { No, Yes } enum Wind { Weak, Strong } enum Humidity { High, Normal } enum Temp { Hot, Mild, Cool } enum Outlook { Sunny, Overcast, Rain } public DiscreteAttribute(String name, double value) { super(name, value); } public DiscreteAttribute(String name, String value) { super(name, value); } }
于 2017-04-04T18:35:16.217 回答