mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-18 22:26:30 +01:00
Add infrastructure to sort dicts by wordcount so robot can have
smallest and human largest. Sorting works. Still need to fix when new dicts get assigned after lang change -- too often all players wind up inheriting the default.
This commit is contained in:
parent
a5fd4cef30
commit
0c9d33da3f
2 changed files with 49 additions and 13 deletions
|
@ -104,34 +104,67 @@ public class DictLangCache {
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String[] getHaveLang( Context context, int code,
|
private static DictInfo[] getInfosHaveLang( Context context, int code )
|
||||||
boolean withCounts )
|
|
||||||
{
|
{
|
||||||
ArrayList<String> al = new ArrayList<String>();
|
ArrayList<DictInfo> al = new ArrayList<DictInfo>();
|
||||||
String[] dicts = GameUtils.dictList( context );
|
String[] dicts = GameUtils.dictList( context );
|
||||||
String fmt = "%s (%d)"; // must match stripCount below
|
|
||||||
for ( String dict : dicts ) {
|
for ( String dict : dicts ) {
|
||||||
DictInfo info = getInfo( context, dict );
|
DictInfo info = getInfo( context, dict );
|
||||||
if ( code == info.langCode ) {
|
if ( code == info.langCode ) {
|
||||||
if ( withCounts ) {
|
al.add( info );
|
||||||
dict = String.format( fmt, dict, info.wordCount );
|
|
||||||
}
|
|
||||||
al.add( dict );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DictInfo[] result = al.toArray( new DictInfo[al.size()] );
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String[] getHaveLang( Context context, int code,
|
||||||
|
Comparator<DictInfo> comp,
|
||||||
|
boolean withCounts )
|
||||||
|
{
|
||||||
|
DictInfo[] infos = getInfosHaveLang( context, code );
|
||||||
|
|
||||||
|
if ( null != comp ) {
|
||||||
|
Arrays.sort( infos, comp );
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<String> al = new ArrayList<String>();
|
||||||
|
String fmt = "%s (%d)"; // must match stripCount below
|
||||||
|
for ( DictInfo info : infos ) {
|
||||||
|
String name = info.name;
|
||||||
|
if ( withCounts ) {
|
||||||
|
name = String.format( fmt, name, info.wordCount );
|
||||||
|
}
|
||||||
|
al.add( name );
|
||||||
|
}
|
||||||
String[] result = al.toArray( new String[al.size()] );
|
String[] result = al.toArray( new String[al.size()] );
|
||||||
Arrays.sort( result );
|
if ( null == comp ) {
|
||||||
|
Arrays.sort( result );
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[] getHaveLang( Context context, int code )
|
public static String[] getHaveLang( Context context, int code )
|
||||||
{
|
{
|
||||||
return getHaveLang( context, code, false );
|
return getHaveLang( context, code, null, false );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Comparator<DictInfo> s_ByCount =
|
||||||
|
new Comparator<DictInfo>() {
|
||||||
|
public int compare( DictInfo di1, DictInfo di2 )
|
||||||
|
{
|
||||||
|
return di2.wordCount - di1.wordCount;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static String[] getHaveLangByCount( Context context, int code )
|
||||||
|
{
|
||||||
|
return getHaveLang( context, code, s_ByCount, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[] getHaveLangCounts( Context context, int code )
|
public static String[] getHaveLangCounts( Context context, int code )
|
||||||
{
|
{
|
||||||
return getHaveLang( context, code, true );
|
return getHaveLang( context, code, null, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String stripCount( String nameWithCount )
|
public static String stripCount( String nameWithCount )
|
||||||
|
@ -212,9 +245,10 @@ public class DictLangCache {
|
||||||
String dict = human? CommonPrefs.getDefaultHumanDict( context )
|
String dict = human? CommonPrefs.getDefaultHumanDict( context )
|
||||||
: CommonPrefs.getDefaultRobotDict( context );
|
: CommonPrefs.getDefaultRobotDict( context );
|
||||||
if ( lang != DictLangCache.getDictLangCode( context, dict ) ) {
|
if ( lang != DictLangCache.getDictLangCode( context, dict ) ) {
|
||||||
String dicts[] = getHaveLang( context, lang );
|
String dicts[] = getHaveLangByCount( context, lang );
|
||||||
if ( dicts.length > 0 ) {
|
if ( dicts.length > 0 ) {
|
||||||
dict = dicts[0];
|
// Human gets biggest; robot gets smallest
|
||||||
|
dict = dicts[ human ? 0 : dicts.length-1 ];
|
||||||
} else {
|
} else {
|
||||||
dict = null;
|
dict = null;
|
||||||
}
|
}
|
||||||
|
@ -284,6 +318,7 @@ public class DictLangCache {
|
||||||
byte[] dict = GameUtils.openDict( context, name );
|
byte[] dict = GameUtils.openDict( context, name );
|
||||||
info = new DictInfo();
|
info = new DictInfo();
|
||||||
XwJNI.dict_getInfo( dict, JNIUtilsImpl.get(), info );
|
XwJNI.dict_getInfo( dict, JNIUtilsImpl.get(), info );
|
||||||
|
info.name = name;
|
||||||
s_nameToLang.put( name, info );
|
s_nameToLang.put( name, info );
|
||||||
}
|
}
|
||||||
return info;
|
return info;
|
||||||
|
|
|
@ -23,4 +23,5 @@ package org.eehouse.android.xw4.jni;
|
||||||
public class DictInfo {
|
public class DictInfo {
|
||||||
public int langCode;
|
public int langCode;
|
||||||
public int wordCount;
|
public int wordCount;
|
||||||
|
public String name;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue