ref #10178: new methods for normalization of names
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / NameServiceImplementBelen.java
1 package eu.etaxonomy.cdm.api.service;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 public class NameServiceImplementBelen {
7 private String tempInputName;
8 private String tempDatabaseName;
9 private String shortenedInputName;
10 private String shortenedDatabaseName;
11
12 // Phonetic changes performed ONLY on the initial characters of each String
13
14 public String replaceInitialCharacter(String inp) {
15 String input=inp.toLowerCase();
16 String output="";
17 String[][] phoneticChange = {
18 {"ae","e"},{"cn","n"},{"ct","t"},{"cz","c"},
19 {"dj","d"},{"ea","e"},{"eu","u"},{"gn","n"},
20 {"kn","n"},{"mc","mac"},{"mn","n"},{"oe","e"},
21 {"qu","q"},{"ph","f"},{"ps","s"},{"pt","t"},
22 {"ts","s"},{"wr","r"},{"x","z"}
23 };
24 for (int i = 0 ; i< phoneticChange.length; i++) {
25 if (input.startsWith(phoneticChange[i][0])){
26 output= input.replaceFirst(phoneticChange[i][0], phoneticChange[i][1]);
27 break;
28 }
29 }
30 return output;
31 }
32
33
34 // trim common characters between query and document
35
36 public List <String> trimCommonChar(String inputName, String databaseName) {
37
38 // trim common leading characters of query and document
39
40 int inputNameLength = inputName.length();
41 int databaseNameLength = databaseName.length();
42 int largestString = Math.max(inputNameLength, databaseNameLength);
43 int i;
44
45 for (i = 0; i < largestString; i++) {
46 if (i >= inputNameLength || i >= databaseNameLength || inputName.charAt(i) != databaseName.charAt(i)) {
47 // Stop iterating when the characters at the current position are not equal.
48 break;
49 }
50 }
51
52 // Create temp names with common leading characters removed.
53 tempInputName = inputName.substring(i);
54 tempDatabaseName = databaseName.substring(i);
55
56 List <String> list= new ArrayList<>();
57
58 // trim common tailing characters between query and document
59
60 int restantInputNameLenght = tempInputName.length();
61 int restantDatabaseNameLenght = tempDatabaseName.length();
62 int shortestString = Math.min(restantInputNameLenght, restantDatabaseNameLenght);
63
64 for (int x = 0; x < shortestString; x++) {
65 if (tempInputName.charAt(restantInputNameLenght - x - 1) != tempDatabaseName
66 .charAt(restantDatabaseNameLenght - x - 1)) {
67 break;
68 }
69 shortenedInputName = tempInputName.substring(0, restantInputNameLenght - x - 1);
70 shortenedDatabaseName = tempDatabaseName.substring(0, restantDatabaseNameLenght - x - 1);
71
72 }
73 list.add(shortenedInputName +" "+ shortenedDatabaseName);
74 return list;
75 }
76 }