-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_helper.py
66 lines (49 loc) · 1.66 KB
/
data_helper.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
54
55
56
57
58
59
60
61
62
63
64
65
66
'''
All of the data access calls involving writing and
reading from the file system are stored here.
Universal methods should be stored here too.
Requisite:
python 3.4 or above
Backing Protocol:
Pickle
Pandas
'''
import pickle
import pandas as pd
import math
from datetime import datetime
from nltk import PorterStemmer
##################################################################
### UNIVERSAL METHODS
##################################################################
PORTER_STEMMER = PorterStemmer()
### Normalise a term by case folding and porter stemming
def normalise_term(t):
return PORTER_STEMMER.stem(t.lower())
### Parse the date format from csv file
def dateparse(x):
return datetime.strptime(x, "%Y-%m-%d %H:%M:%S")
### Get log tf, given the normal tf
def log_tf(x):
return 1 + math.log(x, 10)
##################################################################
### DATA ACCESS METHODS
##################################################################
def store_data(filepath, data):
with open(filepath, 'wb') as f:
pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)
def load_data(filepath):
with open(filepath, 'rb') as f:
data = pickle.load(f)
return data
def store_data_with_handler(file, data):
pickle.dump(data, file, protocol=pickle.HIGHEST_PROTOCOL)
def load_data_with_handler(file, offset):
file.seek(offset)
data = pickle.load(file)
return data
def read_csv(filepath):
df = pd.read_csv(filepath, na_filter=False,
parse_dates=['date_posted'], index_col=False, date_parser=dateparse, encoding="utf-8")
df = df.sort_values("document_id", ascending=True)
return df