Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.TreeMap;
- public class WordStatLineIndex {
- public static void main(String[] args) {
- if (args == null || args.length < 2) {
- System.out.println("Specify input and output files");
- return;
- }
- //args = new String[2];
- //args[1] = "output.txt";
- //args[0] = "Test.txt";
- try {
- TreeMap<String, List<Pair>> map = readToMap(args[0]);
- try (PrintWriter out = new PrintWriter(new File(args[1]), "utf8")) {
- for (Map.Entry<String, List<Pair>> pair : map.entrySet()) {
- List<Pair> f = pair.getValue();
- String word = pair.getKey();
- out.print(word + " " + f.size() + " ");
- for (int i = 0; i < f.size(); i++) {
- out.print(f.get(i).line + ":" + f.get(i).index);
- if (i != f.size() - 1) {
- out.print(" ");
- }
- }
- out.println();
- }
- } catch (FileNotFoundException e) {
- System.out.println("Specify output file");
- } catch (UnsupportedEncodingException e) {
- System.out.println("Unsupported encoding, please use utf8");
- }
- } catch (IOException e) {
- System.out.println("Input file not found");
- }
- }
- private static TreeMap<String, List<Pair>> readToMap(String inputfile) throws IOException {
- List<String> lines = new ArrayList<>();
- FastScanner in = new FastScanner(new FileInputStream(inputfile));
- while (in.hasNextLine()) {
- lines.add(in.readLine());
- }
- in.close();
- TreeMap<String, List<Pair>> map = new TreeMap<>();
- for (int line = 0; line < lines.size(); line++) {
- String[] words = lines.get(line).replaceAll("[^\\p{L}\\p{Pd}']", " ").trim().toLowerCase().split(" +"); // trim - удаляет пробелы по краям, split - делает массив по пробелам
- for (int i = 0; i < words.length; i++) {
- String word = words[i];
- List<Pair> indices = map.getOrDefault(word, new ArrayList<>());
- indices.add(new Pair(line + 1, i + 1));
- map.put(word, indices);
- }
- }
- return map;
- }
- static class Pair {
- int line;
- int index;
- Pair(int line, int index) {
- this.line = line;
- this.index = index;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement