-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.py
37 lines (28 loc) · 1.28 KB
/
reader.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
import os
import pandas as pd
class Reader:
def __init__(self, corpus_path):
self.corpus_path = corpus_path
def read_file(self, file_name):
"""
This function is reading a parquet file contains several tweets
The file location is given as a string as an input to this function.
:param file_name: string - indicates the path to the file we wish to read or a directory that contains
multiple parquet files in other directories
:return: a dataframe contains tweets.
"""
parquet_files = []
full_path = os.path.join(self.corpus_path, file_name)
if ".parquet" in file_name: # Load the single file given
df = pd.read_parquet(full_path, engine="pyarrow")
else: # Read all .parquet files from the directory given
for root, dirs, files in os.walk(full_path):
for file in files:
if file.endswith(".parquet"):
full_path = os.path.join(root, file)
df = pd.read_parquet(full_path, engine="pyarrow")
parquet_files.append(df)
df = pd.concat(parquet_files, sort=False)
return df.values.tolist()
def set_corpus_path(self, path):
self.corpus_path = path