哈希图条目不会进入列

Hashmap Entries don't get into Columns

package einlesen;

/**
 * @author a
 *
 */

import java.io.*;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Einlesen {

    /**
     * @param args
     * @throws IOException
     */
    static List<String> word = new ArrayList<String>();
    static Map<String, Integer> wordsInTheMiddle = new HashMap<>();
    @SuppressWarnings({ "resource" })
    public static void main(String[] args) throws IOException {

        Scanner scan = new Scanner(System.in);
        String antwort;

        System.out.println("Welches Dokument wollen Sie? Geben Sie dabei den Path an, bitte.");
        antwort = ("Downloads/lol.txt"); // scan.nextLine();

        String path = System.getProperty("user.home");
        // System.out.println(path);
        File file = Paths.get(path, antwort).toFile();

        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        Scanner sc = new Scanner(br);
        //List<String> word = new ArrayList<String>();

        while (sc.hasNext()) {
            String wort = sc.next();
            // Remove quotes
            if (wort.startsWith("\"")) {
                wort = wort.substring(1);
            }
            if (wort.endsWith("\"")) {
                wort = wort.substring(0, wort.length() - 1);
            }
            word.add(wort);
        }

        br.close();

        int chunkStartIndex = 0;
        Map<String, Integer> wordsInTheMiddle = new HashMap<>();
        while (word.size() - chunkStartIndex > 0) {

            int chunkEndIndex = chunkStartIndex + 2000;
            if (chunkEndIndex > word.size()) {
                chunkEndIndex = word.size();
            }
            List<String> chunkOfWords = word.subList(chunkStartIndex, chunkEndIndex);

            for (int i = 0; i < chunkOfWords.size(); i++) {

                String word1 = chunkOfWords.get(i);

                if (word1.matches("[A-Z][a-z][a-z]\w+")) {

                    wordsInTheMiddle.putIfAbsent(word1, 0);
                    int oldCount = wordsInTheMiddle.get(word1);
                    wordsInTheMiddle.put(word1, oldCount + 1);

                }
            }

            // do not process the last word! Would cause an index out of bounds exception.
            for (int i = 0; i < chunkOfWords.size() - 1; i++) {

                String word1 = chunkOfWords.get(i);

                if (word1.matches("\w*(\.|\?|!)$")) {

                    // Word is at end of sentence
                    String nextWord = chunkOfWords.get(i + 1);

                    if (wordsInTheMiddle.getOrDefault(nextWord, 0) < 2) {

                        // sort out words that appear at the beginning of a sentence and appear less
                        // than 2 times in the text
                        wordsInTheMiddle.remove(nextWord);

                    }

                }

            }

            // remove blacklisted words
            String[] blacklist = { "This", "When", "Night", "Most", "Stone", "There", "Bonfire", "Tuesday", "Their",
                    "They", "Professor", "Famous", "About", "Madam", "Nearly", "Aunt", "What", "Uncle", "Mommy",
                    "Scars", "Scotch", "Every", "That" };
            for (String listedWord : blacklist) {

                wordsInTheMiddle.remove(listedWord);

            }

            System.out.println("Mitte: " + wordsInTheMiddle);

            chunkStartIndex = chunkEndIndex;

        }


        JTable t = new JTable(toTableModel(wordsInTheMiddle));

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(t.getModel());
        t.setRowSorter(sorter);

        List<RowSorter.SortKey> sortKeys = new ArrayList<>(25);
        sortKeys.add(new RowSorter.SortKey(1, SortOrder.DESCENDING));
        sorter.setSortKeys(sortKeys);

        JPanel p = new JPanel();
        p.add(t);
        JFrame f = new JFrame();
        f.add(p);
        f.setSize(700, 600);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setTitle(antwort);

    }


    public static TableModel toTableModel(Map<?, ?> map) {
        DefaultTableModel model = new DefaultTableModel(new Object[] { "Key", "Value" }, 0);
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            model.addRow(new Object[] { entry.getKey(), entry.getValue() });

            for (int a = word.size()/2000;model.getColumnCount() - 2 <= a;)
            {
                model.addColumn(new Object[] { wordsInTheMiddle});

            }

        }

        return model;

    }

}

这是我的程序代码,它将文本分成 2000 个单词的不同部分,并计算不同部分中名称的出现次数。这是在哈希图中完成的。所以我需要一个 table 用于该哈希图,我可以在其中看到名称、总计数和每个部分的计数。第一列是名称,第二列是总数,其余是不同的部分。

像这样:

name | total count | 1.Part | 2.Part...
---------------------------------------
name | namecounttotal| count1.Part| count2.Part...

我在前两列中输入了正确的条目,但在其余的列中我无法输入值。我希望你能帮助我。

你的程序有两个问题:

  1. 您似乎没有记下每 2,000 个单词块中有多少个名字。您可以通过添加例如解决此问题List<Map<String,Integer>> 或其他数据类型。在我的示例中,列表中的每个元素都会跟踪名称及其在特定块中的计数。
  2. 您的 tableToModel() 确实为带有 .addColumn() 的 2,000 个单词块中的每一个添加了新列。但是,当您使用 .addRow(Object[]) 添加一行时,对象数组仅包含两个元素。您需要以某种方式将特定于块的计数包装到该对象数组中。此外,首先使用 .addColumn() 创建列然后再一次添加新行可能是合理的。

以下是我要解决这些问题的方法:

  1. 将数据添加到 chunck 特定计数器并跟踪 chunck 的编号:

    List<Map<String,Integer>> wordsPerChunck = new ArrayList<>();
    
    while (word.size() - chunkStartIndex > 0) {
    
        // Initialize a chunck-specific word counter
        Map<String, Integer> countInChunck = new HashMap<>();
        wordsPerChunck.add(countInChunck);
        ...
        for (int i = 0; i < chunkOfWords.size(); i++) {
    
            String word1 = chunkOfWords.get(i);
    
            if (word1.matches("[A-Z][a-z][a-z]\w+")) {
    
                wordsInTheMiddle.putIfAbsent(word1, 0);
                wordsInTheMiddle.put(word1, oldCount + 1);
                countInChunck.putIfAbsent(word1, 0);
                // Increase the count in this chunck
                countInChunck.put(word1, countInChunck.get(word1) + 1);
    
            }
        }
    
  2. 稍微修改一下toTableModel:将该列表作为第二个参数。首先创建列,然后创建一个大小正确的 Object 数组,其中包含名称、总计数和块特定计数:

    public static TableModel toTableModel(Map<?, ?> map, List<?> list) {
        DefaultTableModel model = new DefaultTableModel(new Object[] { "Key", "Value" }, 0);
        for (Map.Entry<?, ?> entry : map.entrySet()) {
    
            for (int a = word.size()/2000; model.getColumnCount() - 2 <= a;) {
                model.addColumn(new Object[] { "partial" });
            }
    
            // Create the object that holds all the columns
            Object[] temp = new Object[2+list.size()];
            temp[0] = entry.getKey();
            temp[1] = entry.getValue();
    
            int index = 2;
            for (Object o : list) {
    
                Map<?, ?> m = (Map<?, ?>) o;
                // Get the chunck-specific count with the correct key (the name)
                temp[index] = m.get(temp[0]);
                index++;
            }
            model.addRow(temp);
        }
    

我没有在这里执行您所做的检查。但是如果你修复了这些,你应该能够让它工作。

创建 JTable 然后使用:

JTable t = new JTable(toTableModel(wordsInTheMiddle, wordsPerChunck));

感谢Am9417 我搞定了!如果有人感兴趣,这是结果:

    package einlesen;

/**
 * @author angeliqueschulberger
 *
 */

import java.io.*;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Einlesen {

    /**
     * @param args
     * @throws IOException
     */
    static List<String> word = new ArrayList<String>();
    static Map<String, Integer> wordsInTheMiddle = new HashMap<>();
    @SuppressWarnings({ "resource" })
    public static void main(String[] args) throws IOException {

        Scanner scan = new Scanner(System.in);
        String antwort;

        System.out.println("Welches Dokument wollen Sie? Geben Sie dabei den Path an, bitte.");
        antwort = ("Downloads/lol.txt"); // scan.nextLine();

        String path = System.getProperty("user.home");
        // System.out.println(path);
        File file = Paths.get(path, antwort).toFile();

        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        Scanner sc = new Scanner(br);
        //List<String> word = new ArrayList<String>();

        while (sc.hasNext()) {
            String wort = sc.next();
            // Remove quotes
            if (wort.startsWith("\"")) {
                wort = wort.substring(1);
            }
            if (wort.endsWith("\"")) {
                wort = wort.substring(0, wort.length() - 1);
            }
            word.add(wort);
        }

        br.close();

        int chunkStartIndex = 0;
        Map<String, Integer> wordsInTheMiddle = new HashMap<>();

        List<Map<String,Integer>> wordsPerChunck = new ArrayList<>();
        int chunckNumber = 0;

        while (word.size() - chunkStartIndex > 0) {

            int chunkEndIndex = chunkStartIndex + 2000;
            if (chunkEndIndex > word.size()) {
                chunkEndIndex = word.size();
            }
            List<String> chunkOfWords = word.subList(chunkStartIndex, chunkEndIndex);

            Map<String, Integer> countInChunck = new HashMap<>();
            wordsPerChunck.add(countInChunck);

            for (int i = 0; i < chunkOfWords.size(); i++) {

                String word1 = chunkOfWords.get(i);

                if (word1.matches("[A-Z][a-z][a-z]\w+")) {
                    wordsInTheMiddle.putIfAbsent(word1, 0);
                    int oldCount = wordsInTheMiddle.get(word1);
                    wordsInTheMiddle.put(word1, oldCount + 1);
                    countInChunck.putIfAbsent(word1, 0);
                    // Increase the count in this chunck
                    countInChunck.put(word1, countInChunck.get(word1) + 1);

                }
            }

            // do not process the last word! Would cause an index out of bounds exception.
            for (int i = 0; i < chunkOfWords.size() - 1; i++) {

                String word1 = chunkOfWords.get(i);

                if (word1.matches("\w*(\.|\?|!)$")) {

                    // Word is at end of sentence
                    String nextWord = chunkOfWords.get(i + 1);

                    if (wordsInTheMiddle.getOrDefault(nextWord, 0) < 2) {

                        // sort out words that appear at the beginning of a sentence and appear less
                        // than 2 times in the text
                        wordsInTheMiddle.remove(nextWord);

                    }

                }

            }

            // remove blacklisted words
            String[] blacklist = { "This", "When", "Night", "Most", "Stone", "There", "Bonfire", "Tuesday", "Their",
                    "They", "Professor", "Famous", "About", "Madam", "Nearly", "Aunt", "What", "Uncle", "Mommy",
                    "Scars", "Scotch", "Every", "That" };
            for (String listedWord : blacklist) {

                wordsInTheMiddle.remove(listedWord);

            }


            System.out.println("Mitte: " + wordsInTheMiddle);

            chunkStartIndex = chunkEndIndex;

        }


        JTable t = new JTable(toTableModel(wordsInTheMiddle, wordsPerChunck));

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(t.getModel());
        t.setRowSorter(sorter);

        List<RowSorter.SortKey> sortKeys = new ArrayList<>(25);
        sortKeys.add(new RowSorter.SortKey(1, SortOrder.DESCENDING));
        sorter.setSortKeys(sortKeys);

        JPanel p = new JPanel();
        p.add(t);
        JFrame f = new JFrame();
        f.add(p);
        f.setSize(700, 600);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setTitle(antwort);

    }


    public static TableModel toTableModel(Map<?, ?> map, List<Map<String, Integer>> list) {
        DefaultTableModel model = new DefaultTableModel(new Object[] { "Key", "Value" }, 0);
        for (Map.Entry<?, ?> entry : map.entrySet()) {

            for (int a = word.size()/2000;model.getColumnCount() - 2 <= a;)
            {
                model.addColumn(new Object[] { "partial" });

            }
            Object[] temp = new Object[2+list.size()];
            temp[0] = entry.getKey();
            temp[1] = entry.getValue();

            int index = 2;
            for (Object o : list) {

                Map<?, ?> m = (Map<?, ?>) o;
                // Get the chunck-specific count with the correct key (the name)
                temp[index] = m.get(temp[0]);
                index++;
            }
            model.addRow(temp);

        }

        return model;

    }
}