From 5419b3ceff67fb30f48857fce7bdc4b41a582c84 Mon Sep 17 00:00:00 2001 From: asofold Date: Fri, 7 Sep 2012 11:31:47 +0200 Subject: [PATCH] globalchat: add minwordsize and maxwordsize to methods. Simplify constructors. --- .../engine/processors/DigestedWords.java | 62 +++++++++---------- .../analysis/engine/processors/FlatWords.java | 24 ++----- .../engine/processors/SimilarWordsBKL.java | 25 +++----- .../engine/processors/WordPrefixes.java | 27 ++------ 4 files changed, 49 insertions(+), 89 deletions(-) diff --git a/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/DigestedWords.java b/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/DigestedWords.java index 73ecad73..8cab7c77 100644 --- a/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/DigestedWords.java +++ b/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/DigestedWords.java @@ -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 letters = new ArrayList(10); protected final List digits = new ArrayList(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,24 +103,23 @@ 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 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 public void clear() { letters.clear(); diff --git a/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/FlatWords.java b/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/FlatWords.java index cdade30d..668ae6f1 100644 --- a/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/FlatWords.java +++ b/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/FlatWords.java @@ -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(maxSize); - this.nBuckets = nBuckets; - this.durBucket = durBucket; - this.factor = factor; + this.nBuckets = settings.nBuckets; + this.durBucket = settings.durBucket; + this.factor = settings.factor; } @Override diff --git a/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/SimilarWordsBKL.java b/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/SimilarWordsBKL.java index 75b42571..aa66714d 100644 --- a/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/SimilarWordsBKL.java +++ b/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/SimilarWordsBKL.java @@ -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 diff --git a/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/WordPrefixes.java b/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/WordPrefixes.java index d7b50f9b..392dcf9b 100644 --- a/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/WordPrefixes.java +++ b/src/fr/neatmonster/nocheatplus/checks/chat/analysis/engine/processors/WordPrefixes.java @@ -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