-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringutils.py
More file actions
67 lines (53 loc) · 1.74 KB
/
Copy pathstringutils.py
File metadata and controls
67 lines (53 loc) · 1.74 KB
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
67
#!/usr/bin/env python3
"""Provides StringUtils class"""
import json
import re
__author__ = "Rafael Gonçalves, Stanford University"
class StringUtils:
@staticmethod
def get_dictionary_as_json(dictionary):
return json.dumps(dictionary, sort_keys=True, indent=2)
@staticmethod
def parse_file(input_file):
file = open(input_file)
lines = file.read().splitlines()
return lines
@staticmethod
def parse_cluster_dict(cluster_dict):
with open(cluster_dict) as f:
data = json.load(f)
return data
@staticmethod
def save_list_to_file(output_file, str_list, mode='w'):
with open(output_file, mode) as f:
for item in str_list:
f.write("%s\n" % item)
@staticmethod
def save_line_to_file(output_file, line, mode='w'):
with open(output_file, mode) as f:
f.write(line + '\n')
@staticmethod
def save_dictionary_as_json(output_file, output):
output_file = open(output_file, "w+")
output_file.write(StringUtils.get_dictionary_as_json(output))
output_file.close()
@staticmethod
def tokenize_multi_word_strings(tokens):
normalized_tokens = []
for token in tokens:
normalized_tokens.append(token.split())
return normalized_tokens
@staticmethod
def remove_quotes(text):
text = text.replace("\"", "")
text = text.replace("\'", "")
return text
@staticmethod
def remove_brackets(text):
text = text.replace("[", "")
text = text.replace("]", "")
return text
@staticmethod
def remove_non_alphanumeric_chars(text):
regex = re.compile('[^a-zA-Z0-9, ]')
return regex.sub('', text)