繁体   English   中英

尝试创建一个哈希表以从文本文件中获取 arraylist - Java

[英]Trying to create a hashtable to get an arraylist from the text file - Java

我正在尝试创建一个哈希表以从我的文本文件中获取ArrayList读取它,然后将其计入另一个文本文件。 我应该标记每个单词并通过计数来获取键和值。 到目前为止,我还处于起步阶段,我不知道我的代码有什么问题,似乎没有错误,但它没有连接到文本并获得ArrayList或者只是我的代码错误。 我将不胜感激任何帮助。 谢谢。

这是 Map 文件


public class Map {
    public static String fileName= "C:Users\\ruken\\OneDrive\\Desktop\\workshop.txt";

    private ArrayList<String> arr = new ArrayList<String>();
    public ArrayList <String>getList () {
        return this.arr;
    }

    private Hashtable<String, Integer> map = new Hashtable<String, Integer>();

    public void load(String path) {
        try{
            FileReader f2 = new FileReader("C:Users\\ruken\\OneDrive\\Desktop\\workshop.txt");
            Scanner s = new Scanner(f2);
            while (s.hasNextLine()) {
                String line = s.nextLine();
                String[] words = line.split("\\s");
                for (int i=0;i<words.length; i++){
                    String word = words[i];
                    if (! word.isEmpty()){
                        System.out.println(word);
                        arr.add(word);
                    }
                }
            }
            f2.close();
            System.out.println("An error occurred");
        }
        catch(IOException ex1)
        {
            Collections.sort(arr);
            System.out.println("An error occurred.");
            for (String counter: arr) {
                System.out.println(counter);
            }
            ex1.printStackTrace();
        }

    }

    public static void main(String[] args) {
        Map m =new Map();
        m.load("C:Users\\ruken\\OneDrive\\Desktop\\out.txt");
    }


    public Object get(String word) {
        return null;
    }

    public void put(String word, int i) {

    }


}

这是减少文件

package com.company;

import java.io.*;
import java.util.*;

public class Reduce {

    private Hashtable<String, Integer> map=new Hashtable< String, Integer>();

    public Hashtable < String, Integer> getHashTable () {
        return map;
    }

    public void setHashTable ( Hashtable < String, Integer> map){
        this.map =map;
    }

    public void findMin () {

    }

    public void findMax() {

    }

    public void sort (ArrayList<String> arr) throws IOException {
        Collections.sort(arr);
        Iterator it1 = arr.iterator();
        while (it1.hasNext()) {
            String word = it1.next().toString();
            System.out.println(word);

        }
    }
    //constructors
    public void reduce (ArrayList<String> words) {
        Iterator<String> it1 =words.iterator();
        while (it1.hasNext()) {
            String word=it1.next();
            System.out.println (word);
            if (map.containsKey(word)) {
                map.put(word, 1);
            }
            else {
                int count = map.get(word);
                map.put(word, count+1);
            }

            System.out.println( map.containsValue(word));


            }
        }


    }

这是workshop.txt的一部分。 这是基本的简单文本

" 致谢

我要感谢 Carl Fleischhauer 和 Prosser Gifford 有机会了解我在十个月前还不知道的人类活动领域,以及大卫和露西尔帕卡德基金会支持这个机会。 其他人提供的帮助在单独的页面上得到确认。

                                                      19 October 1992


           ***   ***   ***   ******   ***   ***   ***


                          INTRODUCTION

电子文本研讨会 (1) 汇集了来自不同项目和利益集团的代表,以比较思想、信仰、经验,尤其是以计算机化形式放置和呈现历史文本材料的方法。 大多数与会者从活动中获得了很多见识和 outlook。 但大会并没有形成一个新的国家,或者换句话说,项目和利益的多样性太大,无法将代表们吸引到一个有凝聚力的、以行动为导向的机构中。(2)"

可以使用 java stream API来计算文本中的词频

这是我的实现,后面是解释性说明。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class WordFreq {

    public static void main(String[] args) {
        Path path = Paths.get("workshop.txt");
        Function<String, String> keyMapper = Function.identity();
        Function<String, Integer> valueMapper = (word) -> Integer.valueOf(1);
        BinaryOperator<Integer> mergeFunction = (a, b) -> Integer.valueOf(a.intValue() + b.intValue());
        Supplier<Hashtable<String, Integer>> mapSupplier = () -> new Hashtable<>();
        try {
            Map<String, Integer> map = Files.lines(path)
                 .flatMap(line -> Arrays.stream(line.split("\\b")))
                 .filter(word -> word.matches("^\\w+$"))
                 .map(word -> word.toLowerCase())
                 .collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier));
            BiConsumer<String, Integer> action = (k, v) -> System.out.printf("%3d %s%n", v, k);
            map.forEach(action);
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}
  • class java.nio.file.Files中的方法lines()创建文件中文本行的stream。 在这种情况下,该文件是您的Workshop.txt文件。
  • 对于读取的文件的每一行,我使用 class java.lang.String中的方法 split() 将其拆分为单词,并将方法split() split()的数组转换为另一个 ZF7B44CFAFD5C52223D7498196BZ8A。
  • 实际上,每一行文本都在每个单词边界处拆分,因此split()方法返回的单词数组可能包含不是真正单词的字符串。 因此,我过滤“单词”以便仅提取真实单词。
  • 然后我将每个单词转换为小写,这样我的最终 map 将不区分大小写。 换句话说,单词The和单词the将被认为是同一个单词。
  • Finally I create a Map where the map key is a distinct word in the text of file workshop.txt and the map value is an Integer which is the number of occurrences of that word in the text.

由于您规定Map必须是Hashtable ,所以我明确创建了一个Hashtable来存储 stream 上的collect操作的结果。

上面代码的最后一部分显示了Hashtable的内容。

我整理了第一部分“地图”,如下所示,现在我有一个按字母排序的数组。 如下..现在我应该计算标记化的键值。 “..然而还没有让你年轻的热情为零。缩放……”

package com.company;


        import java.io.FileReader;
        import java.io.IOException;
        import java.util.*;
        import java.util.Collections;

public class Map {

    public static String fileName= "C:\\Users\\ruken\\OneDrive\\Desktop\\workshop.txt";



    private ArrayList<String> arr = new ArrayList<String>();
    public ArrayList <String>getList () {
        return this.arr;
    }

    private Hashtable<String, Integer> map = new Hashtable<String, Integer>();

    public void load() {



        try{
            FileReader f2 = new FileReader("C:\\Users\\ruken\\OneDrive\\Desktop\\workshop.txt");

            Scanner s = new Scanner(f2);
            while (s.hasNextLine()) {
                String line = s.nextLine();
                String[] words = line.split("\\s");
                for (int i=0;i<words.length; i++){
                    String word = words[i];
                    if (! word.isEmpty()){
                        System.out.println(word);
                        arr.add(word);
                    }
                }
            }
            f2.close();
            System.out.println();


    }
            catch(IOException ex1){
                System.out.println("An error occurred.");
             ex1.printStackTrace(); }
        {
            Collections.sort(arr);
            System.out.println("Sorted.");
            for (String counter: arr) {
                System.out.println(counter);
            }

        }

    }

    public static void main(String[] args) {
        Map m =new Map();
        m.load();
    }
}

进行减少的第二部分是:


package com.company;


import java.io.*;
import java.util.*;
import java.io.FileWriter;
import java.io.IOException;

public class Reduce {

    private Hashtable<String, Integer> map = new Hashtable<String, Integer>();


    public Hashtable<String, Integer> getHashTable() {
        return map;
    }

    public void setHashTable(Hashtable<String, Integer> map) {
        this.map = map;
    }


    //constructors
    public void reduce (ArrayList<String> arr) {
        Iterator<String> it1 = arr.iterator();
        while (it1.hasNext()) {
            String word = it1.next();
            System.out.println(word);
            if (map.containsKey(word)) {
                int a = (int) map.get(word);
                a++;
                map.put(word, a);
            } else {
                map.put(word, 1);
            }
        }
    }



    public void write () {

        try {
            FileWriter f1 = new FileWriter("C:\\Users\\ruken\\OneDrive\\Desktop\\output.txt");
            Iterator<String> it1 = map.keySet().iterator();
            while (it1.hasNext()) {
                String word = it1.next().toString();
                f1.write(word + "" + ":" + "" + map.get(word) + "\n" );
            }
            f1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args)  {

        Map m =new Map();
        m.load();
        Reduce r = new Reduce ();
        ArrayList<String> arr=  m.getList();
        r.reduce(arr);
        r.write();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM