2.7. Wordcount Solution (With reduce)
Complete version
[1]:
import os
[2]:
import pycompss.interactive as ipycompss
[3]:
from pycompss.api.task import task
[4]:
from pycompss.api.parameter import *
[5]:
if 'BINDER_SERVICE_HOST' in os.environ:
ipycompss.start(graph=True, trace=True, debug=False,
project_xml='../xml/project.xml',
resources_xml='../xml/resources.xml')
else:
ipycompss.start(graph=True, monitor=1000, trace=True, debug=False)
********************************************************
**************** PyCOMPSs Interactive ******************
********************************************************
* .-~~-.--. ______ ______ *
* : ) |____ \ |____ \ *
* .~ ~ -.\ /.- ~~ . __) | __) | *
* > `. .' < |__ | |__ | *
* ( .- -. ) ____) | _ ____) | *
* `- -.-~ `- -' ~-.- -' |______/ |_| |______/ *
* ( : ) _ _ .-: *
* ~--. : .--~ .-~ .-~ } *
* ~-.-^-.-~ \_ .~ .-~ .~ *
* \ \ ' \ '_ _ -~ *
* \`.\`. // *
* . - ~ ~-.__\`.\`-.// *
* .-~ . - ~ }~ ~ ~-.~-. *
* .' .-~ .-~ :/~-.~-./: *
* /_~_ _ . - ~ ~-.~-._ *
* ~-.< *
********************************************************
* - Starting COMPSs runtime... *
* - Log path : /home/user/.COMPSs/Interactive_20/
* - PyCOMPSs Runtime started... Have fun! *
********************************************************
[6]:
@task(returns=list)
def read_file(file_path):
""" Read a file and return a list of words.
:param file_path: file's path
:return: list of words
"""
data = []
with open(file_path, 'r') as f:
for line in f:
data += line.split()
return data
[7]:
@task(returns=dict)
def wordCount(data):
""" Construct a frequency word dictorionary from a list of words.
:param data: a list of words
:return: a dictionary where key=word and value=#appearances
"""
partialResult = {}
for entry in data:
if entry in partialResult:
partialResult[entry] += 1
else:
partialResult[entry] = 1
return partialResult
[8]:
@task(returns=dict, priority=True)
def merge_dicts(*dictionaries):
import queue
q = queue.Queue()
for i in dictionaries:
q.put(i)
while not q.empty():
x = q.get()
if not q.empty():
y = q.get()
for k in y:
if k in x:
x[k] += y[k]
else:
x[k] = y[k]
q.put(x)
return(x)
[9]:
from pycompss.api.api import compss_wait_on
# Get the dataset path
pathDataset = os.getcwd() + '/dataset'
# Construct a list with the file's paths from the dataset
partialResult = []
for fileName in os.listdir(pathDataset):
p = os.path.join(pathDataset, fileName)
data=read_file(p)
partialResult.append(wordCount(data))
# Accumulate the partial results to get the final result.
result=merge_dicts(*partialResult)
# Wait for result
result = compss_wait_on(result)
Found task: read_file
Found task: wordCount
Found task: merge_dicts
[10]:
print("Result:")
from pprint import pprint
pprint(result)
print("Words: {}".format(sum(result.values())))
Result:
{'Adipisci': 227,
'Aliquam': 233,
'Amet': 207,
'Consectetur': 201,
'Dolor': 198,
'Dolore': 236,
'Dolorem': 232,
'Eius': 251,
'Est': 197,
'Etincidunt': 232,
'Ipsum': 228,
'Labore': 229,
'Magnam': 195,
'Modi': 201,
'Neque': 205,
'Non': 226,
'Numquam': 253,
'Porro': 205,
'Quaerat': 217,
'Quiquia': 212,
'Quisquam': 214,
'Sed': 225,
'Sit': 220,
'Tempora': 189,
'Ut': 217,
'Velit': 218,
'Voluptatem': 235,
'adipisci': 1078,
'aliquam': 1107,
'amet': 1044,
'consectetur': 1073,
'dolor': 1120,
'dolore': 1065,
'dolorem': 1107,
'eius': 1048,
'est': 1101,
'etincidunt': 1114,
'ipsum': 1061,
'labore': 1070,
'magnam': 1096,
'modi': 1127,
'neque': 1093,
'non': 1099,
'numquam': 1094,
'porro': 1101,
'quaerat': 1086,
'quiquia': 1079,
'quisquam': 1144,
'sed': 1109,
'sit': 1130,
'tempora': 1064,
'ut': 1070,
'velit': 1105,
'voluptatem': 1121}
Words: 35409
[11]:
ipycompss.stop()
********************************************************
***************** STOPPING PyCOMPSs ********************
********************************************************
Checking if any issue happened.
Warning: some of the variables used with PyCOMPSs may
have not been brought to the master.
********************************************************
[12]:
ipycompss.complete_task_graph(fit=True)