SynonymReplaceAction implements ITalendStrConversionAction { @Override public String run(String str, int modifCount, String extraParameter, final Random random) { if (str.length() > 0) { SynonymIndexSearcher searcher = getSynonymSearcherMap().get(extraParameter); if (searcher == null) { searcher = new SynonymIndexSearcher(extraParameter); synonymSearcherMap.put(extraParameter, searcher); } try { TopDocs docs = searcher.searchDocumentBySynonym(str); if (docs.totalHits.value > 0) { Document doc = searcher.getDocument(docs.scoreDocs[0].doc); List<String> replaceList = new ArrayList<String>(); String word = doc.getValues(SynonymIndexSearcher.F_WORD)[0]; if (!word.equalsIgnoreCase(str)) { replaceList.add(word); } String[] synonyms = doc.getValues(SynonymIndexSearcher.F_SYN); for (String syn : synonyms) { if (!syn.equalsIgnoreCase(str)) { replaceList.add(syn); } } if (replaceList.isEmpty()) { return str; } else { return replaceList.get(random.nextInt(replaceList.size())); } } } catch (IOException e) { LOG.error(e.getMessage(), e); } } return StringUtils.EMPTY; }  @Override String run(String str, int modifCount, String extraParameter, final Random random); void closeSynonymIndice();  }
@Test public void runWithoutResult() { Assert.assertEquals(StringUtils.EMPTY, action.run("string", 1, "the_index_path", new Random(123L))); }
@Test public void runWithResult() { Assert.assertEquals("Allemagne", action.run("Germany", 1, "the_index_path", new Random(1L))); Assert.assertEquals("Deutschland", action.run("Germany", 1, "the_index_path", new Random(2L))); Assert.assertEquals("德国", action.run("Germany", 1, "the_index_path", new Random(3L))); Assert.assertEquals("德国", action.run("Germany", 1, "the_index_path", new Random(4L))); }