Example usage for org.apache.mahout.math VarIntWritable VarIntWritable

List of usage examples for org.apache.mahout.math VarIntWritable VarIntWritable

Introduction

In this page you can find the example usage for org.apache.mahout.math VarIntWritable VarIntWritable.

Prototype

public VarIntWritable(int value) 

Source Link

Usage

From source file:nl.gridline.free.taalserver.CountDocumentsReduce.java

License:Apache License

@Override
protected void reduce(LongWritable key, java.lang.Iterable<VarLongWritable> values, Context context)
        throws IOException, InterruptedException {
    int numberOfDocuments = 0;
    Iterator<VarLongWritable> i = values.iterator();
    while (i.hasNext()) // VarLongWritable writable : values)
    {//from   w ww  .j  av a 2s. c  om
        numberOfDocuments++;
        i.next();
    }
    context.write(new VarIntWritable(numberOfDocuments), NullWritable.get());
    context.progress();
}

From source file:nl.gridline.zieook.inx.movielens.items.ItemBasedSortSimilaritiesMapper.java

License:Apache License

@Override
protected void map(IntWritable key, VectorWritable value, Context context)
        throws IOException, InterruptedException {
    int maxIndex = -1;

    Vector similarityMatrixRow = value.get();
    /* remove self similarity */

    similarityMatrixRow.set(key.get(), Double.NEGATIVE_INFINITY);

    ///*from  w  ww  .j a va2s  .com*/
    // determine maximum index
    //
    Iterator<Element> it = similarityMatrixRow.iterateNonZero();

    while (it.hasNext()) {
        Element e = it.next();

        // e.index() // == item id

        if (e.index() > maxIndex) {
            maxIndex = e.index();
        }
    }

    // System.out.println(String.format("key: %d maxIndex: %d", key.get(), maxIndex));

    if (maxIndex > 0) {

        RecommendationElement[] itemBasedRecommendations = new RecommendationElement[maxIndex];

        for (int i = 0; i < maxIndex; i++) {
            Element element = similarityMatrixRow.getElement(i);

            double similarityValue = Double.NEGATIVE_INFINITY;

            if (element != null) {
                similarityValue = element.get();
            }

            itemBasedRecommendations[i] = new RecommendationElement(i, similarityValue);
        }

        Arrays.sort(itemBasedRecommendations, new SimilarityComparator());

        RecommendationElementArray array = new RecommendationElementArray(itemBasedRecommendations);

        context.write(new VarIntWritable(key.get()), array);

    }
}

From source file:org.apache.hadoop.mapred.nativetask.testutil.BytesUtil.java

License:Apache License

private static Object newMahoutObject(byte[] seed, String className) {
    if (className.equals(VarIntWritable.class.getName())) {
        return new VarIntWritable(Bytes.toInt(seed));
    } else if (className.equals(VarLongWritable.class.getName())) {
        return new VarLongWritable(Bytes.toLong(seed));
    } else if (className.equals(TreeID.class.getName())) {
        TreeID treeID = new TreeID();
        treeID.set(Bytes.toLong(seed));/* w  ww.  jav  a2 s .c om*/
        return treeID;
    } else if (className.equals(SplitPartitionedWritable.class.getName())) {
        SplitPartitionedWritable spWritable = new SplitPartitionedWritable();
        long taskItemOrdinal = Math.abs(Bytes.toLong(seed, 4));
        spWritable.setTaskItemOrdinal(taskItemOrdinal);
        return spWritable;
    } else if (className.equals(EntityEntityWritable.class.getName())) {
        EntityEntityWritable entityWritable = new EntityEntityWritable(Bytes.toLong(seed, 0),
                Bytes.toLong(seed, 8));
        return entityWritable;
    } else if (className.equals(Gram.class.getName())) {
        String ngram = Bytes.toStringBinary(seed);
        return new Gram(ngram, Gram.Type.NGRAM);
    } else if (className.equals(GramKey.class.getName())) {
        int primaryLength = r.nextInt(seed.length);
        Gram gram = new Gram(Bytes.toStringBinary(seed, 0, Math.max(primaryLength, 1)), Gram.Type.NGRAM);
        byte[] order = new byte[seed.length - primaryLength];
        System.arraycopy(seed, primaryLength, order, 0, order.length);
        return new GramKey(gram, order);
    } else if (className.equals(StringTuple.class.getName())) {
        int tupleSize = r.nextInt(4);
        StringTuple stringTuple = new StringTuple();
        for (int i = 0; i < tupleSize; i++) {
            int index = r.nextInt(seed.length);
            stringTuple.add(Bytes.toStringBinary(seed, index, seed.length - index));
        }
        return stringTuple;
    } else {
        return null;
    }
}