2

我正在尝试按照本指南使用 mahout 对一组文档(作为文本文件)执行 TFIDF 进行计算。

我已经成功创建了字典和向量权重,现在正尝试访问输出。在指南中它说您“例如可以轻松地将生成的字典文件的内容加载到 Map 中,其中令牌索引作为键,令牌作为值。”

我不确定如何按照他的建议将此文件加载到地图中,有人知道它是如何完成的吗?

我从一个文本文件目录创建了我的向量,我在运行“./mahout seq2sparse...”时遇到的一个问题是控制分析器的 -a 标志 - 它应该是 lucene 的 StandardAnalyzer。尝试使用此标志运行时,我收到了 ClassNotFoundException,但删除标志解决了问题,我认为默认分析器也是这个,因此输出应该与示例相同。

如果有人知道如何将这本词典加载到地图中,我将永远感激不尽!

詹姆士

4

3 回答 3

6

我解决了,所以我把这个放在谷歌上遇到这个的任何人。

        SequenceFile.Reader read = new SequenceFile.Reader(fs, new Path("<path do dictionary>"), conf);
        IntWritable dicKey = new IntWritable();
        Text text = new Text();
        Map<Integer, String> dictionaryMap = new HashMap();
        while (read.next(text, dicKey)) {
            dictionaryMap.put(Integer.parseInt(dicKey.toString()), text.toString());
        }
        read.close();

这对我有用,允许我从 mahout 读取我的字典文件中的 id 到文本的映射。

于 2012-03-14T21:06:35.997 回答
1

Mahout 创建 了基于 StandardAnalyzer 的org.apache.mahout.vectorizer.DefaultAnalyzer,因此您可以在 -a 标志中使用它。您不能使用 StandardAnalyzer,因为它没有没有参数的构造函数,这就是您遇到错误的原因。

于 2012-03-23T21:29:51.217 回答
1

由于上面的代码缺少编译它所需的导入语句等,因此这里是一个更完整的版本,可以从命令行读取和转储 dict 文件的输出

转储字典.java:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;

class DumpDict {
  public static void main(String[] args) {
    try {
      Configuration conf = new Configuration();
      FileSystem fs = FileSystem.get(conf);
      SequenceFile.Reader read = new SequenceFile.Reader(fs, new Path(args[0]), conf);
      IntWritable dicKey = new IntWritable();
      Text text = new Text();
      // HashMap dictionaryMap = new HashMap();
      while (read.next(text, dicKey)) {
        // dictionaryMap.put(Integer.parseInt(dicKey.toString()), text.toString());
        System.out.println(dicKey.toString()+" "+text.toString());
      }
      read.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}

我发现有必要明确告诉 java 所有 jar 文件在哪里:

export CLASSPATH=`find /path/to/mahout /usr/share/java -name '*.jar' | perl -ne 'chomp; push @jars, $_; END { print "\".:",(join ":",@jars),"\$CLASSPATH\"\n"; }'`

像这样编译:

javac dumpdict.java

像这样运行:

java -cp .:$CLASSPATH DumpDict {path to dict}

(这对于使用 java 的人来说可能有点过分,但对于我们这些不经常使用它的人来说,它可能会节省时间。)

于 2015-06-12T20:48:42.450 回答