Diacriticizer implements Serializable { public Set<String> diacriticize(String originalWord) { originalWord = this.recompose(originalWord); originalWord = DiacriticRemover.diacriticPattern.matcher(originalWord).replaceAll(""); String undecorated = DiacriticRemover.removeDiacritics(originalWord); String key = undecorated.toLowerCase(); String lowercase = originalWord.toLowerCase(); Set<String> results = map.get(key); if (results == null) return emptySet; Set<String> validResults = null; if (locale != null) validResults = new TreeSet<String>(Collator.getInstance(locale)); else validResults = new TreeSet<String>(); for (String result : results) { boolean validResult = true; for (int i = 0; i < originalWord.length(); i++) { char cO = originalWord.charAt(i); char cR = result.charAt(i); char cU = undecorated.charAt(i); char cL = lowercase.charAt(i); if (Character.isUpperCase(cO)) { if (cO == cU) { continue; } if (cL == cR || cO == cR) { continue; } validResult = false; break; } else { if (cO == cR) { continue; } validResult = false; break; } } if (validResult) validResults.add(result); } String lowercasePreference = this.getLowercasePreferences().get(originalWord); if (lowercasePreference != null) { Set<String> orderedResults = new LinkedHashSet<String>(); orderedResults.add(lowercasePreference); orderedResults.addAll(validResults); validResults = orderedResults; } return validResults; } Diacriticizer();  Diacriticizer(Lexicon lexicon); static void main(String[] args); Set<String> diacriticize(String originalWord); void addLexicon(Lexicon lexicon); void serialize(); static Diacriticizer deserialize(File inFile); Map<String, String> getLowercasePreferences(); void setLowercasePreferences(Map<String, String> lowercasePreferences); Locale getLocale(); void setLocale(Locale locale);  }
@Test public void testDiacriticize() { final Lexicon lexicon = mock(Lexicon.class); final LexicalEntry l1 = mock(LexicalEntry.class); final LexicalEntry l2 = mock(LexicalEntry.class); final LexicalEntry l3 = mock(LexicalEntry.class); final LexicalEntry l4 = mock(LexicalEntry.class); final LexicalEntry l5 = mock(LexicalEntry.class); final LexicalEntry l6 = mock(LexicalEntry.class); final LexicalEntry l7 = mock(LexicalEntry.class); final LexicalEntry l8 = mock(LexicalEntry.class); final LexicalEntry l9 = mock(LexicalEntry.class); List<LexicalEntry> lexicalEntries = Arrays.asList(l1, l2, l3, l4, l5, l6, l7, l8, l9); when(l1.getWord()).thenReturn("mangé"); when(l2.getWord()).thenReturn("mange"); when(l3.getWord()).thenReturn("mangée"); when(l4.getWord()).thenReturn("SARL"); when(l5.getWord()).thenReturn("bala"); when(l6.getWord()).thenReturn("bàlà"); when(l7.getWord()).thenReturn("bàla"); when(l8.getWord()).thenReturn("a"); when(l9.getWord()).thenReturn("à"); when(lexicon.getAllEntries()).thenReturn(lexicalEntries.iterator()); Diacriticizer diacriticizer = new Diacriticizer(); diacriticizer.setLocale(Locale.FRENCH); Map<String, String> lowercasePreferences = new HashMap<>(); lowercasePreferences.put("A", "à"); diacriticizer.setLowercasePreferences(lowercasePreferences); diacriticizer.addLexicon(lexicon); Set<String> results = diacriticizer.diacriticize("MANGEE"); assertEquals(1, results.size()); assertEquals("mangée", results.iterator().next()); results = diacriticizer.diacriticize("mange"); assertEquals(1, results.size()); assertEquals("mange", results.iterator().next()); results = diacriticizer.diacriticize("Mangé"); assertEquals(1, results.size()); assertEquals("mangé", results.iterator().next()); results = diacriticizer.diacriticize("MANGE"); assertEquals(2, results.size()); List<String> resultList = new ArrayList<String>(results); assertEquals("mange", resultList.get(0)); assertEquals("mangé", resultList.get(1)); results = diacriticizer.diacriticize("MANGÉ"); assertEquals(1, results.size()); assertTrue(results.contains("mangé")); results = diacriticizer.diacriticize("SARL"); assertEquals(1, results.size()); assertEquals("SARL", results.iterator().next()); results = diacriticizer.diacriticize("sarl"); assertEquals(0, results.size()); results = diacriticizer.diacriticize("BALA"); assertEquals(3, results.size()); resultList = new ArrayList<String>(results); assertEquals("bala", resultList.get(0)); assertEquals("bàla", resultList.get(1)); assertEquals("bàlà", resultList.get(2)); results = diacriticizer.diacriticize("A"); assertEquals(2, results.size()); resultList = new ArrayList<String>(results); assertEquals("à", resultList.get(0)); assertEquals("a", resultList.get(1)); results = diacriticizer.diacriticize("mange\u0301"); assertEquals(1, results.size()); assertTrue(results.contains("mangé")); }