diff --git a/py-scripts/word-tags.py b/py-scripts/word-tags.py new file mode 100644 index 0000000..e3af8e6 --- /dev/null +++ b/py-scripts/word-tags.py @@ -0,0 +1,81 @@ +import re +import os + +""" +Replace every space with a special character and then re-replace them? You lose the space between `:«` with current method. +""" + +path = __file__ + +directory = os.path.abspath(os.getcwd()) + +def add_tags(filename): + """ + The filenames should be -.txt + """ + print('Adding tags to: ' + filename) + xmlfile = filename + '.xml' + tmpxmlfile = filename + '.tmp.xml' + with open(xmlfile,'w') as Ixml: + with open(tmpxmlfile,'w') as Ixmltmp: + with open(filename,'r') as Itxt: + for spaced_line in Itxt: + line = re.sub(' ','~',spaced_line) + words = line.split('~') + for i in range(len(words)): + word = words[i] + # Iterate from ending to beginning of word + L = len(word) - 1 + if L == 0: + before_end_word_chars = word + ending_punct_chars = '' + elif L == 1 and '\n' in word: + print('Got HERE') + before_end_word_chars = word.rstrip() + ending_punct_chars = '\n' + else: + print('Got to <- with: ' + word + ' and this L: ' + str(L) + ' in ' + filename) + # we need `word[L] != '-'` because there are some words like `-:` + while word[L] != '’' and word[L] != '-' and not word[L].isalpha(): + L-=1 + else: + ending_punct_begin_index = L + 1 + ending_punct_chars = word[ending_punct_begin_index:] + before_end_word_chars = word[:ending_punct_begin_index] + # Iterate from start to remainder of word + L = len(word) - 1 + if L == 0: + middle_word_chars = word + starting_punct_chars = '' + elif L == 1 and '\n' in word: + print('Got HERE!!!') + middle_word_chars = word.rstrip() + starting_punct_chars = '' + else: + print('Got -> with this word: ' + word + ' and this L: ' + str(L) + ' in ' + filename) + while word[-L - 1] != '‘' and word[-L - 1] != '’' and word[L] != '-' and not word[-L - 1].isalpha(): + L-=1 + else: + starting_punct_end_index = -L - 1 + starting_punct_chars = word[:starting_punct_end_index] + middle_word_chars = before_end_word_chars[-L-1:] + #print(starting_punct_chars + ' ' + middle_word_chars + ' ' + ending_punct_chars + ' ' + str(L)) + new_word = starting_punct_chars + '' + middle_word_chars + '' + ending_punct_chars + Ixmltmp.write(new_word) + Itxt.close() + Ixmltmp.close() + with open(tmpxmlfile,'r') as Ixmltmp: + for line in Ixmltmp: + #line_spaced = re.sub(pattern,repl,string,count=0,flags=0) + def add_space(match): + match = match.group() + return re.sub('', ' ', match) + line_spaced = re.sub('.*?', add_space, line) + Ixml.write(line_spaced) + Ixmltmp.close() + os.remove(tmpxmlfile) + Ixml.close() + +for f in os.listdir(directory): + if f != 'word-tags.py' and f != '.word-tags.py.swp': + add_tags(f)