UTF-8 로 인코딩 된 한글 텍스트 파일에서 음절의 빈도를 조사해본다.


더불어 이 텍스트 파일이 UTF-8인지 아닌지도 함께 출력하며,


UTF-8일 경우 euc-kr (한글 완성형) 으로 인코딩 한 파일을 만들고


UTF-8이 아닐 경우 UTF-8 로 인코딩 한 파일을 만든다.


코드는 Java 언어로 쓰여졌다.



import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.HashMap; import java.util.Iterator; public class Main { public static void main(String[] args) throws Exception { if (args.length == 0) { System.out.println("put your file name"); return; } String fileName = args[0]; if (fileName == null || fileName.trim().equals("")) { System.out.println("There is no the file name"); return; } try { boolean check = checker(fileName); System.out.println("utf-8 : " + check); encode(fileName, check); // if file is euc-kr if (!check) counter(fileName + "_to_utf8"); else counter(fileName); } catch (Exception e) { e.printStackTrace(); } } // true : utf8->euc-kr // false : euc-kr->utf8 private static void encode(String fileName, boolean encodingType) throws Exception { String from = fileName; String to = fileName; if (encodingType) to += "_to_euc-kr"; else to += "_to_utf8"; File fileDir = new File(from); BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(fileDir), encodingType ? "UTF-8" : "euc-kr")); BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(to), encodingType ? "euc-kr" : "UTF-8")); int r; while ((r = in.read()) != -1) { bw.write(r); } in.close(); bw.close(); } private static HashMap<Character, Integer> counter(String fileName) throws Exception { HashMap<Character, Integer> map = new HashMap<>(); File file = new File(fileName); FileReader filereader = new FileReader(file); int r = 0; while ((r = filereader.read()) != -1) { char c = (char) r; int counter = 1; if (map.containsKey(c)) { counter += map.get(c); } map.put(c, counter); } filereader.close(); Iterator itr = map.keySet().iterator(); while (itr.hasNext()) { char key = (char) itr.next(); if (key != '\n') System.out.println(key + " : " + map.get(key)); } return map; } private static boolean checker(String fileName) throws Exception { int mask1 = 14 << 4; int mask2 = 8 << 4; int readData = -1; short count = 0; boolean encodingType = false; FileInputStream fis = null; fis = new FileInputStream(fileName); while ((readData = fis.read()) != -1) { if (count == 0) { int firstByte = readData & mask1; if (firstByte == mask1) count++; else count += 2; } else if (count == 1) { int secondByte = readData & mask2; if (secondByte == mask2) encodingType = true; count++; } else { break; } } fis.close(); return encodingType; } }



물론 이렇게 하면 euc-kr 임에도 utf8 로 인식할 수 있는 문제가 있을 수 있다는 것을 알려주고 싶다.










참고한 곳

https://ko.wikipedia.org/wiki/UTF-8

https://groups.google.com/d/msg/clojure-kr/R1cRgy9Zugk/-3LECHlDAgAJ

https://stackoverflow.com/questions/28890907/implement-a-function-to-check-if-a-string-byte-array-follows-utf-8-format?lq=1

http://nlp.kookmin.ac.kr/data/syldown.html

http://nlp.kookmin.ac.kr/kcc/

https://stackoverflow.com/questions/27930323/java-detect-if-file-is-utf-8-or-ansi

https://studyforus.tistory.com/167


+ Recent posts