Skip to content

Instantly share code, notes, and snippets.

@sue445
Created May 15, 2013 06:22
Show Gist options
  • Save sue445/5581978 to your computer and use it in GitHub Desktop.
Save sue445/5581978 to your computer and use it in GitHub Desktop.
jubeat++での日本語ソート実装
package net.sue445.jubeatplusplus.comparator;
import java.io.Serializable;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
/**
* ひらがなをソートするためのコンパレータ
* @author sue445
*
*/
public class HiraganaComparator implements Comparator<String>, Serializable {
private static final long serialVersionUID = 1L;
/**
* 伸ばし棒
*/
private static char HYPHEN = 'ー';
/**
* 対応文字
*/
private static final char[][] SUPPORT_CHAR_ARRAY = {
{'あ', 'あ'},
{'ぁ', 'あ'},
{'い', 'い'},
{'ぃ', 'い'},
{'う', 'う'},
{'ぅ', 'う'},
{'え', 'え'},
{'ぇ', 'え'},
{'お', 'お'},
{'ぉ', 'お'},
{'か', 'あ'},
{'が', 'あ'},
{'き', 'い'},
{'ぎ', 'い'},
{'く', 'う'},
{'ぐ', 'う'},
{'け', 'え'},
{'げ', 'え'},
{'こ', 'お'},
{'ご', 'お'},
{'さ', 'あ'},
{'ざ', 'あ'},
{'し', 'い'},
{'じ', 'い'},
{'す', 'う'},
{'ず', 'う'},
{'せ', 'え'},
{'ぜ', 'え'},
{'そ', 'お'},
{'ぞ', 'お'},
{'た', 'あ'},
{'だ', 'あ'},
{'ち', 'い'},
{'ぢ', 'い'},
{'つ', 'う'},
{'づ', 'う'},
{'っ', 'う'},
{'て', 'え'},
{'で', 'え'},
{'と', 'お'},
{'ど', 'お'},
{'な', 'あ'},
{'に', 'い'},
{'ぬ', 'う'},
{'ね', 'え'},
{'の', 'お'},
{'は', 'あ'},
{'ば', 'あ'},
{'ぱ', 'あ'},
{'ひ', 'い'},
{'び', 'い'},
{'ぴ', 'い'},
{'ふ', 'う'},
{'ぶ', 'う'},
{'ぷ', 'う'},
{'へ', 'え'},
{'べ', 'え'},
{'ぺ', 'え'},
{'ほ', 'お'},
{'ぼ', 'お'},
{'ぽ', 'お'},
{'ま', 'あ'},
{'み', 'い'},
{'む', 'う'},
{'め', 'え'},
{'も', 'お'},
{'や', 'あ'},
{'ゃ', 'あ'},
{'ゆ', 'う'},
{'ゅ', 'う'},
{'よ', 'お'},
{'ょ', 'お'},
{'ら', 'あ'},
{'り', 'い'},
{'る', 'う'},
{'れ', 'え'},
{'ろ', 'お'},
{'わ', 'あ'},
{'ゐ', 'い'},
{'ゑ', 'え'},
{'を', 'お'},
{'ん', 'ん'},
};
static class SupportCharDto{
/**
* 表示順
*/
int priority;
/**
* 文字
*/
//char character;
/**
* 伸ばした時の音
*/
char beforeHyphen;
};
private static SupportCharDto OTHER = new SupportCharDto();
/**
* charから表示順へのマッピング
*/
private static final Map<Character, SupportCharDto> CHAR_TO_DTO = new HashMap<Character, SupportCharDto>();
static{
for(int index = 0; index < SUPPORT_CHAR_ARRAY.length; index++){
SupportCharDto dto = new SupportCharDto();
dto.priority = index + 1;
dto.beforeHyphen = SUPPORT_CHAR_ARRAY[index][1];
CHAR_TO_DTO.put(SUPPORT_CHAR_ARRAY[index][0], dto);
}
OTHER.priority = 10000;
OTHER.beforeHyphen = '無';
}
/**
* {@inheritDoc}
*/
@Override
public int compare(String str1, String str2) {
for(int index = 0; index < str1.length() && index < str2.length(); index++){
int priority1 = findPriority(str1, index);
int priority2 = findPriority(str2, index);
if(priority1 != priority2){
return priority1 - priority2;
}
}
// 短い方を辞書順で先とみなす
return str1.length() - str2.length();
}
/**
* 表示順DTOを取得する
* @param c
* @return
*/
// package private
SupportCharDto findSupportDto(char c){
SupportCharDto dto = CHAR_TO_DTO.get(c);
if(dto == null){
return OTHER;
}
return dto;
}
/**
* 表示順を取得する
* @param str
* @param index
* @return
*/
// package private
int findPriority(String str, int index){
char currentChar = str.charAt(index);
if(currentChar == HYPHEN && index > 0){
// 伸ばし棒の前の文字から母音を判定する
char beforeChar = str.charAt(index - 1);
SupportCharDto dto = findSupportDto(beforeChar);
SupportCharDto beforeDto = findSupportDto(dto.beforeHyphen);
return beforeDto.priority;
}
SupportCharDto currentDto = findSupportDto(currentChar);
return currentDto.priority;
}
/**
* ひらがなのみで構成されているかどうか
* @param str
* @return
*/
public static boolean isHiragaraOnly(String str){
for(int index = 0; index < str.length(); index++){
char c = str.charAt(index);
if(c == HYPHEN){
continue;
}
SupportCharDto dto = CHAR_TO_DTO.get(c);
if(dto == null){
return false;
}
}
return true;
}
}
@sue445
Copy link
Author

sue445 commented May 15, 2013

楽曲マスタ

http://jubeatplusplus.appspot.com/file/music.xls

利用例

int ret = new HiraganaComparator(). compare("うぃりあむてるじょきょく", "すうぃーとれいん");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment