Exemplo n.º 1
0
 // make a lookup table from symbols and their encodings
 private void buildCode(String[] code, Node n, String path) {
   // If n isn't a leaf, we need to go deeper
   if (!n.isLeaf()) {
     buildCode(code, n.left, path + '0');
     buildCode(code, n.right, path + '1');
   } else // if it's a leaf, we fill the case in the code table that correspond to this letter with
          // the equivalent code that represent it
   {
     code[n.c] = path;
   }
 }
Exemplo n.º 2
0
 private void writeCode(Node n) throws IOException {
   if (n.isLeaf()) {
     output.write(1);
     System.out.print(1);
     output.write((int) n.c);
     System.out.print((int) n.c);
   } else {
     output.write(0);
     System.out.print(0);
     writeCode(n.left);
     writeCode(n.right);
   }
 }
Exemplo n.º 3
0
  private void print(Stack<Node> stack, Node n) {
    if (n.isLeaf()) {
      assert n.value != null;
      for (Node node : stack) {
        System.out.print(node.key + "->");
      }
      System.out.print(n.value);
      System.out.println();

      return;
    }
    for (Map.Entry<Character, Node> e : n.children.entrySet()) {
      stack.push(e.getValue());
      print(stack, e.getValue());
      stack.pop();
    }
  }
Exemplo n.º 4
0
  /**
   * Remove the node if possible or set its count to 0 if it has children and it needs to be kept
   * around
   */
  private Node tryRemove(Node node) {
    if (node == null) {
      return null;
    }

    if (node.weightedCount >= ZERO_WEIGHT_THRESHOLD) {
      --nonZeroNodeCount;
    }

    weightedCount -= node.weightedCount;

    Node result = null;
    if (node.isLeaf()) {
      --totalNodeCount;
    } else if (node.hasSingleChild()) {
      result = node.getSingleChild();
      --totalNodeCount;
    } else {
      node.weightedCount = 0;
      result = node;
    }

    return result;
  }
Exemplo n.º 5
0
  public void outputToJSON(String filePath, int numTopWords, double boundaryFactor) {
    System.out.println("Writing down the model...");

    JSONObject featureCollection = new JSONObject();
    featureCollection.put("type", "FeatureCollection");

    JSONArray features = new JSONArray();
    Queue<Node> nodeQueue = new LinkedList<>();
    nodeQueue.offer(root);
    while (!nodeQueue.isEmpty()) {
      Node currentNode = nodeQueue.poll();
      JSONObject feature = new JSONObject();
      feature.put("type", "Feature");
      feature.put("id", currentNode.hashCode());

      JSONObject properties = new JSONObject();
      properties.put("level", currentNode.level + 1);
      properties.put("num_documents", currentNode.numCustomers);

      if (currentNode.parent != null) {
        properties.put("parent", currentNode.parent.hashCode());
      }

      if (!currentNode.isLeaf()) {
        JSONArray children = new JSONArray();
        for (Node child : currentNode.children) {
          children.put(child.hashCode());
        }
        properties.put("children", children);
      }

      JSONArray center = new JSONArray();
      double longitude = currentNode.location.longitude;
      center.put(longitude);
      double latitude = currentNode.location.latitude;
      center.put(latitude);
      properties.put("center", center);

      JSONArray deviation = new JSONArray();
      double longitudeDeviation = Math.sqrt(currentNode.location.longitudeVariance);
      deviation.put(longitudeDeviation);
      double latitudeDeviation = Math.sqrt(currentNode.location.latitudeVariance);
      deviation.put(latitudeDeviation);
      properties.put("deviation", deviation);

      JSONArray topWords = new JSONArray();
      PriorityQueue<Map.Entry<Integer, Integer>> wordMinHeap =
          new PriorityQueue<>(
              numTopWords,
              new Comparator<Map.Entry<Integer, Integer>>() {
                @Override
                public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                  return o1.getValue() - o2.getValue();
                }
              });
      for (Map.Entry<Integer, Integer> entry : currentNode.wordCounts.entrySet()) {
        if (wordMinHeap.size() < numTopWords) {
          wordMinHeap.offer(entry);
        } else {
          if (entry.getValue() > wordMinHeap.peek().getValue()) {
            wordMinHeap.poll();
            wordMinHeap.offer(entry);
          }
        }
      }
      while (!wordMinHeap.isEmpty()) {
        topWords.put(this.id2Word.get(wordMinHeap.poll().getKey()));
      }
      properties.put("top_words", topWords);

      feature.put("properties", properties);

      JSONObject geometry = new JSONObject();
      geometry.put("type", "Polygon");

      JSONArray coordinates = new JSONArray();
      JSONArray boundary = new JSONArray();
      double east = longitude + boundaryFactor * longitudeDeviation;
      double west = longitude - boundaryFactor * longitudeDeviation;
      double north = latitude + boundaryFactor * latitudeDeviation;
      double south = latitude - boundaryFactor * latitudeDeviation;
      boundary.put(new JSONArray(new double[] {west, north}));
      boundary.put(new JSONArray(new double[] {east, north}));
      boundary.put(new JSONArray(new double[] {east, south}));
      boundary.put(new JSONArray(new double[] {west, south}));
      boundary.put(new JSONArray(new double[] {west, north}));
      coordinates.put(boundary);
      geometry.put("coordinates", coordinates);

      feature.put("geometry", geometry);

      features.put(feature);

      for (Node child : currentNode.children) {
        nodeQueue.offer(child);
      }
    }

    featureCollection.put("features", features);

    try {
      PrintWriter writer = new PrintWriter(filePath);
      featureCollection.write(writer);
      writer.close();
    } catch (FileNotFoundException e) {
      System.err.println("Model JSON cannot be created.");
    }
  }