【问题标题】:What are the best ways to search for a given word (or words) in text在文本中搜索给定单词(或单词)的最佳方法是什么
【发布时间】:2015-07-17 11:50:54
【问题描述】:

我正在处理文本处理,我必须找到提到任何给定单词的推文数量。例如:

tweet 1: I had an egg for breakfast this morning
tweet 2: This is the book that I'll give to you tomorrow morning
tweet 3: I went there yesterday morning but you were not home. Did you go to her house this morning?
given word: this morning

对于上面的示例,频率应该为 2,因为只有两条推文(推文 1 和 3)以与给定单词完全相同的方式提及给定单词。 我担心如果我当前的实现效率低下(在某些方面),也许有更好的方法来做到这一点。到目前为止,我所做的是首先,我尝试获取所有包含给定单词的推文。

public int getDF(String term) throws FileNotFoundException, IOException{
        int frequency = 0;
        File[] paths = f.listFiles();
        for(File f:paths){
            BufferedReader br = new BufferedReader(new FileReader(f));
            String line;
            String[] termTokens = term.split(" ");
            while((line=br.readLine())!=null){
                if(line.toLowerCase().contains(term)){
                    if(termTokens.length > 1){ //just for multi-word
                        if(getDFUtil(line.toLowerCase(), term.toLowerCase()))
                            frequency++;
                    }else
                        frequency++;
                }
            }
        }
        return frequency;
    }

对于给定的多词,我调用函数 getDFUtil 来检查推文是否真的包含给定顺序的词。

public boolean getDFUtil(String tweet, String term){
        String[] tweetTokens = tweet.split(" ");
        String[] termTokens = term.split(" ");
        int chosenIndex = 0;
        int nextIndex = 0;
        if(termTokens.length > 1){
            for(int j=0;j<termTokens.length;j++){
                for(int i=0;i<tweetTokens.length;i++){
                    if(termTokens[j].equals(tweetTokens[i]) && j==0){
                        chosenIndex = i;
                        nextIndex = i;
                    }else if(termTokens[j].equals(tweetTokens[i])){
                        nextIndex = i;
                    }
                }
            }
            if(nextIndex - chosenIndex == termTokens.length - 1)
                return true;
        }else if(tweet.contains(term))
            return true;

        return false;
    }

然而,就像我之前提到的,我想知道(而且应该是)是否有更好或更简单但功能强大的方法来做到这一点。

【问题讨论】:

    标签: java string search twitter match


    【解决方案1】:

    我认为你可以使用正则表达式(regex)来完成这个任务(如果你不知道它是什么,那么学习如何使用它真的很值得)。如果与搜索的单词匹配,则无需检查行中的每个单词,您可以使用正则表达式一次将行与给定的单词或短语匹配。试试这个小应用:

    public class Test{
        public static void main(String[] args){
            int frequency = 0;
            String term = "this morning";
            File tweets = new File(//path to file Tweets.txt);
            String regex = "(?i).*"+term+".*";
            try{
                BufferedReader br = new BufferedReader(new FileReader(tweets));
                String line;
    
                while((line=br.readLine())!=null){
                    if(line.matches(regex)){
                        frequency++;
                    }
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
            System.out.println(frequency);
        }
    }
    

    Tweets.txt 包含来自您上面示例的推文。简而言之 - 给定正则表达式的应用程序计数次数与文件中的行匹配。我认为您可以轻松地在您的应用程序中实现类似的东西。 String.match() 方法返回 true,只有当整个字符串与给定的正则表达式匹配时,所以在这种情况下它是这样构造的:

    • (?i) - 不区分大小写的模式,我看到你习惯了 toLowerCase() 方法,用它大小写不匹配,
    • .* - 匹配此行中的任何内容
    • term - 您正在寻找的确切单词或短语
    • .* - 匹配此行中的任何内容

    您可以查看HERE 这个特定的正则表达式如何与您的推文一起使用。

    【讨论】:

    • 酷,它很有效,而且显然比我的更简单更好。 +1 用于正则表达式的链接。是的,我将开始更多地了解正则表达式,因为它在文本处理中非常重要。再次感谢您
    猜你喜欢
    • 2011-08-16
    • 2023-03-26
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    相关资源
    最近更新 更多