[英]Mahout TFIDF Dictionary File
我正在嘗試使用mahout對一組文檔(作為文本文件)執行TFIDF,以便按照本指南進行計算。
我已經成功創建了字典和向量權重,現在我正在嘗試訪問輸出。 在指南中,它說“你可以輕松地將生成的字典文件的內容加載到Map中,其中令牌索引為鍵,令牌為值。”
我不知道如何將這個文件加載到地圖中,因為他建議,有人知道它是如何完成的嗎?
我從文本文件目錄創建了我的向量,運行“./mahout seq2sparse ...”時遇到的一個問題是控制分析器的-a標志 - 應該是lucene的StandardAnalyzer。 當嘗試使用此標志運行時,我收到了ClassNotFoundException,但刪除該標志解決了問題,我認為默認分析器也是這個,因此輸出應該與示例相同。
如果有人知道如何將這本字典加載到地圖中,我將永遠感激不盡!
詹姆士
我把它解決了,所以我正在為谷歌上遇到這個問題的任何人提出這個建議。
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到文本的映射。
由於上面的代碼缺少編譯它需要的import語句等,這是一個更完整的版本,可以從命令行讀取和轉儲dict文件的輸出
dumpdict.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的人來說,這可能有點過頭了,但對於我們這些經常不使用它的人來說,這可能會節省時間。)
that is build upon the StandardAnalyzer, so you can use this in the -a flag. Mahout創建了構建在StandardAnalyzer上的 ,因此您可以在-a標志中使用它。 您不能使用StandardAnalyzer,因為它沒有沒有args的構造函數,這就是您遇到錯誤的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.