Sentiment conditioned qa span

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

Sentiment conditioned qa span is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Prepend a sentiment token as the query in a QA-style input to condition span extraction on sentiment class without architectural changes

How to use this skill

Skill file

Sentiment-Conditioned QA Span Extraction

Overview

For extracting sentiment-bearing spans from text, frame it as a QA task where the "question" is a single sentiment token. Encode the sentiment label (positive/negative/neutral) as a specific vocabulary token ID and prepend it before the text. The model learns to extract different spans depending on which sentiment token is present. No architecture changes needed — just input formatting.

Quick Start

from transformers import RobertaTokenizer

tokenizer = RobertaTokenizer.from_pretrained('roberta-base')

# Map sentiment labels to specific token IDs
sentiment_id = {
    'positive': tokenizer.encode('positive', add_special_tokens=False)[0],
    'negative': tokenizer.encode('negative', add_special_tokens=False)[0],
    'neutral': tokenizer.encode('neutral', add_special_tokens=False)[0],
}

def encode_qa_input(text, sentiment, tokenizer, max_len=96):
    """Build QA-style input: [CLS] sentiment_token [SEP] [SEP] text [SEP]"""
    enc = tokenizer.encode(text, add_special_tokens=False)
    s_tok = sentiment_id[sentiment]
    
    input_ids = [tokenizer.cls_token_id] + [s_tok] + \
                [tokenizer.sep_token_id] * 2 + enc + [tokenizer.sep_token_id]
    token_type_ids = [0] * 4 + [0] * (len(enc) + 1)
    attention_mask = [1] * len(input_ids)
    
    # Pad
    pad_len = max_len - len(input_ids)
    input_ids += [tokenizer.pad_token_id] * pad_len
    token_type_ids += [0] * pad_len
    attention_mask += [0] * pad_len
    
    return input_ids, attention_mask, token_type_ids

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.