#!/usr/bin/env python3

import pyphen
import argparse
import sys
import re

parser = argparse.ArgumentParser(
    				description='A script to "syllabify" (insert a character between all syllables) a file.',formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-t', '--text-type',
                    help='text type (chant or prose)',
                    default='prose', dest='type')
parser.add_argument('-m', '--hyphen-mode',
                    help='Hyphenation mode (phonetic or etymology)',
                    default='phonetic', dest='mode')
parser.add_argument('-i', '--input', nargs='?', type = argparse.FileType('r'),
					default=sys.stdin, dest='inputfile')
parser.add_argument('-o', '--output-file', nargs='?', type=argparse.FileType('w'),
                    default=sys.stdout, dest='outputfile')
parser.add_argument('-c', '--hyphen-char', nargs='?',
                    default='-', dest='hyphenchar')

args = parser.parse_args()

righthyphenmin = 2
lefthyphenmin = 2
cutvowels = False
if (args.type == 'chant'):
	righthyphenmin=1
	lefthyphenmin=1
	cutvowels = True

hyphenator = pyphen.Pyphen(filename='../patterns/hyph_la_'+args.mode+'.dic',left=lefthyphenmin,right=righthyphenmin)

def hyphenate_one_word(word):
	global hyphenator,args
	return hyphenator.inserted(word,args.hyphenchar)

wordregex = re.compile(r'\b[^\W\d_]+\b')

for line in args.inputfile:
	line = line.strip()
	hyphenline = wordregex.sub(lambda match: hyphenate_one_word(match.group(0)), line)
	args.outputfile.write(hyphenline+'\n')

args.inputfile.close()
args.outputfile.close()
@@ -17,16 +17,1407 @@
 ython3%0A%0A
+%22%22%22%0A    Syllabification script%0A%0A    Copyright (C) 2016 Elie Roux%0A%0A    Permission is hereby granted, free of charge, to any person obtaining a copy of%0A    this software and associated documentation files (the %22Software%22), to deal in%0A    the Software without restriction, including without limitation the rights to%0A    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies%0A    of the Software, and to permit persons to whom the Software is furnished to do%0A    so, subject to the following conditions:%0A%0A    The above copyright notice and this permission notice shall be included in all%0A    copies or substantial portions of the Software.%0A%0A    THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE%0A    SOFTWARE.%0A%0A    Depends on pyphen: http://pyphen.org/%0A    You also need the hyph_la_liturgical.dic file. To get it, get the%0A      hyphen-la project on https://github.com/gregorio-project/hyphen-la%0A      and run %22make%22 in the %22patterns%22 directory.%0A%0A%22%22%22%0A%0A
 import p

add license on syllabifier