1   package org.daisy.pipeline.nlp.lexing;
2   
3   import java.util.List;
4   import java.util.regex.Matcher;
5   import java.util.regex.Pattern;
6   
7   import org.daisy.pipeline.nlp.lexing.LexService.Sentence;
8   import org.daisy.pipeline.nlp.lexing.LexService.TextBoundaries;
9   
10  /**
11   * Convert one Lexer's result to formatted text. Likely to be used for testing.
12   */
13  public class LexResultPrettyPrinter {
14  
15  	private String mWordSeparator = "/";
16  	private boolean mTrimming = false;
17  
18  	public void setWordSeperator(String wordSeparator) {
19  		mWordSeparator = wordSeparator;
20  	}
21  
22  	public void enableTrimming(boolean mode) {
23  		mTrimming = mode;
24  	}
25  
26  	public String convert(Object result, String lexerInput) {
27  		StringBuilder sb = new StringBuilder();
28  		dispatch(result, sb, lexerInput);
29  		return sb.toString();
30  	}
31  
32  	private void dispatch(Object v, StringBuilder sb, String reference) {
33  		if (v instanceof List) {
34  			visit((List<Sentence>) v, sb, reference);
35  		} else if (v instanceof Sentence) {
36  			visit((Sentence) v, sb, reference);
37  		} else if (v instanceof TextBoundaries) {
38  			visit((TextBoundaries) v, sb, reference, mWordSeparator, mWordSeparator);
39  		}
40  	}
41  
42  	private void visit(TextBoundaries t, StringBuilder sb, String reference,
43  	        String separatorLeft, String separatorRight) {
44  		String v = reference.substring(t.left, t.right);
45  		if (mTrimming) {
46  			Matcher m = Pattern.compile("^\\s+").matcher(v);
47  			if (m.find()) {
48  				sb.append(m.group()); //move the white spaces before the separator
49  				if (m.group().length() == v.length()) {
50  					//empty word
51  					return;
52  				}
53  				v = v.substring(m.end());
54  			}
55  			m = Pattern.compile("\\s+$").matcher(v);
56  			if (m.find()) {
57  				if (m.start() != 0) {
58  					sb.append(separatorLeft);
59  					sb.append(v.substring(0, m.start()));
60  					sb.append(separatorRight);
61  					sb.append(m.group()); //move the white spaces after the separator
62  				} else
63  					sb.append(v); //empty word: only white spaces
64  			} else {
65  				sb.append(separatorLeft);
66  				sb.append(v);
67  				sb.append(separatorRight);
68  			}
69  		} else {
70  			sb.append(separatorLeft);
71  			sb.append(v);
72  			sb.append(separatorRight);
73  		}
74  	}
75  
76  	private void visit(List<Sentence> sentences, StringBuilder sb, String references) {
77  		for (Sentence s : sentences)
78  			dispatch(s, sb, references);
79  	}
80  
81  	private void visit(Sentence s, StringBuilder sb, String reference) {
82  		if (s.words == null || s.words.isEmpty()) {
83  			sb.append("{");
84  			String v = reference.substring(s.boundaries.left, s.boundaries.right);
85  			if (mTrimming)
86  				v = v.trim();
87  			sb.append(v);
88  			sb.append("}");
89  		} else {
90  			int lastPos = s.boundaries.left;
91  			if (mTrimming) {
92  				String v = reference.substring(lastPos, s.words.get(0).left);
93  				Matcher m = Pattern.compile("^\\s+").matcher(v);
94  				if (m.find()) {
95  					//sb.append(m.group()); //would move the white spaces before the {
96  					lastPos += m.end();
97  				}
98  			}
99  			sb.append("{");
100 			for (TextBoundaries word : s.words) {
101 				sb.append(reference.substring(lastPos, word.left));
102 				lastPos = word.right;
103 				visit(word, sb, reference, mWordSeparator, mWordSeparator);
104 			}
105 			String v = reference.substring(lastPos, s.boundaries.right);
106 			Matcher m = Pattern.compile("\\s+$").matcher(v);
107 			if (mTrimming && m.find()) {
108 				sb.append(v.substring(0, m.start()));
109 				sb.append("}");
110 				//sb.append(m.group()); //would move the white spaces after the }
111 			} else {
112 				sb.append(v);
113 				sb.append("}");
114 			}
115 		}
116 	}
117 }