Class TrainJunkModel
BuildJunkTrainingData.
z1 (codepoint-bigram log-probability) is trained per script by bucketing
every bigram to its script (JunkDetector.forEachScriptBigram(int[], JunkDetector.BigramSink)) and
building a per-script sorted-occupied bigram table with unigram backoff.
z2 (Unicode block-transition), z3 (control-byte fraction), and z4
(script-transition) are single global document-level features. All features
are calibrated (mu/sigma) and combined by a single global contrastive
combiner producing logit = w1*z1 + ... + w9*z9 + bias; positive
values indicate clean text.
The output model is written in the JUNKDET1 gzipped binary format
read by JunkDetector.load(InputStream) — see that method's javadoc for the byte
layout. The file carries JunkDetector.VERSION; the loader rejects
any other version.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classReturn type ofquantizeFloats(float[]).static final class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BigramTablesbuildBigramTablesFromCounts(HashMap<Long, long[]> pairCounts, HashMap<Integer, long[]> unigramCounts, long unigramTotal, int minBigramCount, double loadFactor, int keyIndexBits) Builds theBigramTablescarrier from pre-tallied pair/unigram counts.static float[]calibrateBucketScript(Path file, String script, Map<String, BigramTables> tables) Bucket-model z1 calibration {mu, sigma} for one script, sampling windows fromfileand taking that script's bucket mean log-prob per window (the inference z1 input).static voidstatic float[]quantizeDequantizeRoundTrip(float[] in) Quantize a float[] to int16 and dequantize back, returning a new float[] with the int16-precision values.quantizeFloats(float[] in) quantizeToShorts(float[] in) int16 (unsigned 0-65535) quantization of a float[].static voidsaveModel(TreeMap<String, BigramTables> f1Tables, TreeMap<String, float[]> f1Calibrations, float[] blockTable, float[] blockCal, float[] controlCal, float[] combinerWeights, List<String> scriptBuckets, float[] scriptTransTable, float[] scriptTransCal, Path output) Writes a model file in the current binary format.static BigramTablestrainBigramTablesForScript(Path trainFile, int minBigramCount, double loadFactor, int keyIndexBits) Builds theBigramTablescarrier for the dominant script bucket of one*.train.gzfile.
-
Constructor Details
-
TrainJunkModel
public TrainJunkModel()
-
-
Method Details
-
main
- Throws:
IOException
-
calibrateBucketScript
public static float[] calibrateBucketScript(Path file, String script, Map<String, BigramTables> tables) throws IOException Bucket-model z1 calibration {mu, sigma} for one script, sampling windows fromfileand taking that script's bucket mean log-prob per window (the inference z1 input). Public for round-trip tests.- Throws:
IOException
-
trainBigramTablesForScript
public static BigramTables trainBigramTablesForScript(Path trainFile, int minBigramCount, double loadFactor, int keyIndexBits) throws IOException Builds theBigramTablescarrier for the dominant script bucket of one*.train.gzfile. Tallies the file via the sharedJunkDetector.forEachScriptBigram(int[], JunkDetector.BigramSink)enumeration (so trained tables fit scored bigrams), then builds tables from the bucket whose name matches the file's script (derived from the filename), falling back to the bucket with the most bigrams. Convenience for tests; the full pipeline tallies all files together inmain(String[]).- Parameters:
trainFile- the per-script*.train.gzminBigramCount- drop pairs whose count is below thisloadFactor- unused (retained for signature compatibility)keyIndexBits- bit-width per index in the packed key (each side of the pair must fit)- Throws:
IOException
-
buildBigramTablesFromCounts
public static BigramTables buildBigramTablesFromCounts(HashMap<Long, long[]> pairCounts, HashMap<Integer, long[]> unigramCounts, long unigramTotal, int minBigramCount, double loadFactor, int keyIndexBits) Builds theBigramTablescarrier from pre-tallied pair/unigram counts. Drops pairs belowminBigramCount, assigns dense codepoint indices, and packs the sorted-occupied bigram table; unigram log-probs useunigramTotalas the denominator.loadFactoris retained for signature compatibility but unused: the sorted-occupied table (binary-search lookup) has no load factor. -
saveModel
public static void saveModel(TreeMap<String, BigramTables> f1Tables, TreeMap<String, float[]> f1Calibrations, float[] blockTable, float[] blockCal, float[] controlCal, float[] combinerWeights, List<String> scriptBuckets, float[] scriptTransTable, float[] scriptTransCal, Path output) throws IOExceptionWrites a model file in the current binary format. Layout: gzip envelope aroundJUNKDET1magic +JunkDetector.VERSIONbyte + global script-transition / block / control / z5 / z6 / z9 sections + the global combiner weight vector + per-script sections (z1 calibration + bigram tables). SeeJunkDetector.load(InputStream)for the load-side spec.- Throws:
IOException
-
quantizeDequantizeRoundTrip
public static float[] quantizeDequantizeRoundTrip(float[] in) Quantize a float[] to int16 and dequantize back, returning a new float[] with the int16-precision values. Used at training time so downstream calibration (mu, sigma) is computed on values the inference path will actually see, eliminating train/infer drift. 65536 levels keep ~0.0002 nats/level resolution, essentially lossless for our [-15, -1] log-prob range. -
quantizeToShorts
int16 (unsigned 0-65535) quantization of a float[]. Linear mapping[min, max] → [0, 65535]. -
quantizeFloats
-