Text overlap features

From wenmin-wu/ds-skills · View interactive version

Text overlap features is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Computes n-gram overlap counts/ratios and NER entity overlap between reference and generated text as features.

How to use this skill

Skill file

Text Overlap Features

Overview

For tasks comparing two texts (summary vs. source, answer vs. reference), compute lexical overlap at multiple granularities: word-level, bigram, trigram, and named entity overlap. These features capture surface-level relevance that complements transformer embeddings.

Quick Start

import spacy
from collections import Counter

nlp = spacy.load("en_core_web_sm")

def ngram_overlap(text_a, text_b, n=2):
    tokens_a = text_a.lower().split()
    tokens_b = text_b.lower().split()
    ngrams_a = set(zip(*[tokens_a[i:] for i in range(n)]))
    ngrams_b = set(zip(*[tokens_b[i:] for i in range(n)]))
    overlap = ngrams_a & ngrams_b
    ratio = len(overlap) / max(len(ngrams_b), 1)
    return len(overlap), ratio

def ner_overlap(text_a, text_b):
    ents_a = {(e.text, e.label_) for e in nlp(text_a).ents}
    ents_b = {(e.text, e.label_) for e in nlp(text_b).ents}
    common = ents_a & ents_b
    return len(common), dict(Counter(e[1] for e in common))

Workflow

  1. Compute word overlap count and ratio
  2. Compute bigram and trigram overlap counts and ratios
  3. Extract NER entities from both texts, compute intersection by type
  4. Combine all features into a feature vector for GBDT or stacking

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.