Révision 1226

tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/NormalizeKeys.java (revision 1226)
1 1
package org.txm.rcp.translate.devtools;
2 2

  
3
import java.util.HashSet;
4

  
3 5
import org.eclipse.osgi.util.NLS;
6
import org.txm.utils.BiHashMap;
4 7

  
5 8
/**
6 9
 * Normalizes the keys using the message words.
7
 * eg. : "Error while computing" => errorWhileComputing
10
 * eg. : "Error while computing." => errorWhileComputing
8 11
 * 
9 12
 * 
10 13
 * @author mdecorde
14
 * @author sjacquot
11 15
 *
12 16
 */
13 17
public class NormalizeKeys {
14
	static {
18
	
19
	public static boolean debug = true;
20
	
21
	
22
	
23
	public static BiHashMap<String, String> normalize(BiHashMap<String, String> messages)	{
15 24
		
25
		for (String key : messages.getKeys()) {
26
			messages.put(normalize(messages.get(key)), messages.get(key));
27
		}
28
		
29
		return messages;
16 30
	}
17 31
	
32
	/**
33
	 * 
34
	 * @param strings
35
	 * @return
36
	 */
37
	public static HashSet<String> normalize(HashSet<String> strings)	{
38
		for (String str : strings) {
39
			if(debug)	{
40
				System.out.println(str);
41
				System.out.println("   => " + normalize(str));
42
			}
43
		}
44
		
45
		return strings;
46
	}
47
	
48
	
49
	/**
50
	 * 
51
	 * @param str
52
	 */
53
	public static String normalize(String str)	{
54

  
55
		// log
56
		if(debug)	{
57
			System.out.println(str);
58
		}
59
		
60
		str = str.replaceAll("[^a-zA-Z0-9 ]", "").trim();
61
		
62
		// empty string
63
		if(str.length() == 0)	{
64
			System.err.println("NormalizeKeys.normalize(): warning: empty string.");
65
		}
66
		else	{
67
			String[] words = str.split(" ");
68
			str = "";
69
			for (int i = 0; i < words.length; i++) {
70
				str += words[i].substring(0, 1).toUpperCase();
71
				try {
72
					str += words[i].substring(1);
73
				}
74
				catch (Exception e) {
75
					// nothing to do
76
				}
77
			}
78
			
79
			// uncapitalize first letter
80
			String tmpString = str.substring(0, 1).toLowerCase();
81
			try {
82
				tmpString += str.substring(1);
83
			}
84
			catch (Exception e) {
85
				// nothing to do
86
			}
87
			str = tmpString;
88
		}
89

  
90
		// log
91
		if(debug)	{
92
			System.out.println("   => " + str);
93
		}
94
		
95
		return str;
96
	}
97
	
98
	/**
99
	 * 
100
	 * @param args
101
	 */
18 102
	public static void main(String[] args) {
103

  
104
		// tests
19 105
		System.out.println(NLS.bind("lol {0}", 1));
106
		
107
		BiHashMap<String, String> messages = new BiHashMap<String, String>();
108
		messages.put("Convert5To6_46", "Error while computing."); 
109
		messages.put("Convert5To6_46", "** Error: the connexion to the server failed: wrong port format");
110
		messages.put("Convert5To6_6", "** Error: ''{0}'' corpus directory is not conformant to TXM corpus binary format: corpus skipped.");
111
		messages.put("Convert5To6_27", "T");
112
		messages.put("Convert5To6_28", "");
113
		messages = normalize(messages);
114
		System.out.println("NormalizeKeys.main()");
115
		
116
//		HashSet<String> strings = new HashSet<String>();
117
//		strings.add("Error while computing.");
118
//		strings.add("** Error: the connexion to the server failed: wrong port format");
119
//		strings.add("** Error: ''{0}'' corpus directory is not conformant to TXM corpus binary format: corpus skipped.");
120
//		strings.add("T");
121
//		strings.add("");
122
//		normalize(strings);
123
		
20 124
	}
125
	
126
	
127
	
21 128
}

Formats disponibles : Unified diff