import java.util.regex.*; 
import java.io.*;

// Copyright (C) 2007  Rafael C. Carrasco
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// Adapted from José A. Mañas in Communications of the ACM 30(7), 1987.
 
/**
 * A class that performs simple hypenation of Spanish words.
 * It does not work with foreign words as pa-ra-psi-co-lo-gía: 
 * hyphenated as in cáp-su-la.
 */
class Hyphenator {
    String v = "[aáeéiíoóuúü]";            // vowels
    String a = "[aáeéíoóú]", i = "[iuü]";  // open and closed vowels
    String c = "[bcdfghjklmnñpqrstvxyz]";  // consonants       
    String r = "[hlr]";                    // liquid and mute consonants
    String b = "[bcdfgjkmnñpqstvxyz]";     // non-liquid consonants
    
    String[] P;
   
    Pattern pattern;
    Matcher matcher;

    public Hyphenator () {
	P = new String[9];
	P[1] = i + "h" + i;
	P[2] = a + "h" + i;
	P[3] = i + "h" + a;
	P[4] = "." + c + r + v;
	P[5] =  c + r + v;
	P[6] = "." + c + v;
	P[7] = a + a;
	P[8] = ".";
	String all = "(" + P[1] + ")|("  + P[2] + ")|(" + P[3]  
	    + ")|(" + P[4] +  ")|("  + P[5] + ")|(" + P[6] 
	    + ")|("  + P[7] + ")|(" + P[8] + ")";
	
	pattern = Pattern.compile ( all, Pattern.CASE_INSENSITIVE );

    }
    
/**
 * Return first matching pattern.
 */
    private int getGroup () {
	int gc = matcher.groupCount();
	for ( int i = 1; i <= gc; ++i ) {
	    if ( matcher.end() == matcher.end(i) ) 
		return i;
	}
	return -1;
    }
    
/**
 * Hyphenates a word.
 * @param The word to be hyphenated.
 * @return hyphenation.
 */
    String parse ( String input ) {
	StringBuffer output = new StringBuffer( 2*input.length() );
	int pos = 0;
	matcher = pattern.matcher( input );
	while ( matcher.find() ) { 
	    output.append( input.charAt(pos) );
	    switch ( getGroup() ) {
		case 4 :
		    output.append('-'); 		    
		    break;
		case 6 :
		    output.append('-'); 		   
		    break;
		case 7 :
		    output.append('-');
		    break;
		default : 
		    break;
	    }
	    matcher.region(++pos, matcher.regionEnd());
	}
	return output.toString();
    }



    public static void main ( String[] args ) {
	Hyphenator hyphen = new Hyphenator();
	BufferedReader in;
	String[] words;
	String separator = "(\\p{Space}|\\p{Punct}|[«»])+";
	
	if ( args.length == 0 ) {
	    System.err.println( "Usage: java Hyphenator -f file1 file2 ..." );
	} else if ( args[0].equals("-f") ) {
	    for ( String file : args ) {
		try { 
		    in = new BufferedReader ( new FileReader(file) ); 
		    while ( in.ready() ) {
			words = in.readLine().split(separator);
			for ( String word : words ) {
			    if ( word.length() > 0 ) {
				System.out.println( word + " " +
						    hyphen.parse( word ) );
			    }
			}
		    }
		} catch ( Exception x ) {
		    System.err.println("Cannot open file " + file);
		}
	    }
	} else {
	    for ( String word : args ) {
		if ( word.length() > 0 )
		    System.out.println( hyphen.parse( word ) );
	    }
	}
    }
}
  

