- 使用MapReduce進行數據密集型文本處理
- 使用MapReduce進行數據密集型文本處理-本地聚合第二部分
共現矩陣可以描述為事件的跟蹤,并且在給定的時間或空間窗口下,似乎還會發生其他事件。 出于本文的目的,我們的“事件”是文本中找到的單個單詞,我們將跟蹤“窗口”中相對于目標單詞的位置出現的其他單詞。 例如,考慮短語“快速的棕色狐貍跳過了懶狗”。 窗口值為2時,單詞“ jumped”的同時出現為[brown,fox,over,the]。 同現矩陣可以應用于需要調查“此”事件何時發生,其他事件似乎同時發生的其他區域。 為了構建我們的文本共現矩陣,我們將使用MapReduce實現數據密集型文本處理的第3章中的“成對和條紋”算法。 用來創建我們共現矩陣的正文是威廉·莎士比亞的集體著作。
對
實施配對方法很簡單。 對于調用map函數時傳遞的每一行,我們將在空格處拆分以創建字符串數組。 下一步將是構造兩個循環。 外循環將遍歷數組中的每個單詞,而內循環將遍歷當前單詞的“鄰居”。 內部循環的迭代次數由捕獲當前單詞鄰居的“窗口”的大小決定。 在內部循環的每次迭代的底部,我們將發送一個WordPair對象(由左側的當前單詞和右側的相鄰單詞組成)作為鍵,并計數1作為值。 這是Pairs實現的代碼:
public class PairsOccurrenceMapper extends Mapper<LongWritable, Text, WordPair, IntWritable> {private WordPair wordPair = new WordPair();private IntWritable ONE = new IntWritable(1);@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {int neighbors = context.getConfiguration().getInt('neighbors', 2);String[] tokens = value.toString().split('\\s+');if (tokens.length > 1) {for (int i = 0; i < tokens.length; i++) {wordPair.setWord(tokens[i]);int start = (i - neighbors < 0) ? 0 : i - neighbors;int end = (i + neighbors >= tokens.length) ? tokens.length - 1 : i + neighbors;for (int j = start; j <= end; j++) {if (j == i) continue;wordPair.setNeighbor(tokens[j]);context.write(wordPair, ONE);}}}}
}
Pairs實現的Reducer將簡單地將給定WordPair鍵的所有數字相加:
public class PairsReducer extends Reducer<WordPair,IntWritable,WordPair,IntWritable> {private IntWritable totalCount = new IntWritable();@Overrideprotected void reduce(WordPair key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int count = 0;for (IntWritable value : values) {count += value.get();}totalCount.set(count);context.write(key,totalCount);}
}
條紋
實現共現的條帶化方法同樣簡單。 方法是相同的,但是所有“鄰居”字都是在HashMap中收集的,其中鄰居字為鍵,整數計數為值。 當已經為給定單詞(外部循環的底部)收集了所有值時,將發出單詞和哈希圖。 這是我們的Stripes實現的代碼:
public class StripesOccurrenceMapper extends Mapper<LongWritable,Text,Text,MapWritable> {private MapWritable occurrenceMap = new MapWritable();private Text word = new Text();@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {int neighbors = context.getConfiguration().getInt('neighbors', 2);String[] tokens = value.toString().split('\\s+');if (tokens.length > 1) {for (int i = 0; i < tokens.length; i++) {word.set(tokens[i]);occurrenceMap.clear();int start = (i - neighbors < 0) ? 0 : i - neighbors;int end = (i + neighbors >= tokens.length) ? tokens.length - 1 : i + neighbors;for (int j = start; j <= end; j++) {if (j == i) continue;Text neighbor = new Text(tokens[j]);if(occurrenceMap.containsKey(neighbor)){IntWritable count = (IntWritable)occurrenceMap.get(neighbor);count.set(count.get()+1);}else{occurrenceMap.put(neighbor,new IntWritable(1));}}context.write(word,occurrenceMap);}}}
}
由于我們需要迭代一組地圖,然后針對每個映射,遍歷該映射中的所有值,因此使用“ Reducer for Stripes”方法要復雜得多。
public class StripesReducer extends Reducer<Text, MapWritable, Text, MapWritable> {private MapWritable incrementingMap = new MapWritable();@Overrideprotected void reduce(Text key, Iterable<MapWritable> values, Context context) throws IOException, InterruptedException {incrementingMap.clear();for (MapWritable value : values) {addAll(value);}context.write(key, incrementingMap);}private void addAll(MapWritable mapWritable) {Set<Writable> keys = mapWritable.keySet();for (Writable key : keys) {IntWritable fromCount = (IntWritable) mapWritable.get(key);if (incrementingMap.containsKey(key)) {IntWritable count = (IntWritable) incrementingMap.get(key);count.set(count.get() + fromCount.get());} else {incrementingMap.put(key, fromCount);}}}
}
結論
查看這兩種方法時,我們可以發現Pairs算法與Stripes算法相比將生成更多的鍵值對。 此外,“對”算法捕獲每個單獨的同現事件,而“條紋”算法捕獲給定事件的所有同現事件。 成對和條紋實現都將受益于使用組合器。 因為兩者都會產生交換和關聯結果,所以我們可以簡單地將每個Mapper的Reducer用作合并器。 如前所述,創建共現矩陣不僅適用于文本處理,而且還適用于其他領域,并且代表了有用的MapReduce算法。 謝謝你的時間。
資源資源
- Jimmy Lin和Chris Dyer 使用MapReduce進行的數據密集型處理
- Hadoop: Tom White 的權威指南
- 來自博客的源代碼和測試
- Hadoop API
- MRUnit用于單元測試Apache Hadoop映射減少工作
參考: 《 隨機編碼》博客上的JCG合作伙伴 Bill Bejeck提供了與Hadoop計算共現矩陣的信息 。
翻譯自: https://www.javacodegeeks.com/2012/11/calculating-a-co-occurrence-matrix-with-hadoop.html