トップ
新規
一覧
単語検索
最終更新
ヘルプ
ログイン
アールメカブ
hidden_Java_CharCode
をテンプレートにして作成
開始行:
[[Programming]]
[[ここ:http://kazmuzik.net/lj/67642.html]]より引用
以前、Nutch の LanguageIdentifier (Java) や、libtextcat (...
前者は、String の Language を判別するメソッドを持つ Java ...
今回は、私が書いた byte 配列の Encoding を判別する簡単な ...
まずは、EncodingIdentifier のソースコードです。
import java.io.UnsupportedEncodingException;
public class EncodingIdentifier {
private static final int DEFAULT_WEIGHT_OF_PROCESS = 1;
private static final int DEFAULT_WEIGHT_OF_ERROR = 4;
private static final String[] DEFAULT_ENCODINGS
= { "US-ASCII", "ISO-2022-JP", "ISO-8859-1",
"UTF-8", "EUC-JP", "Shift-JIS" };
private String[] encodings = null;
private int weightProcess = DEFAULT_WEIGHT_OF_PROCESS;
private int weightError = DEFAULT_WEIGHT_OF_ERROR;
//
public EncodingIdentifier() throws UnsupportedEncodingE...
this(DEFAULT_ENCODINGS);
}
public EncodingIdentifier(String[] encodings)
throws UnsupportedEncodingException {
this(encodings, DEFAULT_WEIGHT_OF_PROCESS, DEFAULT_WE...
}
public EncodingIdentifier(String[] encodings,
int weightProcess, int weight...
throws UnsupportedEncodingException {
this.encodings = encodings;
this.weightProcess = weightProcess;
this.weightError = weightError;
for (String encoding : encodings) {
"".getBytes(encoding);
}
}
public String[] getEncodings() {
return encodings;
}
public String detect(byte[] bytes) {
int maxScore = Integer.MIN_VALUE;
String possibleEncoding = null;
for (String encoding : encodings) {
int score = getScore(bytes, encoding);
// System.err.printf("%s -> %d%n", encoding, score);
if (score > maxScore) {
maxScore = score;
possibleEncoding = encoding;
}
}
return possibleEncoding;
}
private int getScore(byte[] bytes, String encoding) {
String text = null;
try {
text = new String(bytes, encoding);
}
catch (UnsupportedEncodingException e) {
return Integer.MIN_VALUE;
}
int nProcess = bytes.length - text.length();
int nError = 0;
int len = text.length();
for (int i = 0; i < len; i++) {
if (text.charAt(i) == '\ufffd') {
nError++;
}
}
return nProcess * weightProcess - nError * weightError;
}
}
コンストラクタで、可能性のある Encoding のリストを配列で...
それでは、いくつかの例で試してみます。
% cat Test1.java
import java.io.UnsupportedEncodingException;
import java.io.IOException;
public class Test1 {
private static String[] testStrings
= { "今日は良い天気です。", "\u00a9 Copyright", "This...
public static void main(String[] args) throws IOExcepti...
EncodingIdentifier identifier = new EncodingIdentifie...
String[] encodings = identifier.getEncodings();
for (String text : testStrings) {
System.out.println(text);
System.out.println("-------------------------------...
for (int i = 0; i < encodings.length; i++) {
byte[] b = text.getBytes(encodings[i]);
if (containsErrors(b) > 0) {
continue;
}
System.out.printf("%s -> %s%n", encodings[i], ide...
}
System.out.println("===============================...
}
}
private static int containsErrors(byte[] b) {
int n = 0;
// int i = 0;
for (byte x : b) {
// System.err.printf("%d %d%n", i++, x);
if (x == (byte)'?') {
n++;
}
}
return n;
}
}
% javac LanguageIdentifier.java Test1.java
% java Test1
今日は良い天気です。
----------------------------------------
ISO-2022-JP -> ISO-2022-JP
UTF-8 -> UTF-8
EUC-JP -> EUC-JP
Shift-JIS -> Shift-JIS
========================================
© Copyright
----------------------------------------
ISO-2022-JP -> ISO-2022-JP
ISO-8859-1 -> ISO-8859-1
UTF-8 -> UTF-8
EUC-JP -> EUC-JP
========================================
This is a pen.
----------------------------------------
US-ASCII -> US-ASCII
ISO-2022-JP -> US-ASCII
ISO-8859-1 -> US-ASCII
UTF-8 -> US-ASCII
EUC-JP -> US-ASCII
Shift-JIS -> US-ASCII
========================================
%
うまくいっているようです。
最後の例においては、どの Encoding で byte 配列を構成して...
ISO-8859-1 については、どのような byte 列がきても、必ずス...
ISO-2022-JP の byte 列に対しては、US-ASCII としてもスコア...
他の、UTF-8, EUC-JP, Shift-JIS のバイト列に関しては、その...
次回以降は、日本語以外の Encoding や、C言語での実装を考え...
終了行:
[[Programming]]
[[ここ:http://kazmuzik.net/lj/67642.html]]より引用
以前、Nutch の LanguageIdentifier (Java) や、libtextcat (...
前者は、String の Language を判別するメソッドを持つ Java ...
今回は、私が書いた byte 配列の Encoding を判別する簡単な ...
まずは、EncodingIdentifier のソースコードです。
import java.io.UnsupportedEncodingException;
public class EncodingIdentifier {
private static final int DEFAULT_WEIGHT_OF_PROCESS = 1;
private static final int DEFAULT_WEIGHT_OF_ERROR = 4;
private static final String[] DEFAULT_ENCODINGS
= { "US-ASCII", "ISO-2022-JP", "ISO-8859-1",
"UTF-8", "EUC-JP", "Shift-JIS" };
private String[] encodings = null;
private int weightProcess = DEFAULT_WEIGHT_OF_PROCESS;
private int weightError = DEFAULT_WEIGHT_OF_ERROR;
//
public EncodingIdentifier() throws UnsupportedEncodingE...
this(DEFAULT_ENCODINGS);
}
public EncodingIdentifier(String[] encodings)
throws UnsupportedEncodingException {
this(encodings, DEFAULT_WEIGHT_OF_PROCESS, DEFAULT_WE...
}
public EncodingIdentifier(String[] encodings,
int weightProcess, int weight...
throws UnsupportedEncodingException {
this.encodings = encodings;
this.weightProcess = weightProcess;
this.weightError = weightError;
for (String encoding : encodings) {
"".getBytes(encoding);
}
}
public String[] getEncodings() {
return encodings;
}
public String detect(byte[] bytes) {
int maxScore = Integer.MIN_VALUE;
String possibleEncoding = null;
for (String encoding : encodings) {
int score = getScore(bytes, encoding);
// System.err.printf("%s -> %d%n", encoding, score);
if (score > maxScore) {
maxScore = score;
possibleEncoding = encoding;
}
}
return possibleEncoding;
}
private int getScore(byte[] bytes, String encoding) {
String text = null;
try {
text = new String(bytes, encoding);
}
catch (UnsupportedEncodingException e) {
return Integer.MIN_VALUE;
}
int nProcess = bytes.length - text.length();
int nError = 0;
int len = text.length();
for (int i = 0; i < len; i++) {
if (text.charAt(i) == '\ufffd') {
nError++;
}
}
return nProcess * weightProcess - nError * weightError;
}
}
コンストラクタで、可能性のある Encoding のリストを配列で...
それでは、いくつかの例で試してみます。
% cat Test1.java
import java.io.UnsupportedEncodingException;
import java.io.IOException;
public class Test1 {
private static String[] testStrings
= { "今日は良い天気です。", "\u00a9 Copyright", "This...
public static void main(String[] args) throws IOExcepti...
EncodingIdentifier identifier = new EncodingIdentifie...
String[] encodings = identifier.getEncodings();
for (String text : testStrings) {
System.out.println(text);
System.out.println("-------------------------------...
for (int i = 0; i < encodings.length; i++) {
byte[] b = text.getBytes(encodings[i]);
if (containsErrors(b) > 0) {
continue;
}
System.out.printf("%s -> %s%n", encodings[i], ide...
}
System.out.println("===============================...
}
}
private static int containsErrors(byte[] b) {
int n = 0;
// int i = 0;
for (byte x : b) {
// System.err.printf("%d %d%n", i++, x);
if (x == (byte)'?') {
n++;
}
}
return n;
}
}
% javac LanguageIdentifier.java Test1.java
% java Test1
今日は良い天気です。
----------------------------------------
ISO-2022-JP -> ISO-2022-JP
UTF-8 -> UTF-8
EUC-JP -> EUC-JP
Shift-JIS -> Shift-JIS
========================================
© Copyright
----------------------------------------
ISO-2022-JP -> ISO-2022-JP
ISO-8859-1 -> ISO-8859-1
UTF-8 -> UTF-8
EUC-JP -> EUC-JP
========================================
This is a pen.
----------------------------------------
US-ASCII -> US-ASCII
ISO-2022-JP -> US-ASCII
ISO-8859-1 -> US-ASCII
UTF-8 -> US-ASCII
EUC-JP -> US-ASCII
Shift-JIS -> US-ASCII
========================================
%
うまくいっているようです。
最後の例においては、どの Encoding で byte 配列を構成して...
ISO-8859-1 については、どのような byte 列がきても、必ずス...
ISO-2022-JP の byte 列に対しては、US-ASCII としてもスコア...
他の、UTF-8, EUC-JP, Shift-JIS のバイト列に関しては、その...
次回以降は、日本語以外の Encoding や、C言語での実装を考え...
ページ名: