【问题标题】:Extract words out of a text file从文本文件中提取单词
【发布时间】:2008-11-09 22:05:10
【问题描述】:

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

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

我正在使用 Java。 谢谢

【问题讨论】:

    标签: java text


    【解决方案1】:

    这听起来像是正则表达式的正确工作。这里有一些 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()));
    }
    

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

    【讨论】:

    • 我不得不稍微更改正则表达式,使其不包含数字、下划线,并且不包含以引号开头的单词,否则,很好!
    • 我不得不像这样逃避 \w:Pattern.compile("[\\w']+");
    • 这有点跑题了,但是如何在单词的开头或结尾排除单引号?
    • @ScrollerBlaster 您可以为此使用单词边界。 Pattern.compile("\\b[\\w']+\\b");
    • Pattern.compile("\\w[\\w-]+('\\w+)?") 将支持连字符甚至多连字符(sous-vide、mise-en-scène)以及撇号,但不能在单词的开头,必须跟在后面通过更多的单词字母(I've,sous-vide'n)。您可能希望在撇号后允许更多字母(即包括复数所有格),在这种情况下使用Pattern.compile("\\w[\\w-]+('\\w*)?")。
    【解决方案2】:

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

    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;
    
    }
    

    【讨论】:

      【解决方案3】:

      伪代码如下所示:

      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
      

      python 代码是这样的:

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

      在哪里

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

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

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


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

      >>> 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.
      

      【讨论】:

      • 这段代码相对于正则表达式的优势在于它可以简单地一次完成。
      • 是的,Java 有一个 'split' 方法,但它没有等效于 'strip' 方法。
      【解决方案4】:

      基本上,你要匹配

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

      对吗?

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-30
          相关资源
          最近更新 更多