/**
  * @param command run a command from a list usually space are used as splitter
  * @return output buffered at index 0 and error buffered at index 1
  * @throws IOException if error while trying to command output
  */
 public static List<BufferedReader> run(final List<String> command) throws Exception {
   final Process process = new ProcessBuilder(command).start();
   final InputStream out = process.getInputStream();
   final InputStreamReader outr = new InputStreamReader(out, "UTF-8");
   final BufferedReader outb = new BufferedReader(outr);
   final InputStream err = process.getErrorStream();
   final InputStreamReader errr = new InputStreamReader(err, "UTF-8");
   final BufferedReader errbr = new BufferedReader(errr);
   synchronized (process) {
     try {
       process.waitFor();
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   }
   int exitStatus = process.exitValue();
   if (exitStatus != 0) {
     final StringBuilder msg = new StringBuilder();
     errbr
         .lines()
         .forEach(
             line -> {
               msg.append(line);
             });
     throw new Exception(msg.toString());
   }
   return Arrays.asList(outb, errbr);
 }
Example #2
0
  public static void createCreative(Creative creative) throws Exception {
    int status = 0;
    String jsonString = null;
    CloseableHttpClient client = HttpClientBuilder.create().build();

    HttpPost post = new HttpPost("https://rest.emaildirect.com/v1/Creatives?ApiKey=apikey");
    List<NameValuePair> params = creative.getParams();

    post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    HttpResponse response = client.execute(post);

    HttpEntity entity = response.getEntity();
    if (entity != null) {
      BufferedReader rd =
          new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

      try {
        jsonString = rd.lines().collect(Collectors.joining()).toString();
        System.out.println("JSON ->" + jsonString);
      } finally {
        rd.close();
      }
    }

    JSONObject jObj = new JSONObject(jsonString);

    if (status == 201) {
      creative.setCreativeID(jObj.getString("CreativeID"));
      creative.setCreativeTimestamp(jObj.getString("Created"));
      dbUpdate(creative);
    }
  }
 private static String readFileFromArchive(Archive archive, String path) throws IOException {
   try (InputStream manifest = archive.get(path).getAsset().openStream()) {
     BufferedReader reader =
         new BufferedReader(new InputStreamReader(manifest, StandardCharsets.UTF_8));
     return reader.lines().collect(Collectors.joining());
   }
 }
Example #4
0
  /** Count the number of lines in the file using the BufferedReader provided */
  private void exercise4() throws IOException {

    try (BufferedReader reader =
        Files.newBufferedReader(getPath("Sonnetl.txt"), StandardCharsets.UTF_8)) {
      System.out.println(reader.lines().count());
    }
  }
  public static void main(String[] args) throws InterruptedException {
    File file =
        new File(
            "C:\\Users\\chris_000\\SkyDrive\\Documents\\School\\CPSC 371 AI\\RubixCube\\results");
    File[] resultFiles = file.listFiles();

    int[] depths = new int[20];
    for (File resultFile : resultFiles) {
      if (!resultFile.getName().endsWith(".txt") || resultFile.getName().startsWith("training"))
        continue;
      try (BufferedReader br = new BufferedReader(new FileReader(resultFile))) {
        Object[] lines = br.lines().toArray();

        int size = lines.length - 1;

        depths[size]++;

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    for (int i = 0; i < 20; i++) {
      System.out.println("Depth " + i + " = " + depths[i]);
    }
  }
  /**
   * Load preferences from an input stream.
   *
   * <p>Each line is a different preference, with tab-separated fields indicating user, item, weight
   * and other information.
   *
   * @param <U> type of the users
   * @param <I> type of the items
   * @param in input stream to read from
   * @param uParser user type parser
   * @param iParser item type parser
   * @param dp double parse
   * @param uIndex user index
   * @param iIndex item index
   * @return a simple list-of-lists FastPreferenceData with the information read
   * @throws IOException when path does not exists of IO error
   */
  public static <U, I> SimpleFastPreferenceData<U, I> load(
      InputStream in,
      Parser<U> uParser,
      Parser<I> iParser,
      DoubleParser dp,
      FastUserIndex<U> uIndex,
      FastItemIndex<I> iIndex)
      throws IOException {
    AtomicInteger numPreferences = new AtomicInteger();

    List<List<IdxPref>> uidxList = new ArrayList<>();
    for (int uidx = 0; uidx < uIndex.numUsers(); uidx++) {
      uidxList.add(null);
    }

    List<List<IdxPref>> iidxList = new ArrayList<>();
    for (int iidx = 0; iidx < iIndex.numItems(); iidx++) {
      iidxList.add(null);
    }

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
      reader
          .lines()
          .forEach(
              l -> {
                CharSequence[] tokens = split(l, '\t', 4);
                U user = uParser.parse(tokens[0]);
                I item = iParser.parse(tokens[1]);
                double value;
                if (tokens.length >= 3) {
                  value = dp.parse(tokens[2]);
                } else {
                  value = dp.parse(null);
                }

                int uidx = uIndex.user2uidx(user);
                int iidx = iIndex.item2iidx(item);

                numPreferences.incrementAndGet();

                List<IdxPref> uList = uidxList.get(uidx);
                if (uList == null) {
                  uList = new ArrayList<>();
                  uidxList.set(uidx, uList);
                }
                uList.add(new IdxPref(iidx, value));

                List<IdxPref> iList = iidxList.get(iidx);
                if (iList == null) {
                  iList = new ArrayList<>();
                  iidxList.set(iidx, iList);
                }
                iList.add(new IdxPref(uidx, value));
              });
    }

    return new SimpleFastPreferenceData<>(
        numPreferences.intValue(), uidxList, iidxList, uIndex, iIndex);
  }
Example #7
0
 static {
   final StringBuilder usage = new StringBuilder();
   final BufferedReader br =
       new BufferedReader(
           new InputStreamReader(
               Flick.class.getClassLoader().getResourceAsStream(UNFLICK_USAGE_FILE)));
   br.lines().forEach(line -> usage.append(line).append("\n"));
   USAGE_FORMAT = usage.toString();
 }
  // update mongo
  public static void readAct(MongoDatabase db) {

    try (InputStream inputStream =
            CsvToMongoDb.class.getResourceAsStream("/batch/csv/activites.csv");
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
      reader
          .lines()
          .skip(1)
          .filter(line -> line.length() > 0)
          .map(line -> line.split("\",\""))
          .forEach(
              columns -> {
                // System.out.println("Une ligne");
                String codeInsee =
                    columns[0].matches("\".*\"")
                        ? columns[0].substring(1, columns[0].length() - 1)
                        : columns[0];
                String nomCommune =
                    columns[1].matches("\".*\"")
                        ? columns[1].substring(1, columns[1].length() - 1)
                        : columns[1];
                String numeroEquipment =
                    columns[2].matches("\".*\"")
                        ? columns[2].substring(1, columns[2].length() - 1)
                        : columns[2];
                String nbEquiIdent =
                    columns[3].matches("\".*\"")
                        ? columns[3].substring(1, columns[3].length() - 1)
                        : columns[3];
                String activiteCode =
                    columns[4].matches("\".*\"")
                        ? columns[4].substring(1, columns[4].length() - 1)
                        : columns[4];
                String nomActivite =
                    columns[5].matches("\".*\"")
                        ? columns[5].substring(1, columns[5].length() - 1)
                        : columns[5];

                System.out.println("activiteCode = " + activiteCode);
                System.out.println("activityNom = " + nomActivite);
                // System.out.println("numeroFicheEqui = " + numeroEquipment);

                Document search =
                    new Document(
                        "equipements",
                        new Document("$elemMatch", new Document("numero", numeroEquipment)));
                Document update =
                    new Document("$push", new Document("equipements.$.activites", nomActivite));

                db.getCollection("installations").updateOne(search, update);
              });

    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }
Example #9
0
  public static void main(String[] args) throws IOException {
    try (Stream<Path> stream = Files.list(Paths.get(""))) {
      String joined =
          stream
              .map(String::valueOf)
              .filter(path -> !path.startsWith("."))
              .sorted()
              .collect(Collectors.joining("; "));
      System.out.println("List: " + joined);
    }

    Path start = Paths.get("");
    int maxDepth = 5;
    try (Stream<Path> stream =
        Files.find(start, maxDepth, (path, attr) -> String.valueOf(path).endsWith(".java"))) {
      String joined = stream.sorted().map(String::valueOf).collect(Collectors.joining("; "));
      System.out.println("Found: " + joined);
    }

    try (Stream<Path> stream = Files.walk(start, maxDepth)) {
      String joined =
          stream
              .map(String::valueOf)
              .filter(path -> path.endsWith(".java"))
              .sorted()
              .collect(Collectors.joining("; "));
      System.out.println("walk(): " + joined);
    }

    List<String> lines = Files.readAllLines(Paths.get("src/golf.sh"));
    lines.add("puts 'foobar'");
    Path path = Paths.get("src/golf-modified.sh");
    Files.write(path, lines);

    try (Stream<String> stream = Files.lines(path)) {
      stream.filter(line -> line.contains("puts")).map(String::trim).forEach(System.out::println);
    }

    System.out.println("a" == "a");
    System.out.println("a" != new String("a"));
    System.out.println(null != "a");
    System.out.println("a".equals("a"));

    try (BufferedReader reader = Files.newBufferedReader(path)) {
      while (reader.ready()) System.out.println(reader.readLine());
    }

    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("hello-world.sh"))) {
      writer.write("puts 'Hello world'");
    }

    try (BufferedReader reader = Files.newBufferedReader(path)) {
      long countPuts = reader.lines().filter(line -> line.contains("put")).count();
      System.out.println(countPuts);
    }
  }
Example #10
0
 /**
  * Returns the total count of combinations of bottles to fill the given amount of liters.
  *
  * @param target Amount of liters to fill.
  * @return Total count of combinations of bottles to fill the given amount of liters.
  * @throws IOException
  */
 private static long numberOfCombinations(int target) throws IOException {
   try (BufferedReader br =
       new BufferedReader(
           new InputStreamReader(Day17.class.getResourceAsStream("/17-input.txt")))) {
     List<Integer> bottles = br.lines().map(Integer::valueOf).collect(toList());
     return combinationsMultiset(bottles)
         .filter(l -> l.stream().mapToInt(Integer::intValue).sum() == target)
         .count();
   }
 }
Example #11
0
 public List<String> loadAnagramsFromWeb() throws IOException {
   List<String> listOfWords = new ArrayList<>();
   URL url =
       new URL("https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt");
   URLConnection connection = url.openConnection();
   try (BufferedReader reader =
       new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
     listOfWords = reader.lines().collect(Collectors.toList());
   }
   return listOfWords;
 }
Example #12
0
 /**
  * Using the BufferedReader to access the file, create a list of words with no duplicates
  * contained in the file. Print the words.
  *
  * <p>HINT: A regular expression, WORD_REGEXP, is already defined for your use.
  */
 private void exercise5() throws IOException {
   try (BufferedReader reader =
       Files.newBufferedReader(getPath("Sonnetl.txt"), StandardCharsets.UTF_8)) {
     reader
         .lines()
         //              .flatMap(line -> Stream.of(line.split(WORD_REGEXP)))
         .flatMap(WORD_PATTERN::splitAsStream)
         .distinct()
         .forEach(System.out::println);
   }
 }
Example #13
0
 private static int calculateWith(Calculator<BoxDimensions> calculator)
     throws IOException, FileNotFoundException {
   try (BufferedReader reader =
       new BufferedReader(new FileReader("src/main/resources/input.txt"))) {
     return reader
         .lines()
         .map(s -> new StringTokenizer(s, "x"))
         .map(Main::createBoxDimensions)
         .map(calculator::calculate)
         .reduce(0, (a, b) -> a + b);
   }
 }
Example #14
0
 /**
  * Using the BufferedReader to access the file create a list of words from the file, converted to
  * lower-case and with duplicates removed, which is sorted by natural order. Print the contents of
  * the list.
  */
 private void exercise6() throws IOException {
   try (BufferedReader reader =
       Files.newBufferedReader(getPath("Sonnetl.txt"), StandardCharsets.UTF_8)) {
     reader
         .lines()
         .flatMap(WORD_PATTERN::splitAsStream)
         .map(word -> word.toLowerCase())
         .distinct()
         .sorted()
         .forEach(System.out::println);
   }
 }
Example #15
0
 /**
  * Returns the total count of combinations of the minimal amount of bottles to fill the given
  * amount of liters.
  *
  * @param target Amount of liters to fill.
  * @return Total count of combinations of the minimal amount of bottles to fill the given amount
  *     of liters.
  * @throws IOException
  */
 private static long numberOfMinimalCombinations(int target) throws IOException {
   try (BufferedReader br =
       new BufferedReader(
           new InputStreamReader(Day17.class.getResourceAsStream("/17-input.txt")))) {
     List<Integer> bottles = br.lines().map(Integer::valueOf).collect(toList());
     List<List<Integer>> combis =
         combinationsMultiset(bottles)
             .filter(l -> l.stream().mapToInt(Integer::intValue).sum() == target)
             .collect(toList());
     int minContainers = combis.stream().min(comparingInt(List::size)).get().size();
     return combis.stream().filter(l -> l.size() == minContainers).count();
   }
 }
Example #16
0
 /** Modify exercise6 so that the words are sorted by length */
 private void exercise7() throws IOException {
   try (BufferedReader reader =
       Files.newBufferedReader(getPath("Sonnetl.txt"), StandardCharsets.UTF_8)) {
     reader
         .lines()
         .flatMap(WORD_PATTERN::splitAsStream)
         .map(word -> word.toLowerCase())
         //              .sorted((s1, s2) -> s1.length() - s2.length())
         .sorted(Comparator.comparingInt(String::length))
         .distinct()
         .forEach(System.out::println);
   }
 }
Example #17
0
  public static void main(String[] args) throws IOException {
    try (BufferedReader reader = new BufferedReader(new FileReader("matrix_test"))) {
      // read table from file
      List<String[]> rows =
          reader.lines().map(line -> line.split(" ")).collect(Collectors.toList());
      double[][] values = new double[rows.size()][rows.get(0).length];
      for (int i = 0; i < values.length; i++) {
        String[] row = rows.get(i);
        for (int j = 0; j < values[0].length; j++) {
          values[i][j] = Double.parseDouble(row[j]);
        }
      }

      DoubleMatrix matrix = new DoubleMatrix(values);
      Vector coordVector = new Vector(matrix.getRows()); // 000..00 (n-times)
      while (coordVector.hasNextVector()) {
        coordVector.nextVector(); // get next vector (eg 000..00 -> 000..01)
        DoubleMatrix coord = coordVector.row();
        DoubleMatrix codeWord = coord.mmul(matrix); // m * G
        modi(codeWord, 2);
        String prefix = "0 0 0 0 1 ";
        String word = MatrixUtils.toString(codeWord);
        if (word.startsWith(prefix)) {
          System.out.println(word);
        }
      }

      /*// find syndromes
      Syndrome syndrome = new Syndrome(new DoubleMatrix(values));
      syndrome.build();

      // generate xls file with table
      HSSFWorkbook wb = new HSSFWorkbook();
      HSSFSheet sheet = wb.createSheet("Syndrome table");
      int rowNumber = 0;

      Vector syndromeVector = new Vector(values.length);
      putValue(sheet, rowNumber++, syndromeVector.toString(),
              syndrome.getErrorVector(syndromeVector.toString()));
      while (syndromeVector.hasNextVector()) {
          syndromeVector.nextVector();
          putValue(sheet, rowNumber++, syndromeVector.toString(),
                  syndrome.getErrorVector(syndromeVector.toString()));
      }

      try (FileOutputStream fileOut = new FileOutputStream("syndrome_table.xls")) {
          wb.write(fileOut);
      }*/
    }
  }
Example #18
0
  /** @param args */
  public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    /**
     * @param args the command line arguments
     * @throws IOException
     */
    File myFile = new File("myOutputFile.txt");
    // PrintWriter outputFile;
    BufferedReader inputFile;
    Scanner keybd = new Scanner(System.in);
    String inputString;
    int inputInt;
    int c = 1;

    while (!myFile.exists()) {
      System.out.print(myFile.getName() + " does NOT EXIST. New name: ");
      inputString = keybd.nextLine();
      myFile = new File(inputString);
    }

    inputFile = new BufferedReader(new FileReader(myFile));
    /*
    while( myFile.exists() ) {
    	System.out.print( myFile.getName() + " exists. New name: " );
    	inputString = keybd.nextLine();
    	myFile = new File( inputString );
    }

    // outputFile = new PrintWriter( myFile );
     */

    inputFile.mark(256);
    // do something silly
    while (inputFile.ready()) {
      inputString = inputFile.readLine();
      System.out.println("line [" + c++ + "] = " + inputString);
    }

    inputFile.reset();

    Stream<String> newStream;
    newStream = inputFile.lines();

    newStream.forEach(s -> System.out.println(s));

    inputFile.close();
  }
Example #19
0
  // update mongo
  public static void readEquip(MongoDatabase db) {
    // MongoCollection equipCollection = db.getCollection("equipements");
    try (InputStream inputStream =
            CsvToMongoDb.class.getResourceAsStream("/batch/csv/equipements.csv");
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
      reader
          .lines()
          .skip(1)
          .filter(line -> line.length() > 0)
          .map(line -> line.split(","))
          .forEach(
              columns -> {
                String instNumero = columns[2];
                System.out.println("instNumero = " + instNumero);
                String numero = columns[4];
                System.out.println("numero = " + numero);
                String nom = columns[5];
                System.out.println("nom = " + nom);
                String type = columns[7];
                System.out.println("type = " + type);
                String famille = columns[9];
                System.out.println("famille = " + famille);

                Document searchQuery = new Document();
                searchQuery.put("_id", instNumero);

                Document equipArDoc = new Document();

                equipArDoc.put(
                    "$push",
                    new Document(
                        "equipements",
                        new Document()
                            .append("numero", numero)
                            .append("nom", nom)
                            .append("type", type)
                            .append("famille", famille)));

                db.getCollection("installations").updateOne(searchQuery, equipArDoc);
              });
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }
 public String generateSummaryMail(
     List<BlogPost> posts,
     List<Blog> blogsAddedSinceLastNewsletter,
     int numberOfDaysBackInThePast) {
   try {
     BufferedReader bufferedReader =
         new BufferedReader(new InputStreamReader(blogsSummaryTemplate.getInputStream(), "UTF-8"));
     String templateContent =
         Joiner.on("\n").join(bufferedReader.lines().collect(Collectors.toList()));
     StringTemplate template = new StringTemplate(templateContent);
     template.setAttribute("days", numberOfDaysBackInThePast);
     template.setAttribute(
         "newPosts", posts.stream().map(BlogPostForMailItem::new).collect(Collectors.toList()));
     template.setAttribute(
         "personToBlogHomepage", getPersonToBlogHomepage(blogsAddedSinceLastNewsletter));
     return template.toString();
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Example #21
0
  public static Boolean isServiceRunning(String service) {
    // to check whether service is running or not
    String[] scriptIsRuning = {
      "cmd.exe", "/c", "sc", "query", service, "|", "find", "/C", "\"RUNNING\""
    };
    try {
      Process process = Runtime.getRuntime().exec(scriptIsRuning);

      InputStream inputStream = process.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      if ("0".equalsIgnoreCase(bufferedReader.lines().findFirst().get())) {
        return Boolean.FALSE;
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

    return Boolean.TRUE;
  }
  public static Matrix loadFromFile(File file) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    int dimensionSize = 0;
    Stream<String> lines = reader.lines();
    int objectCount = (int) lines.count();
    reader.close();

    reader = new BufferedReader(new FileReader(file));
    String[] temp = reader.readLine().split(" ");
    double[][] objects = new double[objectCount][temp.length];

    for (int i = 0; i < objectCount; i++) {
      if (i != 0) temp = reader.readLine().split(" ");

      for (int j = 0; j < temp.length; j++) {
        objects[i][j] = Double.parseDouble(temp[j]);
      }
    }

    reader.close();
    return new Matrix(objects);
  }
Example #23
0
  public static void main(String[] args) {
    List<String> lines = Arrays.asList("hallo daar", "aap noot mies", "NL niet naar EK");
    final Path path = Paths.get("lines.txt");

    try (BufferedWriter bw = Files.newBufferedWriter(path);
        PrintWriter pw = new PrintWriter(bw); ) {

      lines.stream().map(String::toUpperCase).forEach(pw::println);

    } catch (IOException ex) {
      System.err.println("Failed. " + ex);
    }

    try (BufferedReader br = Files.newBufferedReader(path); ) {

      br.lines().forEach(System.out::println);

    } catch (IOException ex) {
      System.err.println("Failed. " + ex);
    }

    System.out.println("Aantal processors: " + Runtime.getRuntime().availableProcessors());
  }
 private String getDocumentAsString(DocumentEntryType docEntry) throws IOException {
   InputStream inputStream = getDocumentAsInputStream(docEntry);
   try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
     return buffer.lines().collect(Collectors.joining("\n"));
   }
 }
Example #25
0
  /** @param arguments String[] */
  public static void main(final String[] arguments) {
    // if (System.getSecurityManager() == null)
    // {
    // System.setSecurityManager(new SecurityManager());
    // }

    System.out.println("Example for the Callback pattern");
    System.out.println("This code will run two RMI objects to demonstrate");
    System.out.println(" callback capability. One will be CallbackClientImpl,");
    System.out.println(" which will request a project from the other remote");
    System.out.println(" object, CallbackServerImpl.");
    System.out.println("To demonstrate how the Callback pattern allows the");
    System.out.println(" client to perform independent processing, the main");
    System.out.println(" progam thread will go into a wait loop until the");
    System.out.println(" server sends the object to its client.");
    System.out.println();

    System.out.println("Running the RMI compiler (rmic)");
    System.out.println();

    try {
      Process process =
          Runtime.getRuntime()
              .exec(
                  "rmic -d target/classes -classpath target/classes callback.CallbackServerImpl callback.CallbackClientImpl");

      try (BufferedReader reader =
          new BufferedReader(new InputStreamReader(process.getInputStream()))) {
        reader.lines().forEach(System.out::println);
      }

      System.out.println();
      System.out.println("rmic exit value: " + process.waitFor());
    } catch (IOException exc) {
      System.err.println("Unable to run rmic utility. Exiting application.");
      System.exit(1);
    } catch (InterruptedException exc) {
      System.err.println("Threading problems encountered while using the rmic utility.");
    }

    System.out.println();
    System.out.println("Starting the rmiregistry");
    System.out.println();

    try {
      // Process rmiProcess =
      Runtime.getRuntime().exec("rmiregistry");
      Thread.sleep(15000);
    } catch (IOException exc) {
      System.err.println("Unable to start the rmiregistry. Exiting application.");
      System.exit(1);
    } catch (InterruptedException exc) {
      System.err.println("Threading problems encountered when starting the rmiregistry.");
    }

    System.out.println("Creating the client and server objects");
    System.out.println();

    new CallbackServerImpl();
    CallbackClientImpl callbackClient = new CallbackClientImpl();

    System.out.println("CallbackClientImpl requesting a project");
    callbackClient.requestProject("New Java Project");

    try {
      while (!callbackClient.isProjectAvailable()) {
        System.out.println("Project not available yet; sleeping for 2 seconds");
        Thread.sleep(2000);
      }
    } catch (InterruptedException exc) {
      // Ignore
    }

    System.out.println("Project retrieved: " + callbackClient.getProject());
  }
Example #26
0
  // insert into MongoDB(DBobject)
  public static void readInstallations(MongoDatabase db) {
    MongoCollection collection = db.getCollection("installations");
    // clean
    collection.drop();

    try (InputStream inputStream =
            CsvToMongoDb.class.getResourceAsStream("/batch/csv/installations.csv");
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {

      MongoCollection<Document> coll = db.getCollection("installations");

      reader
          .lines()
          .skip(1)
          .filter(line -> line.length() > 0)
          .map(line -> line.split("\",\""))
          .forEach(
              columns -> {
                System.out.println("Une ligne");

                // String nom =
                // columns[0].matches("\".*\"")?columns[0].substring(2,columns[0].length()-1):columns[0];

                // String nom =
                // columns[0].matches("\".*\"")?columns[0].substring(1,columns[0].length()-1):columns[0];
                // System.out.println(nom);
                String nom = columns[0].substring(1, columns[0].length());
                System.out.println("nom = " + nom);
                String numeroInstall =
                    columns[1].matches("\".*\"")
                        ? columns[1].substring(1, columns[1].length() - 1)
                        : columns[1];
                System.out.println("numeroInstall = " + numeroInstall);
                String nomCommune =
                    columns[2].matches("\".*\"")
                        ? columns[2].substring(1, columns[2].length() - 1)
                        : columns[2];
                String codeInsee =
                    columns[3].matches("\".*\"")
                        ? columns[3].substring(1, columns[3].length() - 1)
                        : columns[3];
                String codePostal =
                    columns[4].matches("\".*\"")
                        ? columns[4].substring(1, columns[4].length() - 1)
                        : columns[4];
                String lieuDit =
                    columns[5].matches("\".*\"")
                        ? columns[5].substring(1, columns[5].length() - 1)
                        : columns[5];
                String numeroVoie =
                    columns[6].matches("\".*\"")
                        ? columns[6].substring(1, columns[6].length() - 1)
                        : columns[6];
                String nomVoie =
                    columns[7].matches("\".*\"")
                        ? columns[7].substring(1, columns[7].length() - 1)
                        : columns[7];
                String location =
                    columns[8].matches("\".*\"")
                        ? columns[8].substring(1, columns[8].length() - 1)
                        : columns[8];
                String longitude =
                    columns[9].matches("\".*\"")
                        ? columns[9].substring(1, columns[9].length() - 1)
                        : columns[9];
                String latitude =
                    columns[10].matches("\".*\"")
                        ? columns[10].substring(1, columns[10].length() - 1)
                        : columns[10];
                String accessibilite =
                    columns[11].matches("\".*\"")
                        ? columns[11].substring(1, columns[11].length() - 1)
                        : columns[11];
                String mobiliteReduite =
                    columns[12].matches("\".*\"")
                        ? columns[12].substring(1, columns[12].length() - 1)
                        : columns[12];
                String handicapesSensoriels =
                    columns[13].matches("\".*\"")
                        ? columns[13].substring(1, columns[13].length() - 1)
                        : columns[13];
                String empriseFonciere =
                    columns[14].matches("\".*\"")
                        ? columns[14].substring(1, columns[14].length() - 1)
                        : columns[14];
                String logementGardien =
                    columns[15].matches("\".*\"")
                        ? columns[15].substring(1, columns[15].length() - 1)
                        : columns[15];
                String multiCommune =
                    columns[16].matches("\".*\"")
                        ? columns[16].substring(1, columns[16].length() - 1)
                        : columns[16];
                String placeParking =
                    columns[17].matches("\".*\"")
                        ? columns[17].substring(1, columns[17].length() - 1)
                        : columns[17];
                String parkingHandicapes =
                    columns[18].matches("\".*\"")
                        ? columns[18].substring(1, columns[18].length() - 1)
                        : columns[18];
                String installPart =
                    columns[19].matches("\".*\"")
                        ? columns[19].substring(1, columns[19].length() - 1)
                        : columns[19];
                String metro =
                    columns[20].matches("\".*\"")
                        ? columns[20].substring(1, columns[20].length() - 1)
                        : columns[20];
                String bus =
                    columns[21].matches("\".*\"")
                        ? columns[21].substring(1, columns[21].length() - 1)
                        : columns[21];
                String tram =
                    columns[22].matches("\".*\"")
                        ? columns[22].substring(1, columns[22].length() - 1)
                        : columns[22];
                String train =
                    columns[23].matches("\".*\"")
                        ? columns[23].substring(1, columns[23].length() - 1)
                        : columns[23];
                String bateau =
                    columns[24].matches("\".*\"")
                        ? columns[24].substring(1, columns[24].length() - 1)
                        : columns[24];
                String autre =
                    columns[25].matches("\".*\"")
                        ? columns[25].substring(1, columns[25].length() - 1)
                        : columns[25];
                String nbEquip =
                    columns[26].matches("\".*\"")
                        ? columns[26].substring(1, columns[26].length() - 1)
                        : columns[26];
                String nbFichesEquip =
                    columns[27].matches("\".*\"")
                        ? columns[27].substring(1, columns[27].length() - 1)
                        : columns[27];
                String dateMaj =
                    columns[28].matches("\".*\"")
                        ? columns[28].substring(1, columns[28].length() - 1)
                        : columns[28];

                // Double [] coords = new Double [2];
                // coords[0] = Double.valueOf(latitude);
                // coords[1] = Double.valueOf(longitude);

                // System.out.println(coords[0]);

                Document doc =
                    new Document("_id", numeroInstall)
                        .append("nom", nom)
                        .append(
                            "adresse",
                            new Document()
                                .append("commune", nomCommune)
                                .append("numero", numeroVoie)
                                .append("voie", nomVoie)
                                .append("lieuDit", lieuDit)
                                .append("codePostal", codePostal))
                        .append(
                            "location",
                            new Document("type", "Point")
                                .append(
                                    "coordinates",
                                    Arrays.asList(
                                        Double.valueOf(columns[9]), Double.valueOf(columns[10]))))
                        .append("multiCommune", multiCommune.equals("Oui") ? true : false)
                        .append("nbPlacesParking", placeParking)
                        .append("nbPlacesParkingHandicapes", parkingHandicapes);
                // System.out.println(doc.toJson());
                // doc.put("equipements", new ArrayList<Document>());
                coll.insertOne(doc);
              });
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }
 private static String read(InputStream input) throws IOException {
   try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input))) {
     return buffer.lines().collect(Collectors.joining("\n"));
   }
 }
Example #28
0
  public static void main(String[] args) {

    String title =
        "\n"
            + "      _   __                     ___                \n"
            + "     / | / /__ _      _______   /   |  ____  ____   \n"
            + "    /  |/ / _ \\ | /| / / ___/  / /| | / __ \\/ __ \\  \n"
            + "   / /|  /  __/ |/ |/ (__  )  / ___ |/ /_/ / /_/ /  \n"
            + "  /_/ |_/\\___/|__/|__/____/  /_/  |_/ .___/ .___/   \n"
            + "                                   /_/   /_/        \n";

    System.out.println(title);
    System.out.flush();

    if (args.length != 0 && (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("h"))) {
      InputStream stream = Application.class.getResourceAsStream("/help.txt");
      BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
      reader.lines().forEach(System.out::println);
      System.out.flush();
      return;
    }

    System.out.println("Starting Up...");

    try {
      props = new PropertyConfig();
    } catch (Exception e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
      System.exit(-1);
    }

    Logging.setUp();
    log = Logging.registerLogger(Application.class.getName());
    Logging.registerLogger("");
    Logging.registerLogger("org.hibernate", Level.WARNING);

    log.info("Initializing Entity Manager factory...");
    log.info("Persistence Unit: " + props.getProp("PERSISTENCE_UNIT"));
    persistenceManager = new PersistenceManager(props.getProp("PERSISTENCE_UNIT"));

    if (args.length != 0 && args[0].equals("multilevel-clustering")) {

      String clusteringName = args[1];
      double metaNewsThreshold = Double.parseDouble(args[2]);
      double newsThreshold = Double.parseDouble(args[3]);
      String tfDictionaryName = args[4];
      String languageString = args[5];

      try {
        MultilevelIncrementalClusteringTask.execTask(
            clusteringName, metaNewsThreshold, newsThreshold, tfDictionaryName, languageString);
      } catch (Exception e) {
        e.printStackTrace();
        Application.tearDown();
        System.exit(-1);
      }

      Application.tearDown();
      System.exit(0);
    } else if (args.length != 0 && args[0].equals("multilevel-performance")) {

      double metaNewsThresholdSeed = Double.parseDouble(args[1]);
      int metaNewsThresholdLimit = Integer.parseInt(args[2]);
      double newsThresholdSeed = Double.parseDouble(args[3]);
      int newsThresholdLimit = Integer.parseInt(args[4]);
      String tfDictionaryName = args[5];
      String languageString = args[6];

      try {
        MultilevelClusteringPerformanceTask.execTask(
            metaNewsThresholdSeed,
            metaNewsThresholdLimit,
            newsThresholdSeed,
            newsThresholdLimit,
            tfDictionaryName,
            languageString);
      } catch (Exception e) {
        e.printStackTrace();
        Application.tearDown();
        System.exit(-1);
      }

      Application.tearDown();
      System.exit(0);

    } else if (args.length != 0 && args[0].equals("generate-tf-dictionary")) {

      String dictionaryName = args[1];
      String languageString = args[2];

      if (dictionaryName == null || dictionaryName.length() == 0) {
        System.err.println("Please provide a valid name for TF Dictionary");
        Application.tearDown();
        System.exit(-1);
      }

      int returnCode = -1;

      try {
        TFDictionaryGenerationTask.generateDictionary(dictionaryName, languageString);
        returnCode = 0;
      } catch (Exception e) {
        e.printStackTrace();
        returnCode = -1;
      } finally {
        Application.tearDown();
        System.exit(returnCode);
      }

    } else if (args.length != 0 && args[0].equals("load-words-list")) {

      String filePath = args[1];
      String languageString = args[2];

      if (!Files.exists(Paths.get(filePath))) {
        System.err.println("File does not exists.");
        Application.tearDown();
        System.exit(-1);
      }

      Language language = null;
      try {
        language = Language.valueOf(languageString);
      } catch (Exception e) {
        System.err.println(e.getMessage());
        Application.tearDown();
        System.exit(-1);
      }

      EntityManager em = Application.createEntityManager();

      em.getTransaction().begin();

      NoiseWordsList old =
          em.createQuery(
                  "select l from NoiseWordsList l where l.language=:lang", NoiseWordsList.class)
              .setParameter("lang", language)
              .getSingleResult();
      em.remove(old);

      NoiseWordsList list = new NoiseWordsList();
      list.setDescription(languageString);
      list.setLanguage(language);

      try {
        Files.lines(Paths.get(filePath))
            .forEach(
                w -> {
                  list.getWords().add(w.trim());
                });
      } catch (IOException e) {
        System.err.println("Cannot open file.");
        e.printStackTrace();
        Application.tearDown();
        System.exit(-1);
      }

      em.persist(list);

      em.getTransaction().commit();
      em.close();

      System.out.println("Task completed successfully.");
      Application.tearDown();
      System.exit(0);

    } else if (args.length != 0 && args[0].equals("add-user")) {
      if (args.length < 4) {
        System.err.println("Wrong command syntax, missing arguments.");
        Application.tearDown();
        System.exit(-1);
      }

      String username = args[1];
      String password = args[2];
      String role = args[3];

      if (!role.equalsIgnoreCase("admin") && !role.equalsIgnoreCase("guest")) {
        System.err.println("Role can be one of [admin, guest]");
        Application.tearDown();
        System.exit(-1);
      }

      EntityManager em = Application.createEntityManager();

      User user = null;
      try {
        user =
            em.createQuery("select u from User u where u.username=:uname", User.class)
                .setParameter("uname", username)
                .getSingleResult();
      } catch (NoResultException e) {
        user = new User();
        user.setEmail("");
      }

      user.setUsername(username);
      user.setPassword(password);
      user.setRole(role.toLowerCase());

      em.getTransaction().begin();
      em.persist(user);
      em.getTransaction().commit();

      System.out.println("User successfully created.");
      Application.tearDown();
      System.exit(0);
    }

    log.info("Setting up Scheduler");
    Task.setLogger(Logging.registerLogger("it.fcambi.news.Tasks"));
    configureScheduler();

    log.info("Starting up Web Services...");
    log.info("Bind url >> " + props.getProp("BIND_URI"));
    httpServer = new Server(props);
    httpServer.startServer();

    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              @Override
              public void run() {
                Application.tearDown();
              }
            });

    configureArticleDownloaderTask();

    log.info("Startup Completed - All OK");

    System.out.println(
        "\nApplication Ready\n\t- Server @ "
            + props.getProp("BIND_URI")
            + "\n\t- GUI @ "
            + props.getProp("BIND_URI")
            + "/gui/app/\n");
    System.out.println("Press CTRL+C to stop...");

    try {
      Thread.currentThread().join();
    } catch (Exception e) {
      log.log(Level.SEVERE, "Error joining current thread.", e);
      System.exit(-1);
    }
  }
Example #29
0
  /**
   * extract technical terms using C-value method
   *
   * @param phraseFile
   * @throws IOException
   */
  public static void extractTerm(String phraseFile) throws IOException {

    logger.log(Level.INFO, "*********Collecting and cleaning candidates. Please wait...");

    HashMap<Candidate, Integer> map =
        new HashMap<Candidate, Integer>(); // map candiates and their frequency
    String line = "";
    int percentComplete1 = 0;
    int i = 0;
    try {
      BufferedReader br = new BufferedReader(new FileReader(phraseFile));
      long size = br.lines().count();
      br = new BufferedReader(new FileReader(phraseFile));

      while ((line = br.readLine()) != null) {
        if (!line.equals("")) { // check empty line

          Candidate cand = new Candidate(line, line.split("\\s").length);

          if (map.containsKey(cand)) {
            map.put(cand, map.get(cand) + 1);
          } else map.put(cand, 1);
        }

        // reporting the progress
        i++;
        if (i * 100 / size > percentComplete1) {
          percentComplete1 = percentComplete1 + 1;
          logger.log(Level.INFO, percentComplete1 + " percent of temp candidates processed.");
        }
      }

    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    logger.log(Level.INFO, "*********Removing unfrequent noun phrases. Please wait...");
    // List<Candidate> cleanedCand = new ArrayList<>();

    Candidate[] cleanedCand = map.keySet().toArray(new Candidate[map.size()]);

    List<Candidate> candidates = new ArrayList<>();

    for (Candidate c : cleanedCand) {
      if (map.get(c) >= 50) { // ignore phrases occurring less than 50 times in the corpus
        c.incrementFreq(map.get(c));
        candidates.add(c);
      }
    }

    Document doc = new Document("C:\\", "terms.txt");
    // doc.List(tokenizedSentenceList);

    CValueAlgortithm cvalue = new CValueAlgortithm();
    cvalue.init(doc); // initializes the algorithm for processing the desired document.
    ILinguisticFilter pFilter = new AdjPrepNounFilter(); // filter
    cvalue.addNewProcessingFilter(pFilter);
    ; // for example the AdjNounFilter
    logger.log(Level.INFO, "*********Cvalue algorithm is running...");
    cvalue.setCandidates(candidates); // set candidates to the algorithm
    cvalue.runAlgorithm(); // process the CValue algorithm with the provided filters

    doc.getTermList(); // get the results
    List<Term> termList = doc.getTermList();

    logger.log(Level.INFO, "*********Terms being written...");

    PrintWriter pw2 = new PrintWriter(new FileOutputStream(termFile));
    int k = 0;
    for (Term t : termList) {
      k++;
      pw2.println(t.toString());
    }
    pw2.close();

    logger.log(Level.INFO, "Terms are saved.");

    System.out.println("Top 20 technical terms:");

    for (int l = 0; l < 21; l++) System.out.println(termList.get(l).toString());
  }
 private void openTextFile(File file) throws FileNotFoundException {
   BufferedReader reader = new BufferedReader(new FileReader(file));
   setTableData(
       reader.lines().map(line -> Arrays.asList(line.split("\\t"))).collect(Collectors.toList()));
 }