-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
27 lines (23 loc) · 888 Bytes
/
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
import os
def read_corpus(filename):
try:
with open(os.path.join('corpora', filename + '.txt')) as file:
corpus = file.read()
return corpus
except FileNotFoundError as fnf:
raise fnf
def save_corpus(filename, corpus):
if filename is "":
raise ValueError("Invalid filename! Name is empty")
if os.path.exists(os.path.join('corpora', filename + '.txt')):
raise FileExistsError("This name already exists! Please enter a different name.")
with open(os.path.join('corpora', filename + '.txt'), 'w') as file:
file.write(corpus)
return
def remove_corpus(filename):
try:
os.remove(os.path.join('corpora', filename + '.txt'))
except FileNotFoundError as fnf:
raise fnf
def list_corpora():
return [f[:-4] for f in os.listdir('corpora') if os.path.isfile(os.path.join('corpora', f))]