最有效的方法来查找一个字符串对一个单词数组的匹配计数?

时间:2010-07-09 00:11:29

标签: java

假设我有一个字符串

String test = "This is a test string and I have some stopwords in here";

我希望看到下面数组中的单词与我的字符串匹配的次数

psudocode

array = a,and,the,them,they,I

所以答案是“3”

只是好奇在java中最有效的方法是什么?

5 个答案:

答案 0 :(得分:5)

我可能会将输入中的单词存储到HashSet中,然后遍历数组,看看数组中的每个单词是否都是.contains。

这里是代码......输入是“Around the world in 80 days”。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main
{
    public static void main(final String[] argv)
        throws FileNotFoundException
    {
        final File     file;
        final String[] wordsToFind;

        file        = new File(argv[0]);
        wordsToFind = getWordsToFind(file);
        a(file, wordsToFind);
        b(file, wordsToFind);
        c(file, wordsToFind);
        d(file, wordsToFind);
    }

    // this just reads the file into the disk cache
    private static String[] getWordsToFind(final File file)
        throws FileNotFoundException
    {
        final Scanner     scanner;
        final Set<String> words;

        scanner = new Scanner(file);
        words   = new HashSet<String>();

        while(scanner.hasNext())
        {
            final String word;

            word = scanner.next();
            words.add(word);
        }

        return (words.toArray(new String[words.size()]));
    }

    // bad way, read intpo a list and then iterate over the list until you find a match
    private static void a(final File     file,
                          final String[] wordsToFind)
        throws FileNotFoundException
    {
        final long start;
        final long end;
        final long total;
        final Scanner      scanner;
        final List<String> words;
        int                matches;

        scanner = new Scanner(file);
        words   = new ArrayList<String>();

        while(scanner.hasNext())
        {
            final String word;

            word = scanner.next();
            words.add(word);
        }

        start = System.nanoTime();

        {
            matches = 0;

            for(final String wordToFind : wordsToFind)
            {
                for(final String word : words)
                {
                    if(word.equals(wordToFind))
                    {
                        matches++;
                        break;
                    }
                }
            }

            System.out.println(matches);
        }

        end   = System.nanoTime();
        total = end - start;
        System.out.println("a: " + total);
    }

    // slightly better way, read intpo a list and then iterate over the set (which reduces the number of things you progbably
    // have to read until you find a match), until you find a match
    private static void b(final File     file,
                          final String[] wordsToFind)
        throws FileNotFoundException
    {
        final long start;
        final long end;
        final long total;
        final Scanner     scanner;
        final Set<String> words;
        int               matches;

        scanner = new Scanner(file);
        words   = new HashSet<String>();

        while(scanner.hasNext())
        {
            final String word;

            word = scanner.next();
            words.add(word);
        }

        start = System.nanoTime();

        {
            matches = 0;

            for(final String wordToFind : wordsToFind)
            {
                for(final String word : words)
                {
                    if(word.equals(wordToFind))
                    {
                        matches++;
                        break;
                    }
                }
            }

            System.out.println(matches);
        }

        end   = System.nanoTime();
        total = end - start;
        System.out.println("b: " + total);
    }

    // my way
    private static void c(final File     file,
                          final String[] wordsToFind)
        throws FileNotFoundException
    {
        final long start;
        final long end;
        final long total;
        final Scanner     scanner;
        final Set<String> words;
        int               matches;

        scanner = new Scanner(file);
        words   = new HashSet<String>();

        while(scanner.hasNext())
        {
            final String word;

            word = scanner.next();
            words.add(word);
        }

        start = System.nanoTime();

        {
            matches = 0;

            for(final String wordToFind : wordsToFind)
            {
                if(words.contains(wordToFind))
                {
                    matches++;
                }
            }

            System.out.println(matches);
        }

        end   = System.nanoTime();
        total = end - start;
        System.out.println("c: " + total);
    }

    // Nikita Rybak way
    private static void d(final File     file,
                          final String[] wordsToFind)
        throws FileNotFoundException
    {
        final long start;
        final long end;
        final long total;
        final Scanner     scanner;
        final Set<String> words;
        int               matches;

        scanner = new Scanner(file);
        words   = new HashSet<String>();

        while(scanner.hasNext())
        {
            final String word;

            word = scanner.next();
            words.add(word);
        }

        start = System.nanoTime();

        {
            words.retainAll(new HashSet<String>(Arrays.asList(wordsToFind)));
            matches = words.size();
            System.out.println(matches);
        }

        end   = System.nanoTime();
        total = end - start;
        System.out.println("d: " + total);
    }
}

结果(经过几次运行后,每次运行几乎都是相同的):

12596
a: 2440699000
12596
b: 2531635000
12596
c: 4507000
12596
d: 5597000

如果你通过在getWordsToFind的每个单词中添加“XXX”来修改它(所以没有找到单词),你得到:

0
a: 7415291000
0
b: 4688973000
0
c: 2849000
0
d: 7981000

而且,为了完整起见,我试着搜索单词“I”,结果如下:

1
a: 235000
1
b: 351000
1
c: 75000
1
d: 10725000

答案 1 :(得分:5)

这样的东西?不确定“最有效”,但很简单。

Set<String> s1 = new HashSet<String>(Arrays.asList("This is a test string and I have some stopwords in here".split("\\s")));
Set<String> s2 = new HashSet<String>(Arrays.asList("a", "and", "the", "them", "they", "I"));
s1.retainAll(s2);
System.out.println(s1.size());

两组词的交集。

答案 2 :(得分:3)

最有效的方法是对'test'和'array'进行排序,然后迭代两者:n.log(n)+ n

测试 - &gt; ['a','和','有','这里',in,is,...,'This'] 数组 - &gt; ['a','和',''','他们','他们','我']

阵列测试匹配 'a''a'1 'a''和'1 '和''和'2 '和''拥有'2 ''''这里'2 '''''2' '''是'2' ...

答案 3 :(得分:0)

Nikita答案的一个小变化(尼基塔增加1)。如果对s1使用List,则会得到出现次数(如果单词在句子中多次出现)。

List<String> s1 = new ArrayList<String>(Arrays.asList("This is a test string and I have some stopwords in here".split("\\s")));
Set<String> s2 = new HashSet<String>(Arrays.asList("a", "and", "the", "them", "they", "I"));
s1.retainAll(s2);
System.out.println(s1.size());

答案 4 :(得分:0)

将您的字符串存储在哈希表(HashMap of(String and Integer))中,然后对文本进行迭代,并增加哈希表中匹配单词的整数值。然后在哈希表上迭代,并求和所有整数值。

相关问题