Exemplo n.º 1
0
  public static _SparseFeature[] createSpVct(ArrayList<HashMap<Integer, Double>> vcts) {
    HashMap<Integer, _SparseFeature> spVcts = new HashMap<Integer, _SparseFeature>();
    HashMap<Integer, Double> vPtr;
    _SparseFeature spV;

    int dim = vcts.size();
    for (int i = 0; i < dim; i++) {
      vPtr = vcts.get(i);
      if (vPtr == null || vPtr.isEmpty())
        continue; // it is possible that we are missing this dimension

      // iterate through all the features in this section
      Iterator<Entry<Integer, Double>> it = vPtr.entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry<Integer, Double> pairs = (Map.Entry<Integer, Double>) it.next();
        int index = pairs.getKey();
        double value = pairs.getValue();
        if (spVcts.containsKey(index)) {
          spV = spVcts.get(index);
          spV.addValue(value); // increase the total value
        } else {
          spV = new _SparseFeature(index, value, dim);
          spVcts.put(index, spV);
        }
        spV.setValue4Dim(value, i);
      }
    }

    int size = spVcts.size();
    _SparseFeature[] resultVct = spVcts.values().toArray(new _SparseFeature[size]);

    Arrays.sort(resultVct);
    return resultVct;
  }