Guest User

Untitled

a guest
Jan 3rd, 2021
37
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import lombok.val;
  2. import org.apache.commons.io.FileUtils;
  3.  
  4.  
  5. import java.io.File;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Random;
  11.  
  12. public class RandomGenerator {
  13.  
  14.     List createRandomGaussianSequence(int number){
  15.         var list = new ArrayList<>();
  16.         var random = new Random();
  17.  
  18.         for(var i =0; i < number ; i++){
  19.  
  20.             list.add(random.nextDouble());
  21.         }
  22.  
  23.  
  24.     return list;
  25.     }
  26.  
  27.  
  28.     void createFile(String filename,String folder, List<Double> list){
  29.  
  30.         try {
  31.             FileWriter myWriter = new FileWriter("./"+folder+"/"+filename+".csv");
  32.             String values = "";
  33.             for(Double data: list) {
  34.                values+= data +",";
  35.             }
  36.             //delete last comma
  37.             values = values.replaceAll(", $", "\n");
  38.             myWriter.write(values);
  39.             myWriter.close();
  40.  
  41.         } catch (IOException e) {
  42.             System.out.println("An error occurred.");
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.  
  47.     void createDirectories(String featureDir, String targetDir) {
  48.  
  49.  
  50.         File feature = new File(featureDir);
  51.         File target = new File(targetDir);
  52.  
  53.         boolean isFeatureDirectoryCreated = feature.mkdir();
  54.  
  55.         if (isFeatureDirectoryCreated) {
  56.             System.out.println("successfully made");
  57.  
  58.         } else {
  59.             deleteDir(feature);  // Invoke recursive method
  60.             feature.mkdir();
  61.         }
  62.  
  63.         boolean isTargetDirectoryCreated = target.mkdir();
  64.  
  65.         if (isTargetDirectoryCreated) {
  66.             System.out.println("successfully made");
  67.  
  68.         } else {
  69.             deleteDir(target);  // Invoke recursive method
  70.             feature.mkdir();
  71.         }
  72.  
  73.  
  74.     }
  75.  
  76.  
  77.  
  78.  
  79.     public void deleteDir(File dir) {
  80.         File[] files = dir.listFiles();
  81.  
  82.         for (File myFile: files) {
  83.             if (myFile.isDirectory()) {
  84.                 deleteDir(myFile);
  85.             }
  86.             myFile.delete();
  87.  
  88.         }
  89.     }
  90.  
  91.  
  92.     void createCSV(String featureFolder, String targetFolder, int numberOfFiles, int sizeOfSequence){
  93.  
  94.  
  95.      createDirectories(featureFolder, targetFolder);
  96.         for(Integer i=0; i <= numberOfFiles; i++){
  97.  
  98.             var list = createRandomGaussianSequence(sizeOfSequence);
  99.  
  100.  
  101.             //Save Features
  102.             createFile(i.toString(),featureFolder,list.subList(0,list.size()-2));
  103.             //Save Target
  104.             createFile(i.toString(),targetFolder,list.subList(list.size()-2,list.size()-1));
  105.  
  106.  
  107.  
  108.  
  109.         }
  110.  
  111.  
  112.  
  113.  
  114.     }
  115.  
  116. }
  117.  
RAW Paste Data