-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwlfCleanup.py
43 lines (35 loc) · 1.52 KB
/
bwlfCleanup.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
###############
# B(eo)W(u)LF Code:
# By-Word Long-Form Data Preparation
# This code reads in a sample text file, reformats it, and exports it to
# a new file before further formatting and analysis.
# Written by:
# Date last modified: June 16, 2013
###############
# coding:utf-8
import os,re,unicodedata,shlex,glob
# read in text file
os.chdir('~/bwlfTechReport/')
beowulfText = open('beowulfTextSnippet.txt','r') # open file
beowulf = beowulfText.read() # read in text
# start the cleanup (change according to text)
beowulf = re.sub('\r','\n',beowulf) # ensure all newlines are identical
beowulf = re.sub('(^|\n)([A-Z]{2,}( |,))','\n\n[canto]\n\\2',beowulf) # create canto indicator
beowulf = re.sub(':|;|(\-)|,',' ',beowulf) # remove non-target punctuation
beowulf = re.sub('(\.){3,}','.',beowulf)
beowulf = re.sub(' {1,}',' ',beowulf) # remove redundant spaces
# convert all text to lower case
beowulf = beowulf.lower()
# split by spaces
beowulf = re.sub('\n(?=([A-Z]|[a-z]|\"))',' [line] ',beowulf) # create line indicator
beowulf = re.sub('\n',' ',beowulf) # change newline to space
beowulf = re.split(' +',beowulf) # split file by space
beowulf = str(beowulf) # convert to a string
beowulf = re.sub('((\')|("))\, ((\')|("))',',',beowulf) # remove extraneous quotations
beowulf = re.sub('\,{2}',',',beowulf) # remove empty cells
beowulf = re.sub('(\[\'(,)?)|(\'\])','',beowulf) # remove extraneous brackets
# close file and print new file
beowulfText.close()
cleaned = file('beowulfCleaned.csv','w')
cleaned.write(beowulf)
cleaned.close()