-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2023.01.22.py
50 lines (39 loc) · 1.16 KB
/
2023.01.22.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
49
50
print('One a Day One Liners - 2023.01.22')
print('Apply a function over each line of a file 📄')
from collections import Counter
from datetime import datetime
line_counter = 0
kw_counter = Counter()
FILE = './data/csv/web-of-science-20k.csv'
def count_keywords(line: str) -> None:
"""
Count keywords from a given line
"""
global line_counter
line_counter += 1
cols = line.split(',')
# Only count key words for class 0
if cols[0] == '0':
kws = filter(
lambda x: len(x),
map(lambda x: x.strip(), cols[5].split(' ')))
kw_counter.update(kws)
# Apply the count_keywords function line by line
t1 = datetime.now()
for line in open(FILE): count_keywords(line)
t2 = datetime.now()
print(f'Parsed keywords from {line_counter} lines in {t2-t1}')
for kw in kw_counter.most_common(32):
print(kw)
# Apply the count_keywords function to each line
# after reading the whole file
kw_counter = Counter()
t3 = datetime.now()
with open(FILE) as f:
lines = f.read().split('\n')
for line in lines:
count_keywords(line)
t4 = datetime.now()
print(f'Parsed keywords after reading whole file in {t4-t3}')
for kw in kw_counter.most_common(32):
print(kw)