Skip to content

Instantly share code, notes, and snippets.

@sujathakvr
Created November 9, 2016 16:52
Show Gist options
  • Save sujathakvr/5ed80b0f2e4d450e3db370e2ffaef82c to your computer and use it in GitHub Desktop.
Save sujathakvr/5ed80b0f2e4d450e3db370e2ffaef82c to your computer and use it in GitHub Desktop.
package com.ourOffice.utils;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
public class Utils {
/**
* This method is used to extract the file extension
*
* @param fileName
* input file name whose extension needs to be extracted
* @return
*/
public static String getFileExtension(String fileName) {
String extension = null;
int i = fileName.lastIndexOf('.');
if (i > 0) {
extension = fileName.substring(i + 1);
}
return extension;
}
/**
* This method is used to extract the file name without the extension
*
* @param fileName
* input file name has to be extracted
* @return
*/
public static String getFileNameWithoutExtension(String fileName) {
String fileNameAlone = null;
int i = fileName.lastIndexOf('.');
if (i > 0) {
fileNameAlone = fileName.substring(0, i);
}
return fileNameAlone;
}
/**
* This method is used for generating the csv file
*
* @param fileName
* Name of the output csv file
* @param hMap
* HashMap containing words and count
* @param totalWordsCounter
* total words counter in a file
*/
public static void generateCsvFile(String fileName, HashMap<String, Integer> hMap, int totalWordsCounter) {
try (FileWriter writer = new FileWriter(fileName, false);) {
writer.append("Word");
writer.append(',');
writer.append("Doc Frequency");
writer.append(',');
writer.append("Doc Frequency/1000");
writer.append('\n');
for (String key : hMap.keySet()) {
String value = hMap.get(key).toString();
double valueDouble = Double.parseDouble(value);
double totalWordsCounterDouble = totalWordsCounter;
writer.append(key);
writer.append(',');
writer.append(value);
writer.append(',');
// If number of occurrences / Total Words is needed, uncomment
// the next line and comment the one after next line.
// writer.append(value + "/" + totalWordsCounter);
writer.append(Double.valueOf(valueDouble / totalWordsCounterDouble).toString());
writer.append('\n');
}
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment