
from Scratch
389
from typing import Iterable
def wc
_
reducer(word: str,
counts: Iterable[int]) -> Iterator[Tuple[str, int]]:
"""
단어의
모든
빈도
수를
더한다
."""
yield (word, sum(counts))
이제 다시
2
번째 단계로 돌아가 보면
wc_mapper
로부터 얻은 결과를
wc_reducer
한테 전달해 주기만 하면 된다. 이것을 컴퓨터 한 대로 처리할 방법을 생각해
보자.
from collections import defaultdict
def word
_
count(documents: List[str]) -> List[Tuple[str, int]]:
"""
맵리듀스를
사용해서
입력
문서의
단어
빈도
수를
세어
준다
."""
collector = defaultdict(list) #
쌍으로
묶인
값을
저장할
공간
for document in documents:
for word, count in wc
_
mapper(document):
collector[word].append(count)
return [output
for word, counts in collector.items()
for output in wc
_
reducer(word, ...