Ejemplo n.º 1
0
  private Incident extractIncidentInfo(final Incident incident) {
    // using the library of keywords to identify the important information
    final String message =
        incident.getMessage().replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("\\.", "");
    final String messageValues[] = message.split(" "); // white space delimiter

    // check if messageValues contains a keyword
    for (final String[] keyword : keywords) {
      for (int i = 0; i < messageValues.length; i++) {
        if (messageValues[i].equalsIgnoreCase(keyword[0])) {
          // System.out.println("Matched: " + messageValues[i]);

          // set the position of keyword
          keyword[1] = String.valueOf(i);

          break; // move on to the next keyword
        }
      }
    }

    // some keywords have valid positions, some don't (-1)
    // arrange the ones with valid positions in ascending order, using custom comparator
    Arrays.sort(
        keywords,
        new Comparator<String[]>() {

          @Override
          public int compare(final String[] keywordArray1, final String[] keywordArray2) {
            // get second element of array and transform it to integer
            final Integer pos1 = Integer.parseInt(keywordArray1[1]);
            final Integer pos2 = Integer.parseInt(keywordArray2[1]);

            // ascending order
            return pos1.compareTo(pos2);
          }
        });

    // print check
    int firstOccurrence = -1;

    for (int i = 0; i < keywords.length; i++) {
      final String keyword[] = keywords[i];

      // System.out.println(String.format("Value: %s,  position: %s", keyword[0], keyword[1]));

      if (Integer.parseInt(keyword[1]) > -1) {
        firstOccurrence = i;

        // System.out.println("First Occurrence:" + firstOccurrence);
        break;
      }
    }

    // retrieve important information between the lines.
    while (firstOccurrence < keywords.length) {
      // two by two
      final Integer firstPos =
          Integer.parseInt(keywords[firstOccurrence][1]); // get the first position
      Integer secondPos;

      if (firstOccurrence == (keywords.length - 1)) {
        // when keyword array reaches the last element
        // secondPos is the length of messageValues, to prevent array OOB
        secondPos = messageValues.length;
      } else {
        secondPos = Integer.parseInt(keywords[firstOccurrence + 1][1]); // get the second position
      }

      final Integer diff = (secondPos - firstPos) - 1;
      final String infoArray[] = new String[diff];

      // System.out.println(String.format("secondPos: %s,firstPos: %s, diff: %s", secondPos,
      // firstPos, diff));

      // System.out.println(String.format("> keyword: %s", keywords[firstOccurrence][0]));

      for (int i = 1; i <= diff; i++) {
        final String info = messageValues[firstPos + i];

        // System.out.println(String.format("messageValues[%s], info: %s", firstPos + i, info));

        infoArray[i - 1] = info;
      }

      // incidentHashmap assignment
      incident.getIncidentMap().put(keywords[firstOccurrence][0], infoArray);

      firstOccurrence++;
    }

    // clear keyword array back to "-1" positions
    for (final String keyword[] : keywords) {
      keyword[1] = "-1";
    }

    // System.out.println(incident.getIncidentMap());
    return incident;
  }