Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
text-scorer
Advanced tools
A configurable text quality scorer/gibberish detector.
npm install text-scorer
This text scoring model implements a matrix that tracks the probabilities of character bigram and trigram transitions, i.e. a Markov chain where the event chains consist of character bigrams and trigrams and the transition probabilities correspond to approximate relative frequencies of each chain within the English language. The model consists of three major parts:
t-h
bigram is much more likely to occur than the q-g
bigram.Sample use cases:
import { TextScorer } from 'text-scorer'
import { CutoffScoreStrictness, NGramMatrix } from 'text-scorer' // Additional type imports
const textScorer = new TextScorer(useBigram?: boolean, options?: {
initialTrainingText?: string
goodSamples?: string[]
badSamples?: string[]
ignoreCase?: boolean
additionalCharsToInclude?: string
})
Instantiates new TextScorer
object. Constructor takes optional arguments useBigram
(defaults to true
, which prefers bigrams over trigrams) and options
:
options field | type | purpose/description | default value |
---|---|---|---|
initialTrainingText | string | An English corpus/text in string format to initialize N-gram probability matrix | stringified Harry Potter & the Sorcerer's Stone |
goodSamples | string[] | An array of manually selected correctly-spelled English sentences to calculate predicted cutoff scores in conjunction with badSamples | hard-coded array of English sentence strings |
badSamples | string[] | An array of gibberish strings to calculate predicted cutoff scores in conjunction with goodSamples | hard-coded array of gibberish strings |
ignoreCase | boolean | If true , converts all training input text and scoring output text to lower case. Else, considers upper lower case chars in N-grams | true |
additionalCharsToInclude | string | All unique chars in additionalCharsToInclude are appended to the base set of chars ([a-z] and space, or unicodes 97-122 and 32) to include in N-grams. | '' |
isGibberish
textScorer.isGibberish('The quick fox jumps over the lazy dog') // false
textScorer.isGibberish('Tom Brady') // false
textScorer.isGibberish('oqbwifsiehf osdfbw sjkdoo thehwei') // true
textScorer.isGibberish('This sentence is half gibberish lwpqgtyukcvi', CutoffScoreStrictness.Loose) // false
textScorer.isGibberish('This sentence is half gibberish lwpqgtyukcvi', CutoffScoreStrictness.Strict) // true
Returns whether input text string is gibberish, according to trained cutoff predictions and desired strictness. strictness
argument must be of a member of the CutoffScoreStrictness
enum (Strict | Avg | Loose
), where CutoffScoreStrictness.Strict
will classify more input strings as gibberish and CutoffScoreStrictness.Loose
will classify fewer input strings as gibberish. The strictness
argument defaults to Avg
.
trainWithEnglishText
textScorer.trainWithEnglishText(my_own_training_text) // Additional training for textScorer if desired
Trains the TextScorer
object with any training string passed to it. This will re-adjust the N-gram probabilities on top of the initial training and any prior training. Training also automatically recalibrates cutoff score predictions. Recommended to train only on long training corpus in accurate English.
recalibrateCutoffScores
textScorer.recalibrateCutoffScores(good_sample_texts, bad_sample_texts) // Recalculate predicted score cutoffs based on provided samples
Manually re-calibrate the estimated cutoff scores. Takes parameters of two hand-picked string[]
of good and bad sample texts.
getTextScore
textScorer.getTextScore('The quick fox jumps over the lazy dog') // 0.07108346875540186
textScorer.getTextScore('asdk akljhsug wertgbk') // 0.009196665505633908
Returns actual calculated number score of input text (average probability of all N-grams in input text: range between 0
and 1
with avg 1/26
for bigrams and 1/(26*26) = 1/676
for trigrams). Useful for viewing scores of input texts to choose your own hard-coded cutoff score points.
getCutoffScores
textScorer.getCutoffScores()
// {
// loose: 0.017614231370230753,
// avg: 0.025681000339544513,
// strict: 0.033747769308858276
// },
Returns predicted cutoff scores at all three strictness levels (loose
, avg
, and strict
).
getTextScoreAndCutoffs
textScorer.getTextScoreAndCutoffs('This sentence is half gibberish lwpqgtyukcvi')
// {
// cutoffs: {
// loose: 0.017614231370230753,
// avg: 0.025681000339544513,
// strict: 0.033747769308858276
// },
// score: 0.029883897109006206
// }
Returns current predicted cutoff scores of NGramMatrix
bundled together with the calculated numerical score of input text for further inspection.
interface TextScorerInterface {
NGramMatrix: NGramMatrix
trainWithEnglishText: (text: string) => void
recalibrateScoreCutoffs: (goodSamples: string[], badSamples: string[]) => void
isGibberish: (text: string, strictness?: CutoffScoreStrictness) => boolean
getTextScore: (text: string) => number
getScoreCutoffs: () => CutoffScore
getTextScoreAndCutoffs: (text: string) => { cutoffs: CutoffScore; score: number }
}
class TextScorer implements TextScorerInterface {}
interface NGramMatrix {
cutoffScores: CutoffScore
train: (text: string) => void
getScore: (text: string) => number
recalibrateCutoffScores: (goodSamples?: string[], badSamples?: string[]) => void
isGibberish: (text: string, strictness?: CutoffScoreStrictness) => boolean
}
interface NGramMatrixOptions {
initialTrainingText?: string
goodSamples?: string[]
badSamples?: string[]
ignoreCase?: boolean
additionalCharsToInclude?: string
}
enum CutoffScoreStrictness {
Strict = 'Strict',
Avg = 'Avg',
Loose = 'Loose',
}
FAQs
A configurable text scorer.
The npm package text-scorer receives a total of 5 weekly downloads. As such, text-scorer popularity was classified as not popular.
We found that text-scorer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.