|
21.05.2007, 15:05:13
#34539826
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
|
не много чего понятно в синтаксисе.. необходимо превести на перл.
задача в том, что есть словарик, и необходимо преверять слова из массива на то, что содержат они слова из словарика или нет..
вот ява-код:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241.
package com.dictionary;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Aider
* Date: 16.05.2007
* Time: 17:54:44
*/
public class Dictionary {
private Hashtable<String, List<String>> dictList = new Hashtable<String, List<String>>();
private static final int MIN_LENGTH = 2 ;
private static final char HYPHEN = '-';
public static final int EQ_DICT = 1 ;
public static final int EQ_DICTS = 2 ;
public static final int CONTAINS_DICT = 3 ;
public static final int NOT_IN_DICT = 0 ;
public int process( final String str) {
if (containsTwoHyphen(str)) {
if (containsDict(str)) {
return CONTAINS_DICT;
}
} else {
if (equalsDict(str)) {
return EQ_DICT;
} else {
Hashtable<String, List<String>> containsDictHash = containsDicts(str);
if (containsDictHash.size() > 0 ) {
if (equalsDicts(str, containsDictHash)) {
return EQ_DICTS;
}
return CONTAINS_DICT;
}
}
}
return NOT_IN_DICT;
}
public boolean equalsDicts(String domainName, Hashtable<String, List<String>> dictsHash) {
int domainLength = domainName.length();
if (domainLength < 3 ) return false;
int beginIndex = 0 ;
int endIndex = MIN_LENGTH;
if (domainName.charAt( 0 ) == HYPHEN) {
beginIndex++;
endIndex++;
}
final String beginStr = domainName.substring(beginIndex, endIndex);
final String endStr = domainName.substring(endIndex, domainLength);
final int endStrLength = endStr.length();
if (endStrLength < 0 ) return false;
if (dictsHash.containsKey(beginStr)) {
List<String> dictList = dictsHash.get(beginStr);
for (String endDict : dictList) {
final int endDictLength = endDict.length();
if (endDictLength == endStrLength && endStr.equals(endDict)) {
return true;
} else
if (endStrLength > endDictLength && endStr.startsWith(endDict) && equalsDicts(endStr.substring(endDictLength, endStrLength), dictsHash)) {
return true;
}
}
}
return false;
}
public boolean equalsDict( final String domainName) {
int domainLength = domainName.length();
if (domainLength < 3 ) return false;
final String beginStr = domainName.substring( 0 , MIN_LENGTH);
if (!Dictionary.isLetters(beginStr)) return false;
final String endStr = domainName.substring(MIN_LENGTH, domainLength);
return Dictionary.isLetters(endStr) && dictList.containsKey(beginStr) && dictList.get(beginStr).contains(endStr);
}
public boolean containsDict( final String domainName) {
final int domainLength = domainName.length();
for ( int i = 0 ; i < domainLength; i++) {
if ((i + MIN_LENGTH) >= domainLength) {
continue ;
}
final String endDomainName = domainName.substring(i + MIN_LENGTH, domainLength);
if (!Dictionary.isLetter(endDomainName.charAt( 0 ))) continue ;
final String subDomainName = domainName.substring(i, i + MIN_LENGTH);
if (!Dictionary.isLetters(subDomainName)) continue ;
if (!dictList.containsKey(subDomainName)) continue ;
List<String> list = dictList.get(subDomainName);
for (String endDic : list) {
if (endDomainName.startsWith(endDic)) {
return true;
}
}
}
return false;
}
public Hashtable<String, List<String>> containsDicts( final String domainName) {
Hashtable<String, List<String>> hashtable = new Hashtable<String, List<String>>();
final int domainLength = domainName.length();
for ( int i = 0 ; i < domainLength; i++) {
if ((i + MIN_LENGTH) >= domainLength) {
continue ;
}
final String endDomainName = domainName.substring(i + MIN_LENGTH, domainLength);
if (!Dictionary.isLetter(endDomainName.charAt( 0 ))) continue ;
final String subDomainName = domainName.substring(i, i + MIN_LENGTH);
if (!Dictionary.isLetters(subDomainName)) continue ;
if (!dictList.containsKey(subDomainName)) continue ;
List<String> list = dictList.get(subDomainName);
for (String endDic : list) {
if (endDomainName.startsWith(endDic)) {
if (!hashtable.containsKey(subDomainName)) {
List<String> dictList = new ArrayList<String>();
dictList.add(endDic);
hashtable.put(subDomainName, dictList);
} else {
hashtable.get(subDomainName).add(endDic);
}
}
}
}
return hashtable;
}
public static boolean isLetter( final char c) {
return !(HYPHEN == c || Character.isDigit(c));
}
public static boolean isLetters( final String s) {
for ( int i = 0 ; i < s.length(); i++) {
if (!isLetter(s.charAt(i))) {
return false;
}
}
return true;
}
public boolean containsTwoHyphen( final String s) {
int hyphenCounter = 0 ;
char c;
for ( int i = 0 ; i < s.length(); i++) {
c = s.charAt(i);
if (Character.isDigit(c)) {
return true;
} else if (HYPHEN == c) {
hyphenCounter++;
if (hyphenCounter > 1 ) {
return true;
}
} else if (hyphenCounter > 0 ) {
hyphenCounter = 0 ;
}
}
return false;
}
public void load(File file) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new FileReader(file));
try {
String dict;
while ((dict = bufferedReader.readLine()) != null ) {
dict = dict.trim();
int dictLength = dict.length();
if (dictLength > MIN_LENGTH) {
String beginDict = dict.substring( 0 , MIN_LENGTH);
if (!dictList.containsKey(beginDict)) {
List<String> list = new ArrayList<String>();
list.add(dict.substring(MIN_LENGTH, dictLength));
dictList.put(dict.substring( 0 , MIN_LENGTH), list);
} else {
dictList.get(beginDict).add(dict.substring(MIN_LENGTH, dictLength));
}
}
}
} finally {
bufferedReader.close();
}
}
public void load(List<String> dicList) {
try {
for (String dict : dicList) {
int dictLength = dict.length();
if (dictLength > MIN_LENGTH) {
String beginDict = dict.substring( 0 , MIN_LENGTH);
if (!dictList.containsKey(beginDict)) {
List<String> list = new ArrayList<String>();
list.add(dict.substring(MIN_LENGTH, dictLength));
dictList.put(beginDict, list);
} else {
dictList.get(beginDict).add(dict.substring(MIN_LENGTH, dictLength));
}
}
}
} finally {
dicList.clear();
}
}
}
|
|
|