-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.py
53 lines (39 loc) · 1.47 KB
/
Parser.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
51
52
53
# imports
import regex
import pandas as pd
import json
from glob import glob
import dpath.util as dp
import html
# parser
class Parser:
"""
High-level parser for text messages requested from VK and Telegram
"""
def __init__(self, path, mtype=None):
self.path = path
self.mtype = mtype
def parse(self):
text = []
if self.mtype == 'tg':
files = glob(f"{self.path}/**/*.json", recursive=True)
for i in files:
with open(i, encoding='utf-8') as obj:
ms = json.load(obj)
for msg in dp.values(ms, '/**/messages/*'):
if msg['type'] == 'message' and type(msg['text']) == str:
text.append(msg['text'])
elif self.mtype == 'vk':
files = glob(f"{self.path}/**/*.html", recursive=True)
for k in files:
with open(k, encoding='windows-1251') as obj:
contents = obj.read()
msg = regex.findall(r'(?<=<div>).+?(?=<div class="kludges"><div class="attachment">|<div '
r'class="kludges">|</div>)', contents)
text.extend(msg)
else:
raise ValueError("MType value is invalid or isn't specified! It can be either 'tg' or 'vk'.")
df = pd.DataFrame(data={'text': text})
df = df[df.text.str.len() > 0]
df.text = df.text.apply(html.unescape)
return df