Install any skill in seconds. Free to start, no credit card required.
Get Started Free →NLP analysis with perplexity scoring, burstiness, and entropy metrics
.claude/skills/brycewang-stanford-nlp-toolkit-guide/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 131% | 0% |
| case-02 | ✗→✓ | ▲ Improved | -3% | 0% |
| case-03 | ✗→✓ | ▲ Improved | -16% | 0% |
| case-05 | ✗→✓ | ▲ Improved | 29% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 35% | 0% |
Natural Language Processing research requires a diverse set of analytical tools beyond standard model training. Text quality assessment, AI-generated text detection, linguistic feature extraction, and corpus analysis all depend on well-understood metrics: perplexity, burstiness, entropy, and their variants.
This guide provides practical implementations of these core NLP metrics alongside patterns for tokenization, embedding analysis, and text feature engineering. The focus is on metrics used in active research areas -- AI text detection (perplexity + burstiness classifiers), information-theoretic analysis of corpora, and linguistic diversity measurement.
These tools are framework-agnostic where possible, but leverage Hugging Face Transformers for language model operations and standard Python scientific computing libraries for statistical analysis.
Perplexity measures how well a language model predicts a text. Lower perplexity means the text is more predictable to the model -- a key signal in AI text detection, model evaluation, and domain adaptation.
pythonimport torch import numpy as np from transformers import AutoModelForCausalLM, AutoTokenizer def compute_perplexity(text: str, model_name: str = "gpt2") -> dict: """ Compute token-level and text-level perplexity using a causal LM. Returns: dict with 'perplexity', 'log_likelihood', 'token_perplexities' """ tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) model.eval() encodings = tokenizer(text, return_tensors="pt", truncation=True, max_length=1024) input_ids = encodings.input_ids with torch.no_grad(): outputs = model(input_ids, labels=input_ids) neg_log_likelihood = outputs.loss.item() # Token-level perplexities for analysis with torch.no_grad(): logits = outputs.logits[:, :-1, :] # Shift for next-token prediction targets = input_ids[:, 1:] log_probs = torch.log_softmax(logits, dim=-1) token_log_probs = log_probs.gather(2, targets.unsqueeze(-1)).squeeze(-1) token_perplexities = torch.exp(-token_log_probs).squeeze().tolist() perplexity = np.exp(neg_log_likelihood) return { "perplexity": perplexity, "log_likelihood": -neg_log_likelihood, "token_perplexities": token_perplexities, "num_tokens": input_ids.size(1), }
Burstiness measures the tendency of words to appear in clusters rather than uniformly across a text. Human writing tends to be "burstier" -- once a topic is introduced, related terms cluster together, then disappear.
pythonfrom collections import Counter import numpy as np def compute_burstiness(text: str, min_freq: int = 2) -> dict: """ Compute burstiness score for a text. Burstiness B = (sigma - mu) / (sigma + mu) where sigma and mu are the std dev and mean of inter-arrival times. B ranges from -1 (periodic) to 1 (bursty). Human text typically B > 0. """ words = text.lower().split() word_positions = {} for i, word in enumerate(words): word_positions.setdefault(word, []).append(i) burstiness_scores = {} for word, positions in word_positions.items(): if len(positions) < min_freq: continue inter_arrivals = np.diff(positions) mu = np.mean(inter_arrivals) sigma = np.std(inter_arrivals) if mu + sigma == 0: burstiness_scores[word] = 0.0 else: burstiness_scores[word] = (sigma - mu) / (sigma + mu) # Aggregate burstiness if burstiness_scores: avg_burstiness = np.mean(list(burstiness_scores.values())) else: avg_burstiness = 0.0 return { "average_burstiness": avg_burstiness, "word_burstiness": burstiness_scores, "num_words_analyzed": len(burstiness_scores), }
pythonfrom collections import Counter import numpy as np def compute_entropy(text: str, level: str = "word") -> dict: """ Compute Shannon entropy at word or character level. Higher entropy indicates more diverse, less predictable text. AI-generated text often has lower entropy than human text. """ if level == "word": tokens = text.lower().split() elif level == "character": tokens = list(text.lower()) else: raise ValueError("level must be 'word' or 'character'") counts = Counter(tokens) total = sum(counts.values()) probabilities = np.array([c / total for c in counts.values()]) entropy = -np.sum(probabilities * np.log2(probabilities + 1e-12)) max_entropy = np.log2(len(counts)) if len(counts) > 1 else 1.0 normalized_entropy = entropy / max_entropy return { "entropy": entropy, "normalized_entropy": normalized_entropy, "vocabulary_size": len(counts), "total_tokens": total, "type_token_ratio": len(counts) / total, } def compute_conditional_entropy(text: str, n: int = 2) -> float: """Compute conditional entropy H(X_n | X_{n-1}) for n-gram analysis.""" words = text.lower().split() if len(words) < n: return 0.0 ngrams = [tuple(words[i:i+n]) for i in range(len(words) - n + 1)] contexts = [ng[:-1] for ng in ngrams] context_counts = Counter(contexts) ngram_counts = Counter(ngrams) h = 0.0 total = len(ngrams) for ngram, count in ngram_counts.items(): context = ngram[:-1] p_ngram = count / total p_context = context_counts[context] / total h -= p_ngram * np.log2(count / context_counts[context] + 1e-12) return h
Combining perplexity, burstiness, and entropy into a detection pipeline:
pythondef analyze_text_authenticity(text: str) -> dict: """ Multi-signal analysis for AI vs. human text classification. Uses perplexity, burstiness, and entropy as features. """ perplexity_result = compute_perplexity(text) burstiness_result = compute_burstiness(text) entropy_result = compute_entropy(text, level="word") char_entropy = compute_entropy(text, level="character") # Heuristic thresholds from literature signals = { "low_perplexity": perplexity_result["perplexity"] < 30, "low_burstiness": burstiness_result["average_burstiness"] < 0.1, "low_entropy": entropy_result["normalized_entropy"] < 0.7, "uniform_token_ppl": np.std(perplexity_result["token_perplexities"]) < 5, } ai_score = sum(signals.values()) / len(signals) return { "perplexity": perplexity_result["perplexity"], "burstiness": burstiness_result["average_burstiness"], "word_entropy": entropy_result["entropy"], "char_entropy": char_entropy["entropy"], "type_token_ratio": entropy_result["type_token_ratio"], "ai_likelihood_score": ai_score, "signals": signals, }
pythonfrom transformers import AutoTokenizer def compare_tokenizers(text: str, models: list = None) -> dict: """Compare tokenization across different models for research analysis.""" if models is None: models = ["gpt2", "bert-base-uncased", "facebook/opt-1.3b"] results = {} for model_name in models: tokenizer = AutoTokenizer.from_pretrained(model_name) tokens = tokenizer.tokenize(text) results[model_name] = { "num_tokens": len(tokens), "tokens": tokens[:50], # First 50 for inspection "vocab_size": tokenizer.vocab_size, "compression_ratio": len(text) / len(tokens), } return results
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 13,228 | 34,591 | +161% | 1 | 1 | 0% | 2,540 | 5,877 | +131% | 0 | 0 | — |
case-02 | fail→pass | 25,588 | 13,223 | -48% | 1 | 1 | 0% | 5,293 | 5,125 | -3% | 0 | 0 | — |
case-03 | fail→pass | 42,798 | 19,916 | -53% | 1 | 1 | 0% | 8,294 | 6,943 | -16% | 0 | 0 | — |
case-04 | fail→fail | 17,877 | 17,157 | -4% | 1 | 1 | 0% | 3,049 | 5,550 | +82% | 0 | 0 | — |
case-05 | fail→pass | 14,920 | 3,243 | -78% | 1 | 1 | 0% | 2,272 | 2,923 | +29% | 0 | 0 | — |
case-06 | fail→pass | 15,073 | 5,219 | -65% | 1 | 1 | 0% | 2,392 | 3,234 | +35% | 0 | 0 | — |
case-07 | fail→pass | 11,707 | 2,708 | -77% | 1 | 1 | 0% | 1,757 | 2,822 | +61% | 0 | 0 | — |
case-08 | fail→fail | 14,728 | 14,978 | +2% | 1 | 1 | 0% | 2,318 | 4,828 | +108% | 0 | 0 | — |
case-09 | pass→pass | 12,329 | 8,866 | -28% | 1 | 1 | 0% | 2,106 | 4,014 | +91% | 0 | 0 | — |
case-10 | pass→pass | 19,564 | 24,238 | +24% | 1 | 1 | 0% | 3,484 | 6,798 | +95% | 0 | 0 | — |
case-11 | fail→fail | 22,715 | 18,559 | -18% | 1 | 1 | 0% | 3,912 | 5,543 | +42% | 0 | 0 | — |
case-12 | pass→pass | 13,789 | 12,459 | -10% | 1 | 1 | 0% | 2,347 | 4,358 | +86% | 0 | 0 | — |
case-13 | pass→pass | 8,952 | 2,924 | -67% | 1 | 1 | 0% | 1,572 | 2,853 | +81% | 0 | 0 | — |
case-14 | fail→pass | 13,440 | 14,001 | +4% | 1 | 1 | 0% | 2,216 | 4,667 | +111% | 0 | 0 | — |
case-15 | pass→pass | 15,149 | 18,457 | +22% | 1 | 1 | 0% | 2,466 | 5,043 | +105% | 0 | 0 | — |
case-16 | pass→fail | 19,034 | 20,259 | +6% | 1 | 1 | 0% | 2,572 | 5,874 | +128% | 0 | 0 | — |
case-17 | pass→pass | 15,284 | 17,154 | +12% | 1 | 1 | 0% | 2,354 | 5,263 | +124% | 0 | 0 | — |
case-18 | pass→pass | 16,139 | 22,511 | +39% | 1 | 1 | 0% | 2,404 | 5,484 | +128% | 0 | 0 | — |
case-19 | fail→fail | 20,492 | 29,721 | +45% | 1 | 1 | 0% | 3,130 | 7,577 | +142% | 0 | 0 | — |
case-20 | pass→pass | 20,006 | 31,808 | +59% | 1 | 1 | 0% | 3,727 | 7,018 | +88% | 0 | 0 | — |
case-21 | pass→pass | 12,522 | 12,371 | -1% | 1 | 1 | 0% | 2,626 | 5,044 | +92% | 0 | 0 | — |
case-22 | pass→pass | 19,175 | 20,812 | +9% | 1 | 1 | 0% | 3,072 | 6,397 | +108% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +27 percentage points is the difference between those two pass rates over the 22 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.