globalchat: add minwordsize and maxwordsize to methods. Simplify

constructors.
This commit is contained in:
asofold 2012-09-07 11:31:47 +02:00
parent ad849d8273
commit 5419b3ceff
4 changed files with 49 additions and 89 deletions

View File

@ -20,17 +20,11 @@ public abstract class DigestedWords extends AbstractWordProcessor{
public boolean compress = false; public boolean compress = false;
public boolean split = false; public boolean split = false;
public float weight = 1f; public float weight = 1f;
public int minWordSize = 0;
public int maxWordSize = 0;
public DigestedWordsSettings(){ public DigestedWordsSettings(){
} }
public DigestedWordsSettings(boolean sort, boolean compress, boolean split, float weight) {
this.sort = sort;
this.compress = compress;
this.split = split;
this.weight = weight;
}
/** /**
* Returns this object. * Returns this object.
* @param config * @param config
@ -42,13 +36,18 @@ public abstract class DigestedWords extends AbstractWordProcessor{
this.compress = config.getBoolean(prefix + "compress", this.compress); this.compress = config.getBoolean(prefix + "compress", this.compress);
this.split = config.getBoolean(prefix + "split", this.split); this.split = config.getBoolean(prefix + "split", this.split);
this.weight = (float) config.getDouble(prefix + "weight", this.weight); this.weight = (float) config.getDouble(prefix + "weight", this.weight);
this.minWordSize = config.getInt(prefix + "minwordsize", this.minWordSize);
this.maxWordSize = config.getInt(prefix + "maxwordsize", this.maxWordSize);
return this; return this;
} }
} }
protected final boolean sort; protected boolean sort = false;
protected final boolean compress; protected boolean compress = false;
protected final boolean split; protected boolean split = false;
protected int minWordSize = 0;
protected int maxWordSize = 0;
protected final List<Character> letters = new ArrayList<Character>(10); protected final List<Character> letters = new ArrayList<Character>(10);
protected final List<Character> digits = new ArrayList<Character>(10); protected final List<Character> digits = new ArrayList<Character>(10);
@ -60,8 +59,13 @@ public abstract class DigestedWords extends AbstractWordProcessor{
* @param settings * @param settings
*/ */
public DigestedWords(String name, DigestedWordsSettings settings){ public DigestedWords(String name, DigestedWordsSettings settings){
this(name, settings.sort, settings.compress, settings.split); this(name);
this.weight = settings.weight; this.weight = settings.weight;
this.minWordSize = settings.minWordSize;
this.maxWordSize = settings.maxWordSize;
this.sort = settings.sort;
this.compress = settings.compress;
this.split = settings.split;
} }
/** /**
@ -72,11 +76,8 @@ public abstract class DigestedWords extends AbstractWordProcessor{
* @param compress Only use every letter once. * @param compress Only use every letter once.
* @param split Check for letters, digits, other individually (!). * @param split Check for letters, digits, other individually (!).
*/ */
public DigestedWords(String name, boolean sort, boolean compress, boolean split) { public DigestedWords(String name) {
super(name); super(name);
this.sort = sort;
this.compress = compress;
this.split = split;
} }
@Override @Override
@ -102,24 +103,23 @@ public abstract class DigestedWords extends AbstractWordProcessor{
else if (Character.isDigit(c)) digits.add(c); else if (Character.isDigit(c)) digits.add(c);
else other.add(c); else other.add(c);
} }
if (sort){
Collections.sort(letters);
Collections.sort(digits);
Collections.sort(other);
}
float score = 0; float score = 0;
if (!letters.isEmpty()){ if (prepare(letters)) score += getScore(letters, ts) * (float) letters.size();
score += getScore(letters, ts) * (float) letters.size(); if (prepare(digits)) score += getScore(digits, ts) * (float) digits.size();
} if (prepare(other)) score += getScore(other, ts) * (float) other.size();
if (!digits.isEmpty()){ return len == 0?0f:(score / (float) len);
score += getScore(digits, ts) * (float) digits.size();
}
if (!other.isEmpty()){
score += getScore(other, ts) * (float) other.size();
}
return len == 0?0f:(score / (float) len);
} }
protected boolean prepare(final List<Character> chars) {
if (chars.isEmpty()) return false;
final int size = chars.size();
if (size < minWordSize) return false;
if (maxWordSize > 0 && size > maxWordSize) return false;
if (sort) Collections.sort(chars);
return true;
}
@Override @Override
public void clear() { public void clear() {
letters.clear(); letters.clear();

View File

@ -23,14 +23,7 @@ public class FlatWords extends DigestedWords{
* split by default. * split by default.
*/ */
public FlatWordsSettings(){ public FlatWordsSettings(){
super(false, true, true, 1f); this.split = true;
}
public FlatWordsSettings (int maxSize, int nBuckets, long durBucket, float factor, boolean sort, boolean compress, boolean split, float weight){
super(sort, compress, split, weight);
this.maxSize = maxSize;
this.nBuckets = nBuckets;
this.durBucket = durBucket;
this.factor = factor;
} }
public FlatWordsSettings applyConfig(ConfigFile config, String prefix){ public FlatWordsSettings applyConfig(ConfigFile config, String prefix){
super.applyConfig(config, prefix); super.applyConfig(config, prefix);
@ -52,17 +45,12 @@ public class FlatWords extends DigestedWords{
protected long lastAdd = System.currentTimeMillis(); protected long lastAdd = System.currentTimeMillis();
public FlatWords(String name, FlatWordsSettings settings){ public FlatWords(String name, FlatWordsSettings settings){
this(name, settings.maxSize, settings.nBuckets, settings.durBucket, settings.factor, settings.sort, settings.compress, settings.split); super(name, settings);
this.weight = settings.weight; this.maxSize = settings.maxSize;
}
public FlatWords(String name, int maxSize, int nBuckets, long durBucket, float factor, boolean sort, boolean compress, boolean split){
super(name, sort, compress, split);
this.maxSize = maxSize;
entries = new LinkedHashMap<String, ActionFrequency>(maxSize); entries = new LinkedHashMap<String, ActionFrequency>(maxSize);
this.nBuckets = nBuckets; this.nBuckets = settings.nBuckets;
this.durBucket = durBucket; this.durBucket = settings.durBucket;
this.factor = factor; this.factor = settings.factor;
} }
@Override @Override

View File

@ -19,14 +19,8 @@ public class SimilarWordsBKL extends DigestedWords {
* split + compress by default. * split + compress by default.
*/ */
public SimilarWordsBKLSettings(){ public SimilarWordsBKLSettings(){
super(false, true, true, 1f); split = true;
} compress = true;
public SimilarWordsBKLSettings(int range, long durExpire, int maxSize, int maxSeek, boolean sort, boolean compress, boolean split, float weight) {
super(sort, compress, split, weight);
this.range = range;
this.durExpire = durExpire;
this.maxSize = maxSize;
this.maxSeek = maxSeek;
} }
public SimilarWordsBKLSettings applyConfig(ConfigFile config, String prefix){ public SimilarWordsBKLSettings applyConfig(ConfigFile config, String prefix){
super.applyConfig(config, prefix); super.applyConfig(config, prefix);
@ -51,16 +45,11 @@ public class SimilarWordsBKL extends DigestedWords {
protected long lastAdd = System.currentTimeMillis(); protected long lastAdd = System.currentTimeMillis();
public SimilarWordsBKL(String name, SimilarWordsBKLSettings settings){ public SimilarWordsBKL(String name, SimilarWordsBKLSettings settings){
this(name, settings.range, settings.durExpire, settings.maxSize, settings.maxSeek , settings.sort, settings.compress, settings.split); super(name, settings);
this.weight = settings.weight; this.maxSize = settings.maxSize;
} this.range = settings.range;
this.durExpire = settings.durExpire;
public SimilarWordsBKL(String name, int range, long durExpire, int maxSize, int maxSeek, boolean sort, boolean compress, boolean split) { this.maxSeek = settings.maxSeek;
super(name, sort, compress, split);
this.maxSize = maxSize;
this.range = range;
this.durExpire = durExpire;
this.maxSeek = maxSeek;
} }
@Override @Override

View File

@ -16,12 +16,8 @@ public class WordPrefixes extends DigestedWords{
* split and compress by default. * split and compress by default.
*/ */
public WordPrefixesSettings(){ public WordPrefixesSettings(){
super(false, true, true, 1f); split = true;
} compress = true;
public WordPrefixesSettings(long durExpire, int maxAdd, boolean sort, boolean compress, boolean split, float weight) {
super(sort, compress, split, weight);
this.maxAdd = maxAdd;
this.durExpire = durExpire;
} }
public WordPrefixesSettings applyConfig(ConfigFile config, String prefix){ public WordPrefixesSettings applyConfig(ConfigFile config, String prefix){
super.applyConfig(config, prefix); super.applyConfig(config, prefix);
@ -42,22 +38,9 @@ public class WordPrefixes extends DigestedWords{
protected long lastAdd = System.currentTimeMillis(); protected long lastAdd = System.currentTimeMillis();
public WordPrefixes(String name, WordPrefixesSettings settings){ public WordPrefixes(String name, WordPrefixesSettings settings){
this(name, settings.durExpire, settings.maxAdd, settings.sort, settings.compress, settings.split); super(name, settings);
this.weight = settings.weight; this.durExpire = settings.durExpire;
} this.maxAdd = settings.maxAdd;
/**
*
* @param durExpire
* @param maxAdd
* @param sort Sort letters.
* @param compress Only use every letter once.
* @param split Check for letters, digits, other individually (!).
*/
public WordPrefixes(String name, long durExpire, int maxAdd, boolean sort, boolean compress, boolean split) {
super(name, sort, compress, split);
this.durExpire = durExpire;
this.maxAdd = maxAdd;
} }
@Override @Override