-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFilePreprocessing.py
More file actions
60 lines (41 loc) · 1.96 KB
/
AbstractFilePreprocessing.py
File metadata and controls
60 lines (41 loc) · 1.96 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
""" import statements """
import os
import re
class AbstractFilePreprocessing:
""" abstract class for preprocessing """
@staticmethod
def string_transformation(input_string):
""" Performs a string transformation on a string object with stemming/lemmatization. """
pass
@staticmethod
def save_bag_of_words(path_to_corpus, name_of_target_file):
""" Creates a dictionary as data structure: filepath -> bag of words. """
pass
# private methods
@staticmethod
def __get_paths_to_resource_files__(root_directory):
""" This method returns a string-array with complete file paths for each file.
It accepts the rootDirectory of the resource files (String format). """
# get all subdirectories in pathToSourceDirectory
subdirectories = [path for path in os.listdir(root_directory)
if os.path.isdir(os.path.join(root_directory, path))]
# list with all files
all_files = []
# get all files
for directory in subdirectories:
complete_path_to_directory = root_directory + "/" + directory
files_in_directory = [file for file in os.listdir(complete_path_to_directory)
if os.path.isfile(os.path.join(complete_path_to_directory, file))]
for file in files_in_directory:
complete_path_to_file = complete_path_to_directory + "/" + file
all_files.append(complete_path_to_file)
return all_files
@staticmethod
def __multiple_replace__(text, adictionary):
""" input: text where occurrences specified in the input dictionary will be replaced
output: string with replaced parts """
word = re.compile('|'.join(map(re.escape, adictionary)))
def get_match(match):
""" return match of dictionary array """
return adictionary[match.group(0)]
return word.sub(get_match, text)