1
我有一個從.txt文件創建哈希表的方法,並使用該哈希表爲傳遞給Reducer的值中的單詞賦值。下面是我試圖做到這一點:爲什麼我的Reducer不能讀取文件?
@Override
public void setup(Context context) throws IOException {
Path pt = new Path("hdfs:/user/jk/sentiwords.txt");
FileSystem fs = FileSystem.get(new Configuration());
BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(pt)));
String line = br.readLine();
while (line!=null) {
String[] split = line.split("\t");
String word = split[0].substring(0, split[0].length() - 2);
double score = Double.parseDouble(split[1]);
int hashCode = word.hashCode();
sentiTable.put(hashCode, score);
line = br.readLine();
System.out.println("Success");
}
}
它,然後在此方法中,這就是所謂的在鍵/值對每個值使用:
public double analyzeString(String str) {
double stringScore = 0.0;
String[] strArr = str.replaceAll("[^a-zA-Z ]", "").toLowerCase().split(" ");
for (String segment: strArr) {
int hashedSeg = segment.hashCode();
if (sentiTable.containsKey(hashedSeg)) {
double value = (double) sentiTable.get(hashedSeg);
stringScore += value;
}
}
return stringScore;
}
理想的情況下,這應該-1和1之間返回一個數字在現實中,它總是返回0
編輯:
我要指出,sentiTable在類級別創建。
我想通了 - 我正在閱讀的文件有格式錯誤!它應該是製表符分隔的,並且大部分是,但是有些新的線路在某個時刻悄悄溜走。我很感激幫助 – treo