在网页上找到最常用的单词(使用Jsoup)?

时间:2015-04-04 14:27:28

标签: java html jsoup webpage word-frequency

在我的项目中,我必须计算维基百科文章中最常用的词。我发现Jsoup用于解析HTML格式,但仍然存在字频问题。 Jsoup中是否有一个函数可以计算单词的频率,或者使用Jsoup来查找哪些单词在网页上最常用?

感谢。

1 个答案:

答案 0 :(得分:2)

是的,您可以使用Jsoup从网页上获取文本,如下所示:

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
String text = doc.body().text();

然后,您需要计算单词并找出哪些是最常见的单词。 This code看起来很有希望。我们需要修改它以使用Jsoup的String输出,如下所示:

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JsoupWordCount {

   public static void main(String[] args) throws IOException {
        long time = System.currentTimeMillis();

        Map<String, Word> countMap = new HashMap<String, Word>();

        //connect to wikipedia and get the HTML
        System.out.println("Downloading page...");
        Document doc = Jsoup.connect("http://en.wikipedia.org/").get();

        //Get the actual text from the page, excluding the HTML
        String text = doc.body().text();

        System.out.println("Analyzing text...");
        //Create BufferedReader so the words can be counted
        BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))));
        String line;
        while ((line = reader.readLine()) != null) {
            String[] words = line.split("[^A-ZÅÄÖa-zåäö]+");
            for (String word : words) {
                if ("".equals(word)) {
                    continue;
                }

                Word wordObj = countMap.get(word);
                if (wordObj == null) {
                    wordObj = new Word();
                    wordObj.word = word;
                    wordObj.count = 0;
                    countMap.put(word, wordObj);
                }

                wordObj.count++;
            }
        }

        reader.close();

        SortedSet<Word> sortedWords = new TreeSet<Word>(countMap.values());
        int i = 0;
        int maxWordsToDisplay = 10;

        String[] wordsToIgnore = {"the", "and", "a"};

        for (Word word : sortedWords) {
            if (i >= maxWordsToDisplay) { //10 is the number of words you want to show frequency for
                break;
            }

            if (Arrays.asList(wordsToIgnore).contains(word.word)) {
                i++;
                maxWordsToDisplay++;
            } else {
                System.out.println(word.count + "\t" + word.word);
                i++;
            }

        }

        time = System.currentTimeMillis() - time;

        System.out.println("Finished in " + time + " ms");
    }

    public static class Word implements Comparable<Word> {
        String word;
        int count;

        @Override
        public int hashCode() { return word.hashCode(); }

        @Override
        public boolean equals(Object obj) { return word.equals(((Word)obj).word); }

        @Override
        public int compareTo(Word b) { return b.count - count; }
    }
}

输出:

Downloading page...
Analyzing text...
42  of
24  in
20  Wikipedia
19  to
16  is
11  that
10  The
9   was
8   articles
7   featured
Finished in 3300 ms

一些注意事项:

  • 此代码可以忽略某些字词,例如&#34;&#34;,&#34;和&#34;,&#34; a&#34;你必须自定义它。

  • 有时候似乎有unicode字符的问题。虽然我没有经历过这种情况,但评论中有人这样做了。

  • 这可以做得更好,代码更少。

  • 测试不佳。

享受!