Xar*_*mer 4 java string filter
我有一个字符串数组,我在我的应用程序中加载,它包含不同的单词.我有一个简单的if语句,看它是否包含字母或数字但不包含单词.
我的意思是我只希望那些话是像AB2CD5X ..我想删除像所有换句话说Hello 3,3 word,any other也就是说这是一个英语单词.除了那些包含真实语法单词的单词之外,是否可以只过滤alphaNumeric单词.
我知道如何检查字符串是否包含字母数字
Pattern p = Pattern.compile("[\\p{Alnum},.']*");
Run Code Online (Sandbox Code Playgroud)
也知道
if(string.contains("[a-zA-Z]+") || string.contains([0-9]+])
Run Code Online (Sandbox Code Playgroud)
小智 5
你需要的是英语单词词典.然后你基本上扫描输入并检查字典中是否存在每个标记.您可以在线查找字典条目的文本文件,例如Jazzy拼写检查器.您也可以检查词典文本文件.
下面是一个示例代码,假设您的字典是UTF-8编码的简单文本文件,每行只有一个(小写)字:
public static void main(String[] args) throws IOException {
final Set<String> dictionary = loadDictionary();
final String text = loadInput();
final List<String> output = new ArrayList<>();
// by default splits on whitespace
final Scanner scanner = new Scanner(text);
while(scanner.hasNext()) {
final String token = scanner.next().toLowerCase();
if (!dictionary.contains(token)) output.add(token);
}
System.out.println(output);
}
private static String loadInput() {
return "This is a 5gse5qs sample f5qzd fbswx test";
}
private static Set<String> loadDictionary() throws IOException {
final File dicFile = new File("path_to_your_flat_dic_file");
final Set<String> dictionaryWords = new HashSet<>();
String line;
final LineNumberReader reader = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(dicFile), "UTF-8")));
try {
while ((line = reader.readLine()) != null) dictionaryWords.add(line);
return dictionaryWords;
}
finally {
reader.close();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您需要更准确的结果,则需要提取单词的词干.请参阅Apache的Lucene和EnglishStemmer
| 归档时间: |
|
| 查看次数: |
7497 次 |
| 最近记录: |