Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. public class Main {
  2.    
  3.     static Scanner sc = new Scanner(System.in);
  4.  
  5.     public static void main(String[] args) throws NumberFormatException, IOException {
  6.        
  7.         Knnalgorithm implement = new Knnalgorithm();
  8.        
  9.         while(true) {
  10.            
  11.         addData(implement);
  12.        
  13.        
  14.         }
  15.  
  16.     }
  17.  
  18.     static void calculate(Knnalgorithm implement) throws NumberFormatException, IOException {
  19.        
  20.         System.out.println("Enter training dataset file name");
  21.         String trainfilename = sc.nextLine();
  22.         System.out.println("Enter test dataset file name");
  23.         String testfilename = sc.nextLine();
  24.  
  25.         implement.getKValue();
  26.         implement.loadtrainData(trainfilename);
  27.         implement.loadtestData(testfilename);
  28.         implement.Distance();
  29.         implement.accuracy();
  30.         //sc.close();
  31.     }
  32.    
  33.     static void addData(Knnalgorithm implement) throws IOException {
  34.        
  35.  
  36.         Main: while (true) {
  37.            
  38.             System.out.println("Do you want add data? Y/N");
  39.             String answer = sc.nextLine();         
  40.             if (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes")) {
  41.                
  42.                 System.out.println("Enter test dataset file name");
  43.                 String testfilename = sc.nextLine();
  44.                 implement.loadtestData(testfilename);
  45.                 System.out.println("Enter new features:");
  46.                 String line = sc.nextLine();
  47.                 String[] split = line.split(",");
  48.                 double[] feature = new double[split.length];
  49.                 for (int i = 0; i < split.length; i++) {
  50.                     feature[i] = Double.parseDouble(split[i]);
  51.                 }
  52.                 Set<String> unique = new HashSet<String>();
  53.                 unique = implement.findUniques();
  54.                
  55.                 System.out.println("hint: Current labels: " + unique.toString());
  56.                 System.out.println("Enter new label:");
  57.  
  58.                 String label = sc.nextLine();
  59.                 boolean check = false;
  60.                 for (String s : unique) {
  61.                     if (s.equalsIgnoreCase(label)) {
  62.                         check = true;
  63.                     }
  64.                 }
  65.                 if (check) {
  66.                     implement.setTest(feature, label);
  67.                     implement.writefile();
  68.                     System.out.println("new data added succesfully!");
  69.                     break Main;
  70.                 } else {
  71.                     System.out.println("are you sure to add new label? Y/N");
  72.                     String answer2 = sc.nextLine();
  73.                     if (answer2.equalsIgnoreCase("y") || answer2.equalsIgnoreCase("yes")) {
  74.                         implement.setTest(feature, label);
  75.                         implement.writefile();
  76.                         System.out.println("new data added succesfully!");
  77.                         break Main;
  78.  
  79.                     } else if (answer2.equalsIgnoreCase("n") || answer2.equalsIgnoreCase("no")) {
  80.                         break Main;
  81.                     } else {
  82.                         System.out.println("unknown input");
  83.  
  84.                     }
  85.  
  86.                 }
  87.  
  88.                
  89.  
  90.             } else if (answer.equalsIgnoreCase("n") || answer.equalsIgnoreCase("no")) {
  91.                 implement.cleanStard();
  92.                 calculate(implement);
  93.             } else {
  94.                 System.out.println("Unknown answer!");
  95.  
  96.                 break Main;
  97.             }
  98.         }
  99.        
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement