Split and count Words from the text in the Given Text File and Compare using Comparator in Java, It is basically used in big data operations.
package com.yubraj.core; /** * Created by yubraj_pokharel on 5/23/16. */ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.*; public class Main { public static void main(String[] args) throws IOException { String text_from_file = getTextFromFile(); text_from_file = trim_non_alpha(text_from_file); String[] final_text = text_from_file.split("[- .,]"); ArrayList<String> myText = new ArrayList<>(); myText = getStringIntoArrayList(final_text); List<HashMap<String, Integer>> final_map = new ArrayList<>(); HashMap<String, Integer> map = new HashMap<String, Integer>(); for (String temp : myText) { Integer count = map.get(temp); map.put(temp, (count == null) ? 1 : count + 1); } final_map.add(map); Collections.sort(final_map, new MapComparator()); print_format(final_map); } private static void print_format(List<HashMap<String, Integer>> final_map) { for(int i =0; i<final_map.size(); i++){ HashMap<String, Integer> new_hashmap = final_map.get(i); Set entrySet = new_hashmap.entrySet(); Iterator it = entrySet.iterator(); while (it.hasNext()){ Map.Entry me = (Map.Entry)it.next(); System.out.println("["+me.getKey()+"] = "+me.getValue()); } } } private static ArrayList<String> getStringIntoArrayList(String[] final_text) { ArrayList<String> tempArray = new ArrayList<>(); for (int i = 0; i < final_text.length; i++) { tempArray.add(final_text[i]); } return tempArray; } private static String trim_non_alpha(String text) { String temp; temp = text.replaceAll("\\b\\w+?[0-9]+\\w+?\\b", "").trim(); return temp; } private static String getTextFromFile() throws IOException { String everything; try (BufferedReader br = new BufferedReader(new FileReader("/home/yubraj_pokharel/big_data_Class/input123.txt"))) { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } everything = sb.toString(); } return everything; } private static class MapComparator implements Comparator<HashMap<String, Integer>> { @Override public int compare(HashMap<String, Integer> first, HashMap<String, Integer> second) { String firstValue = first.keySet().iterator().next(); String secondValue = second.keySet().iterator().next(); return firstValue.compareTo(secondValue); } } }
Input::
this is java and this is awesome
Output::
[awesome] = 1
[java] = 1
[and] = 1
[this] = 2
[is] = 2
Happy Coding !!!
Happy Coding !!!
0 comments:
Post a Comment