Here is one way to explain the Mapreduce programming
model. Say that I wanted to count the number of
occurrences of each word that appears at least once in
a document. Let's use the text of Alice in Wonderland.
Here's a bit of text that says Alice was
begining to get very tired of sitting by her sister
on the bank And of having nothing to do. If
I wanted to solve this problem without Mapreduce, I might
create a Python dictionary consisting of all the words
and their counts. I could go through the document
and say, for each word in the document, if
there is a key for that word, add one.
Otherwise, set the initial for that key equal to
one. And instead of applying it to this short
sentence fragment from the book, we'd apply it to
the entire book. Before we solve this problem with Mapreduce,
why don't you try to write a Python script
along the lines of what we just discussed, that will
get the job done. Given many lines of a text,
create a dictionary with a key for each word, and
a value corresponding to the count of the word in
that text. Note that we want the words to be
stripped of any capitalization and punctuation. We just want the
basic words. Here's some code to get you started. First,
we import system string. And then we
initialize an empty dictionary, which will hold our
words and values. We cycle through the lines of the input, and for each line we
create an array, data. Which is essentially all
of the words in that line, split by
white space. So if we started with this
line. Hello, how are you? It would become,
hello, how, are, and you, in an array of length four. Your code should go here.
After we split the line by white space, and before we print out the dictionary.
فيما يلي أحد نماذج برمجة Mapreduce
لنفرض أنني أريد احتساب عدد .
تكرارات كل كلمة تظهر مرة واحدة على الأقل في مستند
.معين. فلنستخدم نصًا من رواية أليس في بلاد العجائب
هذا جزء من النص الذي يقول أن أليس بدأت تشعر بالتعب
الشديد من ملازمة أختها
على ضفة النهر ومن عدم فعل شيء. إن أردت حل المشكلة دون الاستعانة بـ
Mapreduce، فيمكنني أن أنشئ قاموس Python
يتضمن جميع الكلمات
وعددها. يمكنني التنقل في المستند والقول أنه بالنسبة إلى كل كلمة في المستند
إذا كان هناك مفتاح لهذه الكلمة فأضف
.الرقم واحد
أو قم بتعيين البداية لهذا الحرف بما يعادل الرقم
واحد. وبدلاً من تطبيق ذلك على جزء الجملة القصيرة هذه من
الكتاب، فإننا نرغب في تطبيقه على الكتاب
،كله. قبل حل هذه المشكلة باستخدام Mapreduce
لماذا لا نحاول كتابة برنامج Python النصي
بالإضافة إلى الأسطر التي ناقشناها للتو، والتي ستؤدي إلى
،تنفيذ المهمة. ومع توفر أسطر متعددة من النص
،أنشئ قاموسًا يتضمن مفتاحًا لكل كلمة
وقيمة تقابل عدد الكلمات في
هذا النص. لاحظ أننا نريد كل الكلمات مكتوبة بأحرف صغيرة فقط
وبدون أي علامات ترقيم. فنحن نريد الكلمات الأساسية
،فقط. فيما يلي بعض التعليمات البرمجية التي يمكنك البدء بها. أولاً
سنقوم باستيراد سلسلة نظام. ثم سننشئ قاموسًا فارغًا
وهو الذي سيحتفظ بالكلمات والقيم
سننتقل خلال صفوف الإدخال وسننشئ صفيفًا وبيانات لكل .
صف. ومن الضروري فصل
جميع الكلمات في هذا السطر
بمسافة فارغة. لذا إن بدأنا بهذا
السطر. مرحبًا، كيف حالك؟ فستكون الصيغة هي
.مرحبًا، كيف، حالك، وأنت، في صفيف يتكون طوله من أربع كلمات. Your code should go here
.بعد تقسمينا للسطر بالمسافات البيضاء، وقبل طباعة القاموس
ではMapReduceプログラミングモデルの説明です
ある文書に出てくる単語について
それぞれの出現数をカウントするとします
「ふしぎの国のアリス」でやってみましょう
これは一文を抜き出したものです
Alice was beginning to get very tired
of sitting by her sister on the bank
and of having nothing to do
MapReduceを使わずにこの問題を解くとしたら
すべての単語とそのカウント数で構成される
辞書を作ります
文を読んでいき文中に出てくる各単語について
単語のキーが出てきたら1を加えます
なければその単語のキーの初期値を1にセットします
このような本の中の短い一文ではなく
本全体を調べます
MapReduceでこれを解く前に
今の説明に従ってPythonスクリプトを記述し
処理してみましょう
このようにテキスト行が多い場合キーを各単語とし
値を単語のカウント数とした辞書を作成します
単語はすべて小文字にし句読点記号などは除き
単数形や現在形で使います
これは準備段階のコードです
まずsysとstringをインポートし
空の辞書の初期化を行います
単語と値が入るところです
流れとしては入力する行全体を読み
各行に対して配列を作成します これがdataです
これは行の中のすべての単語から成り
これをホワイトスペースで区切ります
例えば "Hello, how are you?"という行の場合
この配列は
hello、how、are、youの4つの要素を持ちます
コードはこの部分に書きます
行をホワイトスペースで区切ったあと
辞書に出力させる前です
Veja aqui uma maneira de explicar o modelo de programação
MapReduce. Digamos que eu quisesse contar o número de
ocorrências de cada palavra que aparece pelo menos em
um documento. Vamos usar o texto de Alice no País das Maravilhas.
Aqui está um pequeno texto que diz: Alice
estava começando a ficar muito cansada de estar sentada ao lado de sua irmã
e não ter nada para fazer. Se
eu quisesse resolver este problema sem o MapReduce, eu poderia
criar um dicionário Python que tivesse todas as palavras
e suas contagens. Eu poderia passar pelo documento
e dizer, para cada palavra do documento, se
houver uma chave para essa palavra, adicione um.
Caso contrário, definiria a inicial para essa chave igual a
um. E, ao invés de aplicá-la a este fragmento de sentença
curto do livro, aplicaria isso ao
livro todo. Antes de resolver este problema com o MapReduce,
por que você não tenta gravar um script Python
junto com as linhas que acabamos de discutir, que
realizará a tarefa? Dadas muitas linhas de um texto,
crie um dicionário com uma chave para cada palavra e
um valor correspondente à contagem de palavras
desse texto. Observe que queremos que as palavras sejam
retiradas sem qualquer capitalização e pontuação. Queremos apenas as
palavras básicas. Veja aqui um código para você começar. Primeiro,
importamos a string do sistema. Depois,
começamos um dicionário vazio, que coletará nossas
palavras e valores. Passaremos por todas as linhas da entrada e para cada linha
criaremos um array, data. Que são essencialmente todas
as palavras dessa linha, divididas pelo
espaço em branco. Se começarmos com esta
linha. Oi, tudo bem? Ela se tornaria,
Oi, tudo, bem? em um array de tamanho três. Seu código deve entrar aqui.
Depois, dividimos a linha pelo espaço em branco e antes imprimimos o dicionário.