//
// Latex Project Public Licence (lppl1.3) applies
// Originally written by Kwanleung Tse
// First Release : 1st July, 2014
// Last Update : 27th September, 2014
//
//
package worker4math.hktex2;

public class Hyphenator {

    protected String hyphenate(String word) {
    	
        int i,i2,i3,i4=0;
        int ii=0;
        int jj=0;
        int l=0;
        int l2=0;
  

        KnuthLiangTable pattern = new KnuthLiangTable();
        pattern.list();


        String hyphenatedword = "";
        String wordi="";
        l=word.length();
        if (l > 2){
        	for (i=0;i<=l-2;i++){
        		wordi=wordi+word.charAt(i)+"0";
        	}
        	wordi=".0"+wordi+word.charAt(l-1)+"0.";
        	word="."+word+".";
        	String word1="";
        	String word2="";
        	String word3="";
        	l=word.length();
        	int starti = 0;
        	for (i2=starti; i2<=l-2; i2++){
        		int ll=l-1-i2;
        		i3=i2+Math.min(ll, 7);
        		for (i4=i2+2;i4<=i3;i4++){
        			word1=word.substring(i2,i4);
        			if (pattern.pattern2.contains(word1)){
        				i=pattern.pattern2.headSet(word1).size();
        				word2=pattern.pattern1[i];
        				l2=word2.length();
        				jj=0;
        				word3="";
        				for (ii=0;jj<=l2-1;ii++){
        					if (word2.charAt(jj)==  '1' || word2.charAt(jj)== '2' 
        							|| word2.charAt(jj)== '3' || word2.charAt(jj)== '4' 
        							|| word2.charAt(jj)== '5')
        					{if (word3.length()>=1){word3=word3.substring(0,word3.length()-1)+word2.charAt(jj);}else{word3=word2.substring(jj,jj+1);};
        					jj++;			 
        					}
        					else {
        						word3=word3+word2.charAt(jj)+"0";
        						jj++;   	 				
        					};    	 			 
        				};
        				l2=word3.length();
        				if (word3.charAt(0) == '1' || word3.charAt(0) == '2' || word3.charAt(0) == '3'|| word3.charAt(0) == '4' || word3.charAt(0) == '5'){
        					for (ii=0;ii<=l2-1;ii+=1){
        						if(word3.charAt(ii)>wordi.charAt(i2*2+ii-1)){
        							wordi=wordi.substring(0,i2*2+ii-1)+word3.charAt(ii)+wordi.substring(i2*2+ii);}
        					};    	 		
        				} else {
        					for (ii=0;ii<=l2-1;ii+=1){
        						if(word3.charAt(ii)>wordi.charAt(i2*2+ii)){
        							wordi=wordi.substring(0,i2*2+ii)+word3.charAt(ii)+wordi.substring(i2*2+ii+1);}
        					};    	 		
        				};
        			}
        		}        	
        	};
        	l=wordi.length();
        	word=wordi.substring(2,3)+wordi.substring(4,5);
        	for (i=5;i<=l-5;i+=2){
        		if (wordi.charAt(i)== '1' || wordi.charAt(i) == '3' || wordi.charAt(i) == '5'){word=word+"-"+wordi.charAt(i+1);}
        		else {word=word+wordi.charAt(i+1);};
        	}
        	hyphenatedword=word+wordi.substring(l-3,l-2);
        }
        else { hyphenatedword=word;}
        return hyphenatedword;
    }}