001/* 002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * Michaƫl Vachette 019 * 020 */ 021package org.nuxeo.ecm.platform.importer.random; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025 026import java.io.BufferedReader; 027import java.io.IOException; 028import java.io.InputStream; 029import java.io.InputStreamReader; 030import java.net.URL; 031import java.util.ArrayList; 032import java.util.List; 033import java.util.Random; 034 035/** 036 * @author Thierry Delprat 037 */ 038public class HunspellDictionaryHolder implements DictionaryHolder { 039 040 protected static final int INITIAL_SIZE = 100000; 041 042 protected List<String> words = new ArrayList<>(INITIAL_SIZE); 043 044 protected Random generator; 045 046 protected int wordCount; 047 048 protected String dicName; 049 050 public static final Log log = LogFactory.getLog(HunspellDictionaryHolder.class); 051 052 public HunspellDictionaryHolder(String dicName) { 053 generator = new Random(System.currentTimeMillis()); 054 this.dicName = dicName; 055 } 056 057 @Override 058 public void init() throws IOException { 059 loadDic(); 060 wordCount = words.size(); 061 } 062 063 /** 064 * @deprecated since 6.0 065 */ 066 @Deprecated 067 protected void loadDic(String dicName) throws IOException { 068 this.dicName = dicName; 069 loadDic(); 070 } 071 072 /** 073 * @since 6.0 074 */ 075 protected void loadDic() throws IOException { 076 URL url = Thread.currentThread().getContextClassLoader().getResource(dicName); 077 if (url == null) { 078 log.error("not found: " + dicName); 079 return; 080 } 081 try (InputStream in = url.openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { 082 String line; 083 while ((line = reader.readLine()) != null) { 084 int idx = line.indexOf("/"); 085 if (idx > 0) { 086 String word = line.substring(0, idx); 087 words.add(word + " "); 088 } else { 089 words.add(line + " "); 090 } 091 } 092 } 093 } 094 095 @Override 096 public int getWordCount() { 097 return wordCount; 098 } 099 100 @Override 101 public String getRandomWord() { 102 int idx = generator.nextInt(wordCount); 103 return words.get(idx); 104 } 105}