OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 108 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
108 ilm 1
package org.openconcerto.modules.ocr.parser;
2
 
3
import java.util.Calendar;
4
import java.util.Date;
5
 
6
public class ParserUtils {
7
    public static Date parseDate(String text) {
8
        Date result = null;
9
        if (text.length() == 8) {
10
            text = text.substring(0, 2) + "." + text.substring(2, 4) + "." + text.substring(4, 8);
11
        } else if((result = getFromFrenchDate(text)) != null){
12
            return result;
13
        } else {
14
            text = text.replace("/", ".").replace("\\", ".").replace("'", ".").replace("-", ".").replace("‘", ".").replace(" ", "");
15
        }
16
        final int textLength = text.length();
17
        final StringBuilder date = new StringBuilder();
18
        for(int i = 0; i < textLength; i++){
19
            final String charac = text.substring(i, i + 1);
20
            if(isInteger(charac)){
21
                date.append(charac);
22
                for(int j = i + 1; j < textLength && date.length() < 10; j++){
23
                    final String charac2 = text.substring(j, j + 1);
24
                    if(charac2.equals(".") || isInteger(charac2)){
25
                        date.append(charac2);
26
                    } else if(!charac2.equals(" ")){
27
                        break;
28
                    }
29
                }
30
                final String[] testDate = date.toString().split("\\.");
31
                final Calendar c = Calendar.getInstance();
32
                int day, month, year = -1, swap;
33
                if (testDate.length > 2) {
34
                    try {
35
                        day = Integer.parseInt(testDate[0]);
36
                        month = Integer.parseInt(testDate[1]) - 1;
37
                        if(testDate[2].length() == 2){
38
                            year = Integer.parseInt("20" + testDate[2]);
39
                        } else {
40
                            year = Integer.parseInt(testDate[2]);
41
                        }
42
                        if (month > 12 && month <= 31 && day <= 12) {
43
                            swap = month;
44
                            month = day;
45
                            day = swap;
46
                        }
140 ilm 47
                        if (month < 12 && month >= 0 && day <= 31 && day > 0 && year > 2000 && year < 4000) {
108 ilm 48
                            c.set(Calendar.DAY_OF_MONTH, day);
49
                            c.set(Calendar.MONTH, month);
50
                            c.set(Calendar.YEAR, year);
51
                            result = c.getTime();
52
                            break;
53
                        }
54
                    } catch (Exception ex) {
55
                        // nothing
56
                    }
57
                }
58
                date.delete(0, date.length());
59
            }
60
        }
61
        return result;
62
    }
63
 
64
    public final static String getCleanNumberString(String s) {
65
        if (s.indexOf('.') >= 0 && s.indexOf(',') >= 0) {
66
            s = s.replace('.', ' ').replace(',', '.');
67
        } else if (s.indexOf('.') < 0 && s.indexOf(',') >= 0) {
68
            s = s.replace(',', '.');
69
        }
70
 
71
        final StringBuilder b = new StringBuilder();
72
        final int length = s.length();
73
        for (int i = 0; i < length; i++) {
74
            final char c = s.charAt(i);
75
            if (Character.isDigit(c) || c == '.') {
76
                b.append(c);
77
            }
78
        }
79
        return b.toString();
80
 
81
    }
82
 
83
    public final static boolean isLong(String text) {
84
        try {
85
            Long.parseLong(text);
86
            return true;
87
        } catch (Exception ex) {
88
            return false;
89
        }
90
    }
91
 
92
    public final static boolean isInteger(String text) {
93
        try {
94
            Integer.parseInt(text);
95
            return true;
96
        } catch (Exception ex) {
97
            return false;
98
        }
99
    }
100
 
101
    public static boolean compareDate(Date date1, Date date2){
102
        final Calendar cal = Calendar.getInstance();
103
        cal.setTime(date1);
104
        final int year1 = cal.get(Calendar.YEAR), month1 = cal.get(Calendar.MONTH), day1 = cal.get(Calendar.DATE);
105
        cal.setTime(date2);
106
        final int year2 = cal.get(Calendar.YEAR), month2 = cal.get(Calendar.MONTH), day2 = cal.get(Calendar.DATE);
107
        return (year1 == year2 && month1 == month2 && day1 == day2 );
108
    }
109
 
110
    private static Date getFromFrenchDate(String frenchText) {
111
        final int i = frenchText.indexOf("20");
112
        int year = -1;
113
        if (i != -1 && frenchText.length() >= i + 4) {
114
            try {
115
                year = Integer.parseInt(frenchText.substring(i, i + 4));
116
            } catch (Exception e) {
117
                // nothing
118
            }
119
            frenchText = frenchText.toLowerCase();
120
            int month = getFrenchMonth(frenchText.substring(0, i));
121
            if (year < 2000 || month <= 0) {
122
                return null;
123
            }
124
 
125
            final String[] split = frenchText.split("\\s+");
126
            final Calendar d = Calendar.getInstance();
127
            int day = 0;
128
            for (int j = 0; j < split.length; j++) {
129
                String sDay = split[j];
130
                try {
131
                    day = Integer.parseInt(sDay);
132
                    if (day > 0 && day < 32) {
133
                        break;
134
                    }
135
                } catch (Exception e) {
136
                    // nothing
137
                }
138
                try {
139
                    if (day != year) {
140
                        sDay = sDay.substring(0, 2);
141
                        day = Integer.parseInt(sDay);
142
                        if (day > 0 && day < 32) {
143
                            break;
144
                        }
145
                    }
146
                } catch (Exception e) {
147
                    // nothing
148
                }
149
            }
150
            if (day > 0) {
151
                d.set(year, month, day, 0, 0, 0);
152
            } else {
153
                d.set(year, month, 1, 0, 0, 0);
154
            }
155
            return d.getTime();
156
        } else {
157
            return null;
158
        }
159
    }
160
 
161
    private static int getFrenchMonth(String frenchDate){
162
        int month = -1;
163
        if (frenchDate.contains("anvier")) {
164
            month = 0;
165
        } else if (frenchDate.contains("vrier")) {
166
            month = 1;
167
        } else if (frenchDate.contains("mars")) {
168
            month = 2;
169
        } else if (frenchDate.contains("avr")) {
170
            month = 3;
171
        } else if (frenchDate.contains("mai")) {
172
            month = 4;
173
        } else if (frenchDate.contains("juin") || frenchDate.contains("ju1n") || (frenchDate.contains("ju") && frenchDate.contains("n"))) {
174
            month = 5;
175
        } else if (frenchDate.contains("llet") || (frenchDate.contains("ju") && frenchDate.contains("et"))) {
176
            month = 6;
177
        } else if (frenchDate.contains("ao")) {
178
            month = 7;
179
        } else if (frenchDate.contains("sept")) {
180
            month = 8;
181
        } else if (frenchDate.contains("oct") || frenchDate.contains("obre")) {
182
            month = 9;
183
        } else if (frenchDate.contains("novem")) {
184
            month = 10;
185
        } else if (frenchDate.contains("cembre") || frenchDate.contains("décerfibre")) {
186
            month = 11;
187
        }
188
        return month;
189
    }
190
 
191
    public int LevenshteinDistance(String s0, String s1) {
192
        int len0 = s0.length() + 1;
193
        int len1 = s1.length() + 1;
194
 
195
        // the array of distances
196
        int[] cost = new int[len0];
197
        int[] newcost = new int[len0];
198
 
199
        // initial cost of skipping prefix in String s0
200
        for (int i = 0; i < len0; i++) cost[i] = i;
201
 
202
        // dynamically computing the array of distances
203
 
204
        // transformation cost for each letter in s1
205
        for (int j = 1; j < len1; j++) {
206
            // initial cost of skipping prefix in String s1
207
            newcost[0] = j;
208
 
209
            // transformation cost for each letter in s0
210
            for(int i = 1; i < len0; i++) {
211
                // matching current letters in both strings
212
                int match = (s0.charAt(i - 1) == s1.charAt(j - 1)) ? 0 : 1;
213
 
214
                // computing cost for each transformation
215
                int cost_replace = cost[i - 1] + match;
216
                int cost_insert  = cost[i] + 1;
217
                int cost_delete  = newcost[i - 1] + 1;
218
 
219
                // keep minimum cost
220
                newcost[i] = Math.min(Math.min(cost_insert, cost_delete), cost_replace);
221
            }
222
 
223
            // swap cost/newcost arrays
224
            int[] swap = cost; cost = newcost; newcost = swap;
225
        }
226
 
227
        // the distance is the cost for transforming all letters in both strings
228
        return cost[len0 - 1];
229
    }
230
}