| id | nlp-toolkit |
| version | 1.0.0 |
| name | NLP Toolkit |
| description | Advanced NLP with perplexity scoring, burstiness analysis, and entropy calculation |
| author | NeoClaw Team |
| category | detection |
| tags | ["nlp","perplexity","burstiness","entropy"] |
| dependencies | [] |
NLP Toolkit
Advanced NLP analysis for AI content detection using statistical measures.
Implementation
async function analyzeText(text, options = {}) {
const {
perplexityThreshold = 45.0,
burstinessThreshold = 0.35,
minTextLength = 50
} = options;
if (text.length < minTextLength) {
return {
error: 'Text too short for analysis',
minLength: minTextLength
};
}
const perplexity = calculatePerplexity(text);
const burstiness = calculateBurstiness(text);
const entropy = calculateEntropy(text);
const tokenStats = analyzeTokenDistribution(text);
const isAI = perplexity < perplexityThreshold && burstiness < burstinessThreshold;
const confidence = calculateConfidence(perplexity, burstiness, entropy);
return {
isAI,
confidence: Math.round(confidence * 100),
metrics: {
perplexity: Math.round(perplexity * 100) / 100,
burstiness: Math.round(burstiness * 100) / 100,
entropy: Math.round(entropy * 100) / 100
},
tokenStats,
thresholds: {
perplexity: perplexityThreshold,
burstiness: burstinessThreshold
},
explanation: isAI ?
'Low perplexity and uniform burstiness suggest AI generation' :
'Natural variation in metrics suggests human writing'
};
}
function calculatePerplexity(text) {
const words = text.toLowerCase().split(/\s+/);
const uniqueWords = new Set(words);
const ratio = uniqueWords.size / words.length;
const perplexity = 100 / ratio;
return Math.min(perplexity, 100);
}
function calculateBurstiness(text) {
const sentences = text.split(/[.!?]+/).filter(s => s.trim());
if (sentences.length < 2) return 0;
const lengths = sentences.map(s => s.split(/\s+/).length);
const avg = lengths.reduce((a, b) => a + b, 0) / lengths.length;
const variance = lengths.reduce((sum, len) => sum + Math.pow(len - avg, 2), 0) / lengths.length;
const stdDev = Math.sqrt(variance);
const burstiness = stdDev / avg;
return Math.min(burstiness, 1.0);
}
function calculateEntropy(text) {
const chars = text.toLowerCase().split('');
const freq = {};
for (const char of chars) {
freq[char] = (freq[char] || 0) + 1;
}
let entropy = 0;
const total = chars.length;
for (const count of Object.values(freq)) {
const p = count / total;
entropy -= p * Math.log2(p);
}
return entropy;
}
function analyzeTokenDistribution(text) {
const words = text.toLowerCase().split(/\s+/);
const uniqueWords = new Set(words);
return {
totalWords: words.length,
uniqueWords: uniqueWords.size,
vocabularyRichness: Math.round((uniqueWords.size / words.length) * 100) / 100
};
}
function calculateConfidence(perplexity, burstiness, entropy) {
const perplexityScore = Math.max(0, 1 - (perplexity / 100));
const burstinessScore = Math.max(0, 1 - (burstiness / 0.5));
const entropyScore = (entropy > 3.5 && entropy < 5.0) ? 0.8 : 0.4;
const confidence = (perplexityScore + burstinessScore + entropyScore) / 3;
return Math.min(confidence, 1.0);
}
module.exports = {
analyzeText,
calculatePerplexity,
calculateBurstiness,
calculateEntropy
};
Usage
const result = await skills.nlpToolkit.analyzeText(text, {
perplexityThreshold: 45.0,
burstinessThreshold: 0.35
});
console.log(`AI Detection: ${result.isAI} (${result.confidence}% confidence)`);
console.log(`Perplexity: ${result.metrics.perplexity}`);
console.log(`Burstiness: ${result.metrics.burstiness}`);
Configuration
{
"perplexityThreshold": 45.0,
"burstinessThreshold": 0.35,
"minTextLength": 50
}