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 split = false;
public float weight = 1f;
public int minWordSize = 0;
public int maxWordSize = 0;
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.
* @param config
@ -42,13 +36,18 @@ public abstract class DigestedWords extends AbstractWordProcessor{
this.compress = config.getBoolean(prefix + "compress", this.compress);
this.split = config.getBoolean(prefix + "split", this.split);
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;
}
}
protected final boolean sort;
protected final boolean compress;
protected final boolean split;
protected boolean sort = false;
protected boolean compress = false;
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> digits = new ArrayList<Character>(10);
@ -60,8 +59,13 @@ public abstract class DigestedWords extends AbstractWordProcessor{
* @param settings
*/
public DigestedWords(String name, DigestedWordsSettings settings){
this(name, settings.sort, settings.compress, settings.split);
this(name);
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 split Check for letters, digits, other individually (!).
*/
public DigestedWords(String name, boolean sort, boolean compress, boolean split) {
public DigestedWords(String name) {
super(name);
this.sort = sort;
this.compress = compress;
this.split = split;
}
@Override
@ -102,22 +103,21 @@ public abstract class DigestedWords extends AbstractWordProcessor{
else if (Character.isDigit(c)) digits.add(c);
else other.add(c);
}
if (sort){
Collections.sort(letters);
Collections.sort(digits);
Collections.sort(other);
}
float score = 0;
if (!letters.isEmpty()){
score += getScore(letters, ts) * (float) letters.size();
}
if (!digits.isEmpty()){
score += getScore(digits, ts) * (float) digits.size();
}
if (!other.isEmpty()){
score += getScore(other, ts) * (float) other.size();
}
return len == 0?0f:(score / (float) len);
if (prepare(letters)) 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();
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

View File

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

View File

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

View File

@ -16,12 +16,8 @@ public class WordPrefixes extends DigestedWords{
* split and compress by default.
*/
public WordPrefixesSettings(){
super(false, true, true, 1f);
}
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;
split = true;
compress = true;
}
public WordPrefixesSettings applyConfig(ConfigFile config, String prefix){
super.applyConfig(config, prefix);
@ -42,22 +38,9 @@ public class WordPrefixes extends DigestedWords{
protected long lastAdd = System.currentTimeMillis();
public WordPrefixes(String name, WordPrefixesSettings settings){
this(name, settings.durExpire, settings.maxAdd, settings.sort, settings.compress, settings.split);
this.weight = settings.weight;
}
/**
*
* @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;
super(name, settings);
this.durExpire = settings.durExpire;
this.maxAdd = settings.maxAdd;
}
@Override