Java 从文本文件中提取单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/276546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:23:58  来源:igfitidea点击:

Extract words out of a text file

javatext

提问by Nathan H

Let's say you have a text file like this one: http://www.gutenberg.org/files/17921/17921-8.txt

假设您有一个这样的文本文件:http: //www.gutenberg.org/files/17921/17921-8.txt

Does anyone has a good algorithm, or open-source code, to extract words from a text file? How to get all the words, while avoiding special characters, and keeping things like "it's", etc...

有没有人有好的算法或开源代码来从文本文件中提取单词?如何获得所有单词,同时避免特殊字符,并保留诸如“it's”之类的东西......

I'm working in Java. Thanks

我在 Java 工作。谢谢

采纳答案by Tomalak

This sounds like the right job for regular expressions. Here is some Java code to give you an idea, in case you don't know how to start:

这听起来像是正则表达式的正确工作。下面是一些 Java 代码,可以让您了解一下,以防您不知道如何开始:

String input = "Input text, with words, punctuation, etc. Well, it's rather short.";
Pattern p = Pattern.compile("[\w']+");
Matcher m = p.matcher(input);

while ( m.find() ) {
    System.out.println(input.substring(m.start(), m.end()));
}

The pattern [\w']+matches all word characters, and the apostrophe, multiple times. The example string would be printed word-by-word. Have a look at the Java Pattern class documentationto read more.

该模式[\w']+多次匹配所有单词字符和撇号。示例字符串将逐字打印。查看Java 模式类文档以了解更多信息。

回答by GurdeepS

You could try regex, using a pattern you've made, and run a count the number of times that pattern has been found.

您可以使用您创建的模式尝试正则表达式,并计算找到该模式的次数。

回答by Claudiu

Pseudocode would look like this:

伪代码如下所示:

create words, a list of words, by splitting the input by whitespace
for every word, strip out whitespace and punctuation on the left and the right

The python code would be something like this:

python 代码将是这样的:

words = input.split()
words = [word.strip(PUNCTUATION) for word in words]

where

在哪里

PUNCTUATION = ",. \n\t\\"'][#*:"

or any other characters you want to remove.

或您要删除的任何其他字符。

I believe Java has equivalent functions in the String class: String.split() .

我相信 Java 在 String 类中有等效的功能:String.split() 。



Output of running this code on the text you provided in your link:

在链接中提供的文本上运行此代码的输出:

>>> print words[:100]
['Project', "Gutenberg's", 'Manual', 'of', 'Surgery', 'by', 'Alexis', 
'Thomson', 'and', 'Alexander', 'Miles', 'This', 'eBook', 'is', 'for', 
'the', 'use', 'of', 'anyone', 'anywhere', 'at', 'no', 'cost', 'and', 
'with', 'almost', 'no', 'restrictions', 'whatsoever', 'You', 'may', 
'copy', 'it', 'give', 'it', 'away', 'or', 're-use', 'it', 'under', 
... etc etc.

回答by Ed Marty

Basically, you want to match

基本上,你想匹配

([A-Za-z])+('([A-Za-z])*)?

([A-Za-z])+('([A-Za-z])*)?

right?

对?

回答by Rafael Frost

Here's a good approach to your problem: This function receives your text as an input and returns an array of all the words inside the given text

这是解决您的问题的好方法:此函数接收您的文本作为输入并返回给定文本中所有单词的数组

private ArrayList<String> get_Words(String SInput){

    StringBuilder stringBuffer = new StringBuilder(SInput);
    ArrayList<String> all_Words_List = new ArrayList<String>();

    String SWord = "";
    for(int i=0; i<stringBuffer.length(); i++){
        Character charAt = stringBuffer.charAt(i);
        if(Character.isAlphabetic(charAt) || Character.isDigit(charAt)){
            SWord = SWord + charAt;
        }
        else{
            if(!SWord.isEmpty()) all_Words_List.add(new String(SWord));
            SWord = "";
        }

    }

    return all_Words_List;

}