-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_words.py
executable file
·48 lines (38 loc) · 1.45 KB
/
count_words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
import pika
from collections import Counter
import json
# connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
def count_words(s: list) -> dict:
return dict(Counter(s))
credentials = pika.PlainCredentials('briedel', '2QfKHusafBn2zw9S')
parameters = pika.ConnectionParameters('rabbitmq.api.jetstream.rr.icecube.aq',
5672,
'/',
credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
consumer = channel.consume('book_test', inactivity_timeout=10)
channel.queue_declare(queue='book_test_out')
for method_frame, properties, body_raw in consumer:
if (method_frame is None) and (properties is None) and (body_raw is None):
break
body = body_raw.decode()
print(body.strip().split(" ")[0])
if not body.strip().split(" ")[0]:
channel.basic_ack(method_frame.delivery_tag)
continue
newl = []
for w in body.strip().split(" "):
if "." in w: w = w.strip(".")
if "," in w: w = w.strip(",")
if "\"" in w: w = w.strip("\"")
newl.append(w)
print(newl)
word_count = count_words(newl)
channel.basic_publish(exchange='',
routing_key='book_test_out',
body=json.dumps(word_count))
print(word_count)
channel.basic_ack(method_frame.delivery_tag)
connection.close()