Exemplo n.º 1
0
  public static void printup(Hashtable table) {
    Hashtable new_table = new Hashtable();
    Hashtable confidences = new Hashtable();

    Iterator key_iter = table.keySet().iterator();
    while (key_iter.hasNext()) {
      ArrayList<Integer> keys = (ArrayList<Integer>) key_iter.next();
      if (keys.size() > 1) {
        PermutationIterable<Integer> pi = new PermutationIterable<Integer>(keys);
        for (List<Integer> lc : pi) {
          new_table.put(lc, table.get(keys));
        }
      }
    }
    // System.out.println(table+"!");
    table.putAll(new_table);
    // System.out.println(table);
    confidences = findConfidence(table);
    /*System.out.println(table);
    System.out.println("-------");
    System.out.println(confidences);*/

    Iterator key_iter2 = table.keySet().iterator();

    while (key_iter2.hasNext()) {
      ArrayList<Integer> current_value = new ArrayList<Integer>();
      current_value = (ArrayList<Integer>) key_iter2.next();

      // System.out.println(MINSUP);
      // System.out.println(MINCONF);
      if (table.get(current_value) != null && confidences.get(current_value) != null) {
        if ((Integer) table.get(current_value) >= MINSUP) {
          if (((Double) confidences.get(current_value) * 10) >= MINCONF) {
            System.out.println(
                test
                    + ":"
                    + current_value
                    + " is a valid rule with support "
                    + table.get(current_value)
                    + "(Minsupport was"
                    + MINSUP
                    + ") and confidence"
                    + (Double) confidences.get(current_value) * 100
                    + "%");
          }
        }
      }
    }
  }
  /**
   * Convert a keyword-to-values map to a LoggingEvent
   *
   * @param fieldMap
   * @param exception
   * @return logging event
   */
  private LoggingEvent convertToEvent(Map fieldMap, String[] exception) {
    if (fieldMap == null) {
      return null;
    }

    // a logger must exist at a minimum for the event to be processed
    if (!fieldMap.containsKey(LOGGER)) {
      fieldMap.put(LOGGER, "Unknown");
    }
    if (exception == null) {
      exception = emptyException;
    }

    Logger logger = null;
    long timeStamp = 0L;
    String level = null;
    String threadName = null;
    Object message = null;
    String ndc = null;
    String className = null;
    String methodName = null;
    String eventFileName = null;
    String lineNumber = null;
    Hashtable properties = new Hashtable();

    logger = Logger.getLogger((String) fieldMap.remove(LOGGER));

    if ((dateFormat != null) && fieldMap.containsKey(TIMESTAMP)) {
      try {
        timeStamp = dateFormat.parse((String) fieldMap.remove(TIMESTAMP)).getTime();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    // use current time if timestamp not parseable
    if (timeStamp == 0L) {
      timeStamp = System.currentTimeMillis();
    }

    message = fieldMap.remove(MESSAGE);
    if (message == null) {
      message = "";
    }

    level = (String) fieldMap.remove(LEVEL);
    Level levelImpl;
    if (level == null) {
      levelImpl = Level.DEBUG;
    } else {
      // first try to resolve against custom level definition map, then fall back to regular levels
      levelImpl = (Level) customLevelDefinitionMap.get(level);
      if (levelImpl == null) {
        levelImpl = Level.toLevel(level.trim());
        if (!level.equals(levelImpl.toString())) {
          // check custom level map
          if (levelImpl == null) {
            levelImpl = Level.DEBUG;
            getLogger()
                .debug(
                    "found unexpected level: "
                        + level
                        + ", logger: "
                        + logger.getName()
                        + ", msg: "
                        + message);
            // make sure the text that couldn't match a level is added to the message
            message = level + " " + message;
          }
        }
      }
    }

    threadName = (String) fieldMap.remove(THREAD);

    ndc = (String) fieldMap.remove(NDC);

    className = (String) fieldMap.remove(CLASS);

    methodName = (String) fieldMap.remove(METHOD);

    eventFileName = (String) fieldMap.remove(FILE);

    lineNumber = (String) fieldMap.remove(LINE);

    properties.put(Constants.HOSTNAME_KEY, host);
    properties.put(Constants.APPLICATION_KEY, path);
    properties.put(Constants.RECEIVER_NAME_KEY, getName());

    // all remaining entries in fieldmap are properties
    properties.putAll(fieldMap);

    LocationInfo info = null;

    if ((eventFileName != null)
        || (className != null)
        || (methodName != null)
        || (lineNumber != null)) {
      info = new LocationInfo(eventFileName, className, methodName, lineNumber);
    } else {
      info = LocationInfo.NA_LOCATION_INFO;
    }

    LoggingEvent event =
        new LoggingEvent(
            null,
            logger,
            timeStamp,
            levelImpl,
            message,
            threadName,
            new ThrowableInformation(exception),
            ndc,
            info,
            properties);

    return event;
  }
Exemplo n.º 3
0
  public static void main(String[] args) {
    num_users = 0;
    int movie_number = 0;
    MINSUP = Double.parseDouble(args[1]);
    MINCONF = Double.parseDouble(args[3]);
    MAXMOVIES = Integer.parseInt(args[5]);
    String test_raw = args[6];
    test = "";
    test = test_raw.substring(1);
    if (test.equals("comb")) {
      go_to = true;
    }

    if ((MINSUP < 0 || MINSUP > 100)
        || (MINCONF < 0 || MINCONF > 100)
        || MAXMOVIES < 0
        || test == null) {
      System.out.println("Input variables aren't correct!");
      System.exit(0);
    }
    ArrayList<User> users = new ArrayList<User>(Collections.nCopies(945, blank));
    int[] user_exists = new int[945];
    try {
      FileInputStream fstream = new FileInputStream("src/u.data"); // location of file in eclipse
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
      while ((strLine = br.readLine()) != null) {
        StringTokenizer tok = new StringTokenizer(strLine);
        String ID = tok.nextToken();
        int userID = Integer.parseInt(ID);
        String MID = tok.nextToken();
        int movieID = Integer.parseInt(MID);
        String RANK = tok.nextToken();
        int movieRating = Integer.parseInt(RANK);
        Movie current_movie = new Movie(movieID, movieRating);

        if (user_exists[userID] == 0) {
          num_users++;
          users.set(userID, new User(userID, movieID, movieRating));
          user_exists[userID] = 1;
        } else {
          users.get(userID).movies.add(current_movie);
        }
      }

      in.close();
    } catch (Exception e) {
      if (e.getMessage() != null) {
        System.err.println("Error: " + e.getMessage());
      }
    }
    try {
      FileInputStream fstream = new FileInputStream("src/u.item"); // location of file in eclipse
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
      while ((strLine = br.readLine()) != null) {
        movie_number++;
        StringTokenizer tok = new StringTokenizer(strLine, "|");
        String number = tok.nextToken();
        int movieNumber = Integer.parseInt(number);
        String movieName = tok.nextToken();
        for (int i = 0; i < users.size(); i++) {
          for (int j = 0; j < users.get(i).movies.size(); j++) {
            if (users.get(i).movies.get(j).movieID == movieNumber) {
              users.get(i).movies.get(j).setName(movieName);
            }
          }
        }
      }

      in.close();
    } catch (Exception j) {
      System.err.println("Error: " + j.getMessage());
    }

    // System.out.println("Num users is "+num_users);
    if (test.equals("pos")) {
      for (int user_num = 1; user_num < num_users + 1; user_num++) {
        for (int usermovies = 0; usermovies < users.get(user_num).movies.size(); usermovies++) {
          if (users.get(user_num).movies.get(usermovies).ranking <= 3) {
            users.get(user_num).movies.remove(usermovies);
            usermovies--;
          }
        }
      }
      MINSUP *= num_users;
      MINCONF *= 10;
      System.out.println(MINSUP);
      System.out.println(MINCONF);
      ArrayList<Movie> users_movies = new ArrayList<Movie>();
      Hashtable table = new Hashtable();
      Hashtable master_table = new Hashtable();
      for (int i = 1; i <= num_users; i++) {
        for (int k = 0; k < users.get(i).movies.size(); k++) {
          ArrayList<Integer> add = new ArrayList<Integer>();
          add.add(users.get(i).movies.get(k).movieID);
          // System.out.println(users.get(i).movies.get(k).movieID+ " is the current movie from user
          // "+i);

          // if(users.get(i).movies.get(k).ranking>3){
          if (table.containsKey(add)) {
            int table_value = (Integer) table.get(add);
            table_value++;
            table.put(add, table_value);
            // System.out.println("added 1 to the existing");
          } else {
            table.put(add, 1);
            // System.out.println("added a new");
          }
          // }
        }
      }
      System.out.println(table);
      Iterator final_iter = table.keySet().iterator();
      while (final_iter.hasNext()) {
        ArrayList<Integer> next = (ArrayList<Integer>) final_iter.next();
        // System.out.println(t.get(next));
        // System.out.println(MINSUP);
        if ((Integer) table.get(next) < MINSUP) {
          // System.out.println("hi");
          final_iter.remove();
        }
      }
      // System.out.println(table);
      master_table.putAll(table);
      // System.out.println(master_table);
      for (int i = 2; i <= MAXMOVIES; i++) {
        table = find_combos(table, users, i);
        // System.out.println("table generated is "+table);
        if (!table.isEmpty()) {
          master_table.putAll(table);
        }
        // System.out.println(master_table);
      }
      // System.out.println(master_table+" is before print_up");
      printup(master_table);
    }
    if (test.equals("neg")) {
      for (int user_num = 1; user_num < num_users + 1; user_num++) {
        for (int usermovies = 0; usermovies < users.get(user_num).movies.size(); usermovies++) {
          if (users.get(user_num).movies.get(usermovies).ranking >= 3) {
            users.get(user_num).movies.remove(usermovies);
            usermovies--;
          }
        }
      }
      MINSUP *= num_users;
      MINCONF *= 10;
      ArrayList<Movie> users_movies = new ArrayList<Movie>();
      Hashtable table = new Hashtable();
      Hashtable master_table = new Hashtable();
      for (int i = 1; i <= num_users; i++) {
        for (int k = 0; k < users.get(i).movies.size(); k++) {
          ArrayList<Integer> add = new ArrayList<Integer>();
          add.add(users.get(i).movies.get(k).movieID);
          // System.out.println(users.get(i).movies.get(k).movieID+ " is the current movie from user
          // "+i);

          // if(users.get(i).movies.get(k).ranking>3){
          if (table.containsKey(add)) {
            int table_value = (Integer) table.get(add);
            table_value++;
            table.put(add, table_value);
            // System.out.println("added 1 to the existing");
          } else {
            table.put(add, 1);
            // System.out.println("added a new");
          }
          // }
        }
      }
      // System.out.println(table);
      Iterator final_iter = table.keySet().iterator();
      while (final_iter.hasNext()) {
        ArrayList<Integer> next = (ArrayList<Integer>) final_iter.next();
        // System.out.println(t.get(next));
        // System.out.println(MINSUP);
        if ((Integer) table.get(next) < MINSUP) {
          // System.out.println("hi");
          final_iter.remove();
        }
      }
      // System.out.println(table);
      master_table.putAll(table);
      // System.out.println(master_table);
      for (int i = 2; i <= MAXMOVIES; i++) {
        table = find_combos(table, users, i);
        if (!table.isEmpty()) {
          master_table.putAll(table);
        }
      }
      // System.out.println(master_table+" is before print_up");
      printup(master_table);
    }
    if (test.equals("comb")) {
      for (int user_num = 1; user_num < num_users + 1; user_num++) {
        for (int usermovies = 0; usermovies < users.get(user_num).movies.size(); usermovies++) {
          if (users.get(user_num).movies.get(usermovies).ranking >= 3) {
            users.get(user_num).movies.remove(usermovies);
            usermovies--;
          }
        }
      }
      MINSUP *= num_users;
      MINCONF *= 10;
      ArrayList<Movie> users_movies = new ArrayList<Movie>();
      Hashtable table = new Hashtable();
      Hashtable master_table = new Hashtable();
      for (int i = 1; i <= num_users; i++) {
        for (int k = 0; k < users.get(i).movies.size(); k++) {
          ArrayList<Integer> add = new ArrayList<Integer>();
          add.add(users.get(i).movies.get(k).movieID);
          // System.out.println(users.get(i).movies.get(k).movieID+ " is the current movie from user
          // "+i);

          // if(users.get(i).movies.get(k).ranking>3){
          if (table.containsKey(add)) {
            int table_value = (Integer) table.get(add);
            table_value++;
            table.put(add, table_value);
            // System.out.println("added 1 to the existing");
          } else {
            table.put(add, 1);
            // System.out.println("added a new");
          }
          // }
        }
      }
      // System.out.println(table);
      Iterator final_iter = table.keySet().iterator();
      while (final_iter.hasNext()) {
        ArrayList<Integer> next = (ArrayList<Integer>) final_iter.next();
        // System.out.println(t.get(next));
        // System.out.println(MINSUP);
        if ((Integer) table.get(next) < MINSUP) {
          // System.out.println("hi");
          final_iter.remove();
        }
      }
      // System.out.println(table);
      master_table.putAll(table);
      // System.out.println(master_table);
      for (int i = 2; i <= MAXMOVIES; i++) {
        table = find_combos(table, users, i);
        if (!table.isEmpty()) {
          master_table.putAll(table);
        }
      }
      // System.out.println(master_table+" is before print_up");
      test = "neg";
      printup(master_table);
      test = "comb2";
    }
    if (test.equals("comb2")) {

      users = new ArrayList<User>(Collections.nCopies(945, blank));
      user_exists = new int[945];
      num_users = 0;

      try {
        FileInputStream fstream = new FileInputStream("src/u.data"); // location of file in eclipse
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null) {
          StringTokenizer tok = new StringTokenizer(strLine);
          String ID = tok.nextToken();
          int userID = Integer.parseInt(ID);
          String MID = tok.nextToken();
          int movieID = Integer.parseInt(MID);
          String RANK = tok.nextToken();
          int movieRating = Integer.parseInt(RANK);
          Movie current_movie = new Movie(movieID, movieRating);

          if (user_exists[userID] == 0) {
            num_users++;
            users.set(userID, new User(userID, movieID, movieRating));
            user_exists[userID] = 1;
          } else {
            users.get(userID).movies.add(current_movie);
          }
        }

        in.close();
      } catch (Exception e) {
        if (e.getMessage() != null) {
          System.err.println("Error: " + e.getMessage());
        }
      }
      try {
        FileInputStream fstream = new FileInputStream("src/u.item"); // location of file in eclipse
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null) {
          movie_number++;
          StringTokenizer tok = new StringTokenizer(strLine, "|");
          String number = tok.nextToken();
          int movieNumber = Integer.parseInt(number);
          String movieName = tok.nextToken();
          for (int i = 0; i < users.size(); i++) {
            for (int j = 0; j < users.get(i).movies.size(); j++) {
              if (users.get(i).movies.get(j).movieID == movieNumber) {
                users.get(i).movies.get(j).setName(movieName);
              }
            }
          }
        }

        in.close();
      } catch (Exception j) {
        System.err.println("Error: " + j.getMessage());
      }
      for (int user_num = 1; user_num < num_users + 1; user_num++) {
        for (int usermovies = 0; usermovies < users.get(user_num).movies.size(); usermovies++) {
          if (users.get(user_num).movies.get(usermovies).ranking <= 3) {
            users.get(user_num).movies.remove(usermovies);
            usermovies--;
          }
        }
      }

      ArrayList<Movie> users_movies = new ArrayList<Movie>();
      Hashtable table = new Hashtable();
      Hashtable master_table = new Hashtable();
      for (int i = 1; i <= num_users; i++) {
        for (int k = 0; k < users.get(i).movies.size(); k++) {
          ArrayList<Integer> add = new ArrayList<Integer>();
          add.add(users.get(i).movies.get(k).movieID);
          // System.out.println(users.get(i).movies.get(k).movieID+ " is the current movie from user
          // "+i);

          if (table.containsKey(add)) {
            int table_value = (Integer) table.get(add);
            table_value++;
            table.put(add, table_value);
            // System.out.println("added 1 to the existing");
          } else {
            table.put(add, 1);
            // System.out.println("added a new");
          }
          // }
        }
      }
      // System.out.println(table);
      Iterator final_iter = table.keySet().iterator();
      while (final_iter.hasNext()) {
        ArrayList<Integer> next = (ArrayList<Integer>) final_iter.next();
        // System.out.println(t.get(next));
        // System.out.println(MINSUP);
        if ((Integer) table.get(next) < MINSUP) {
          // System.out.println("hi");
          final_iter.remove();
        }
      }
      // System.out.println(table);
      master_table.putAll(table);
      // System.out.println(master_table);
      for (int i = 2; i < MAXMOVIES; i++) {
        table = find_combos(table, users, i);
        if (!table.isEmpty()) {
          master_table.putAll(table);
        }
      }
      // System.out.println(master_table+" is before print_up");
      test = "pos";
      printup(master_table);
    }
  }