通过密钥提取tfidf向量而不破坏文件格式

q3aa0525  于 2021-06-21  发布在  Pig
关注(0)|答案(1)|浏览(279)

我有大约200000个输出格式为seq2sparse的tfidf向量。现在我需要提取500,但不是随机像分裂函数。我知道其中500个的密钥,我需要它们的数据格式与seq2sparse中的相同。当我打开包含200000个条目的sequencefile时,我可以看到键是用org.apache.hadoop.io.text编码的,值是用org.apache.mahout.math.vectorwritable编码的。
但是当我尝试使用https://github.com/kevinweil/elephant-bird/blob/master/mahout/src/main/java/com/twitter/elephantbird/pig/mahout/vectorwritableconverter.java

https://github.com/kevinweil/elephant-bird/blob/master/pig/src/main/java/com/twitter/elephantbird/pig/store/sequencefilestorage.java
在pig拉丁语中,为了读写它们,输出的key和value都是org.apache.hadoop.io.text。
我确实需要这500个条目,因为我想在trainnb和testnb中使用它们。
基本上,这将是足够的,知道我如何可以做的事情,如马霍特seqdumper逆转。

thigvfpy

thigvfpy1#

虽然没有特定的mahout命令来执行此操作,但您可以使用mahout命令编写一个相对简单的实用程序函数:

org.apache.mahout.common.Pair;
org.apache.mahout.common.iterator.sequencefile.SequenceFileIterable;
org.apache.mahout.math.VectorWritable;

以及:

org.apache.hadoop.io.SequenceFile;
org.apache.hadoop.io.Text;
com.google.common.io.Closeables;

您可以执行以下操作:

// load up the 500 desired keys with some function
Vector<Text>desiredKeys = getDesiredKeys();
//create a new SequenceFile writer for the 500 Desired Vectors 
SequenceFile.Writer writer =
        SequenceFile.createWriter(fs, conf, output500filePath ,
                                  Text.class,
                                  VectorWritable.class);         
try {
  // create an iterator over the tfidfVector sequence file 
  SequenceFileIterable<Text, VectorWritable>seqFileIterable =
          new SequenceFileIterable<Text, VectorWritable>(
              tfidfVectorPath, true, conf)

  // loop over tfidf sequence file and write out only Pairs with keys
  // contained in the desiredKeys Vector to the output500file 
  for (Pair<Text, VectorWritable> pair : seqFileIterable) {
      if(desiredKeys.contains(pair.getFirst())){
            writer.append(pair.getFirst(),pair.getSecond());
      }
  }
}finally {
  Closeables.close(writer, false);
}

并使用“output500file”的路径作为trainnb的输入。使用vector.contains()并不是最有效的方法,但这是一般的想法。

相关问题