| 1 |
#! /usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
A script to check the syllabation of a gabc file |
| 5 |
|
| 6 |
See checkSyllabation.py -h for help |
| 7 |
|
| 8 |
Copyright (C) 2016-2025 Elie Roux |
| 9 |
|
| 10 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 11 |
this software and associated documentation files (the "Software"), to deal in |
| 12 |
the Software without restriction, including without limitation the rights to |
| 13 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 14 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 15 |
so, subject to the following conditions: |
| 16 |
|
| 17 |
The above copyright notice and this permission notice shall be included in all |
| 18 |
copies or substantial portions of the Software. |
| 19 |
|
| 20 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 |
SOFTWARE. |
| 27 |
|
| 28 |
Depends on pyphen: http://pyphen.org/ |
| 29 |
You also need the hyph_la_liturgical.dic file. To get it, get the |
| 30 |
hyphen-la project on https://github.com/gregorio-project/hyphen-la |
| 31 |
and run "make" in the "patterns" directory. |
| 32 |
|
| 33 |
""" |
| 34 |
|
| 35 |
|
| 36 |
import os |
| 37 |
import sys |
| 38 |
import re |
| 39 |
import glob |
| 40 |
import argparse |
| 41 |
import pyphen |
| 42 |
|
| 43 |
|
| 44 |
DEFAULT_OUTFILE = False |
| 45 |
if os.name == 'nt': |
| 46 |
DEFAULT_OUTFILE = 'check-syllabation.log' |
| 47 |
|
| 48 |
def get_parser(): |
| 49 |
"Return command line parser" |
| 50 |
parser = argparse.ArgumentParser( |
| 51 |
description='A script to check the syllabation of gabc files') |
| 52 |
parser.add_argument('-p', '--pat-path', |
| 53 |
help='hyphenation pattern file', |
| 54 |
action='store', default="hyph_la_liturgical.dic") |
| 55 |
parser.add_argument('-o', '--outfile', |
| 56 |
help='The file that will contain the report', |
| 57 |
action='store', default=DEFAULT_OUTFILE) |
| 58 |
parser.add_argument('-v', '--verbose', |
| 59 |
help='Report also files without error', |
| 60 |
action='store_true', default=False) |
| 61 |
parser.add_argument('path', |
| 62 |
help='Path to a gabc file or a directory containing gabc files', |
| 63 |
action='store', default=".") |
| 64 |
return parser |
| 65 |
|
| 66 |
def deacc(accstr): |
| 67 |
"Remove accents from vowels" |
| 68 |
return accstr.replace('á', 'a').replace('é', 'e').replace('í', 'i').replace('ó', 'o').replace('ú', 'u').replace('ý', 'y').replace('́', '').replace('ǽ', 'æ') |
| 69 |
|
| 70 |
def checkwords(words_list, hyphenator): |
| 71 |
errors = [] |
| 72 |
for word in words_list: |
| 73 |
initialword = deacc(word.lower()) |
| 74 |
if initialword.find('-') == -1: |
| 75 |
# no need for noise |
| 76 |
continue |
| 77 |
correctword = hyphenator.inserted(initialword.replace('-','')) |
| 78 |
if correctword != initialword: |
| 79 |
errors.append( (initialword,correctword) ) |
| 80 |
return errors |
| 81 |
|
| 82 |
def get_words_list(gabc_content): |
| 83 |
gabc_content = gabc_content.split('%%\n', 1)[1] # no headers |
| 84 |
gabc_content = re.sub(r'%.*\n', '', gabc_content) |
| 85 |
gabc_content = gabc_content.replace('\n', ' ').replace('\r', ' ').replace('{','') |
| 86 |
gabc_content = gabc_content.replace('}','').replace('<sp>\'ae</sp>', 'ǽ') |
| 87 |
gabc_content = gabc_content.replace('<sp>oe</sp>', 'œ').replace('<sp>\'oe</sp>', 'œ') |
| 88 |
gabc_content = gabc_content.replace('<sp>ae</sp>', 'æ').replace('<sp>\'æ</sp>', 'ǽ') |
| 89 |
gabc_content = gabc_content.replace('<sp>\'œ</sp>', 'œ') |
| 90 |
gabc_content = re.sub(r'\([^\)]*\)', '-', gabc_content) |
| 91 |
gabc_content = re.sub(r'<\/?[ibuec]>', '', gabc_content) |
| 92 |
gabc_content = re.sub(r'<\/?sc>', '', gabc_content) |
| 93 |
gabc_content = re.sub(r'<\/?eu>', '', gabc_content) |
| 94 |
gabc_content = re.sub(r'<v>[^<]*<\/v>', '', gabc_content) |
| 95 |
gabc_content = re.sub(r'<v>[^>]*<\/v>', '', gabc_content) |
| 96 |
gabc_content = re.sub(r'<sp>[^>]*<\/sp>', '', gabc_content) |
| 97 |
gabc_content = re.sub(r'<alt>[^>]*<\/alt>', '', gabc_content) |
| 98 |
gabc_content = re.sub(r'\[[^\]]*\]', '', gabc_content) |
| 99 |
gabc_content = re.sub(r'-+', '-', gabc_content) |
| 100 |
gabc_content = re.sub(r'-?(\s+|$)', ' ', gabc_content) |
| 101 |
gabc_content = re.sub(r'[^a-záéíóæúýœǽ\u0301 -]', '', gabc_content, flags=re.IGNORECASE) |
| 102 |
gabc_content = re.sub(r'(^|\s+)-', ' ', gabc_content) |
| 103 |
return gabc_content.split() |
| 104 |
|
| 105 |
def get_file_list(path): |
| 106 |
"Generate list of files to parse" |
| 107 |
if os.path.isfile(path): |
| 108 |
return [path] |
| 109 |
if os.path.isdir(path): |
| 110 |
files = glob.glob(os.path.join(path, '**/*.gabc'), recursive=True) |
| 111 |
files = sorted(files) |
| 112 |
return files |
| 113 |
print(f'Error! Cannot find {path}', file=sys.stderr) |
| 114 |
sys.exit(1) |
| 115 |
|
| 116 |
def check_file(filepath, hyphenator, outfd, report_no_error=False): |
| 117 |
words_list = [] |
| 118 |
with open(filepath, 'r', encoding='utf8') as inputf: |
| 119 |
words_list = get_words_list(inputf.read()) |
| 120 |
errors = checkwords(words_list, hyphenator) |
| 121 |
nb_errors = len(errors) |
| 122 |
if nb_errors > 0 or report_no_error : |
| 123 |
outfd.write('analyzing '+filepath+':\n') |
| 124 |
if nb_errors == 0: |
| 125 |
outfd.write(' no error!\n') |
| 126 |
else: |
| 127 |
for t in errors: |
| 128 |
outfd.write(' '+t[0]+' should be '+t[1]+'\n') |
| 129 |
outfd.write('\n') |
| 130 |
return nb_errors |
| 131 |
|
| 132 |
def main(): |
| 133 |
"Main function" |
| 134 |
parser = get_parser() |
| 135 |
if len(sys.argv) == 1: |
| 136 |
parser.print_help() |
| 137 |
sys.exit(1) |
| 138 |
args = parser.parse_args() |
| 139 |
hyphenator = pyphen.Pyphen(filename=args.pat_path,left=1,right=1) |
| 140 |
outfd = sys.stdout |
| 141 |
if args.outfile != False: |
| 142 |
outfd = open(args.outfile, 'w', encoding='utf8') |
| 143 |
file_list = get_file_list(args.path) |
| 144 |
nb_errors = 0 |
| 145 |
for filename in file_list: |
| 146 |
nb_errors += check_file(filename, hyphenator, outfd, args.verbose) |
| 147 |
if len(file_list) > 1 and nb_errors > 0: |
| 148 |
outfd.write('Total errors: '+str(nb_errors)+'\n') |
| 149 |
elif nb_errors == 0 and not args.verbose: |
| 150 |
outfd.write('No error!\n') |
| 151 |
if outfd is not sys.stdout: |
| 152 |
outfd.close() |
| 153 |
if nb_errors == 0: |
| 154 |
sys.exit(0) |
| 155 |
else: |
| 156 |
sys.exit(2) |
| 157 |
|
| 158 |
if __name__ == "__main__": |
| 159 |
main() |