Skill

Extract Text Features for ML

Text Feature Extractor transforms raw text into numerical features for machine learning, extracting statistical, linguistic, and n-gram patterns from documents.

Works with githubspacysklearn

Maintainer of this project? Claim this page to edit the listing.


91
Spark score
out of 100
Updated 7 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Transform raw text into structured numerical features for advanced machine learning analysis. This asset extracts a wide range of statistical, linguistic, and n-gram features, preparing your text data for classification, clustering, and other ML tasks.

Outcomes

What it gets done

01

Extract statistical text features (length, diversity, readability).

02

Generate linguistic features (POS tags, entities).

03

Create TF-IDF and n-gram vector representations.

04

Engineer domain-specific features for social media and documents.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-text-feature-extractor | bash

Overview

Text Feature Extractor

Text Feature Extractor converts unstructured text into structured numerical representations for machine learning pipelines. It extracts statistical features (character/word counts, lexical diversity, readability scores), linguistic features (POS tags, named entities), and vectorized representations (TF-IDF, n-grams) from text corpora. Use this skill when building ML models that require text input (document classification, sentiment analysis, authorship detection), when analyzing text corpus characteristics, or when engineering domain-specific features for social media, emails, or formal documents. Use it for feature selection and correlation analysis to optimize model performance. Do NOT use this for end-to-end model training (it focuses on feature extraction only), or when you need real-time streaming text processing without the ability to cache expensive NLP computations.

What it does

Text Feature Extractor converts unstructured text into structured numerical representations for machine learning pipelines. It extracts statistical features (character/word counts, lexical diversity, readability scores), linguistic features (POS tags, named entities), and vectorized representations (TF-IDF, n-grams) from text corpora. The skill specializes in feature engineering for classification, clustering, and analysis tasks.

When to use - and when NOT to

Use this skill when building ML models that require text input (document classification, sentiment analysis, authorship detection), when analyzing text corpus characteristics, or when engineering domain-specific features for social media, emails, or formal documents. Use it for feature selection and correlation analysis to optimize model performance.

Do NOT use this for end-to-end model training (it focuses on feature extraction only), or when you need real-time streaming text processing without the ability to cache expensive NLP computations.

Inputs and outputs

You provide raw text strings or text corpora. The extractor processes text to generate numerical features including length-based metrics, lexical diversity scores, readability indices, punctuation statistics, POS tag distributions, named entity counts, TF-IDF matrices, and n-gram vectors.

Integrations

The skill integrates with scikit-learn (TfidfVectorizer, CountVectorizer, SelectKBest for feature selection), spaCy (for POS tagging, named entity recognition, and dependency parsing), textstat (for readability metrics including Flesch-Kincaid and Gunning Fog indices), pandas and numpy (for feature matrix construction and manipulation), and scipy (for correlation analysis).

Here's the core implementation:

import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from textstat import flesch_kincaid_grade, gunning_fog
import spacy
import re
from collections import Counter

class TextFeatureExtractor:
    def __init__(self):
        self.nlp = spacy.load("en_core_web_sm")
        
    def extract_basic_stats(self, text):
        """Extract fundamental statistical features"""
        features = {}
        
        # Length features
        features['char_count'] = len(text)
        features['word_count'] = len(text.split())
        features['sentence_count'] = len(re.split(r'[.!?]+', text))
        features['avg_word_length'] = np.mean([len(word) for word in text.split()])
        
        # Lexical diversity
        words = text.lower().split()
        features['unique_words'] = len(set(words))
        features['type_token_ratio'] = len(set(words)) / len(words) if words else 0
        
        # Punctuation
        features['exclamation_count'] = text.count('!')
        features['question_count'] = text.count('?')
        features['comma_count'] = text.count(',')
        features['punctuation_ratio'] = sum(1 for c in text if c in '.,!?;:') / len(text)
        
        return features

Who it's for

Data scientists and ML engineers building text classification or clustering models need this for systematic feature engineering. NLP researchers analyzing corpus characteristics use it for linguistic feature extraction. Product teams working on content moderation, spam detection, or document categorization apply it for domain-specific feature sets (social media features like hashtag counts, document features like email addresses and phone numbers).

Source README

Text Feature Extractor Expert

You are an expert in text feature extraction, natural language processing, and feature engineering for machine learning. You specialize in identifying, extracting, and transforming textual data into meaningful numerical representations that can be used for analysis, classification, clustering, and other ML tasks.

Core Text Feature Categories

Statistical Features

  • Length-based: Character count, word count, sentence count, average word length
  • Lexical diversity: Type-token ratio, unique word percentage, vocabulary richness
  • Readability: Flesch-Kincaid score, gunning fog index, syllable complexity
  • Punctuation: Punctuation density, specific punctuation counts (!, ?, ...)

Linguistic Features

  • Part-of-speech: POS tag distributions, noun/verb/adjective ratios
  • Syntactic: Parse tree depth, dependency relations, clause complexity
  • Semantic: Named entity counts, sentiment polarity, emotion detection
  • Stylistic: Formality scores, register classification, authorship markers

N-gram and Bag-of-Words Features

  • Unigrams: Individual word frequencies, TF-IDF scores
  • Bigrams/Trigrams: Sequential word pair/triplet patterns
  • Character n-grams: Subword features for morphology and style
  • Skip-grams: Non-contiguous word combinations

Feature Extraction Implementation

import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from textstat import flesch_kincaid_grade, gunning_fog
import spacy
import re
from collections import Counter

class TextFeatureExtractor:
    def __init__(self):
        self.nlp = spacy.load("en_core_web_sm")
        
    def extract_basic_stats(self, text):
        """Extract fundamental statistical features"""
        features = {}
        
        # Length features
        features['char_count'] = len(text)
        features['word_count'] = len(text.split())
        features['sentence_count'] = len(re.split(r'[.!?]+', text))
        features['avg_word_length'] = np.mean([len(word) for word in text.split()])
        
        # Lexical diversity
        words = text.lower().split()
        features['unique_words'] = len(set(words))
        features['type_token_ratio'] = len(set(words)) / len(words) if words else 0
        
        # Punctuation
        features['exclamation_count'] = text.count('!')
        features['question_count'] = text.count('?')
        features['comma_count'] = text.count(',')
        features['punctuation_ratio'] = sum(1 for c in text if c in '.,!?;:') / len(text)
        
        return features
    
    def extract_readability(self, text):
        """Extract readability and complexity metrics"""
        return {
            'flesch_kincaid': flesch_kincaid_grade(text),
            'gunning_fog': gunning_fog(text),
            'avg_sentence_length': len(text.split()) / len(re.split(r'[.!?]+', text))
        }
    
    def extract_linguistic_features(self, text):
        """Extract NLP-based linguistic features"""
        doc = self.nlp(text)
        
        # POS tag distribution
        pos_counts = Counter([token.pos_ for token in doc])
        total_tokens = len(doc)
        
        features = {
            'noun_ratio': pos_counts.get('NOUN', 0) / total_tokens,
            'verb_ratio': pos_counts.get('VERB', 0) / total_tokens,
            'adj_ratio': pos_counts.get('ADJ', 0) / total_tokens,
            'adv_ratio': pos_counts.get('ADV', 0) / total_tokens
        }
        
        # Named entities
        features['entity_count'] = len(doc.ents)
        features['person_entities'] = sum(1 for ent in doc.ents if ent.label_ == 'PERSON')
        features['org_entities'] = sum(1 for ent in doc.ents if ent.label_ == 'ORG')
        
        return features

Advanced Vectorization Techniques

def create_feature_matrix(texts, feature_types=['tfidf', 'ngrams', 'stats']):
    """Create comprehensive feature matrix from text corpus"""
    extractor = TextFeatureExtractor()
    feature_matrix = []
    
    for text in texts:
        text_features = {}
        
        if 'stats' in feature_types:
            text_features.update(extractor.extract_basic_stats(text))
            text_features.update(extractor.extract_readability(text))
            text_features.update(extractor.extract_linguistic_features(text))
            
        feature_matrix.append(text_features)
    
    df = pd.DataFrame(feature_matrix)
    
    # Add vectorized features
    if 'tfidf' in feature_types:
        tfidf = TfidfVectorizer(max_features=1000, stop_words='english')
        tfidf_matrix = tfidf.fit_transform(texts)
        tfidf_df = pd.DataFrame(tfidf_matrix.toarray(), 
                               columns=[f'tfidf_{word}' for word in tfidf.get_feature_names_out()])
        df = pd.concat([df, tfidf_df], axis=1)
    
    if 'ngrams' in feature_types:
        ngram_vectorizer = CountVectorizer(ngram_range=(2, 3), max_features=500)
        ngram_matrix = ngram_vectorizer.fit_transform(texts)
        ngram_df = pd.DataFrame(ngram_matrix.toarray(),
                               columns=[f'ngram_{gram}' for gram in ngram_vectorizer.get_feature_names_out()])
        df = pd.concat([df, ngram_df], axis=1)
    
    return df

Domain-Specific Feature Engineering

Social Media Text

def extract_social_features(text):
    """Features specific to social media content"""
    return {
        'hashtag_count': len(re.findall(r'#\w+', text)),
        'mention_count': len(re.findall(r'@\w+', text)),
        'url_count': len(re.findall(r'http[s]?://\S+', text)),
        'caps_ratio': sum(1 for c in text if c.isupper()) / len(text),
        'emoji_count': len(re.findall(r'[😀-🿿]', text))
    }

Email/Document Classification

def extract_document_features(text):
    """Features for formal document analysis"""
    return {
        'email_addresses': len(re.findall(r'\S+@\S+', text)),
        'phone_numbers': len(re.findall(r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b', text)),
        'dates': len(re.findall(r'\b\d{1,2}[/-]\d{1,2}[/-]\d{2,4}\b', text)),
        'currency': len(re.findall(r'\$\d+', text)),
        'formal_words': len([word for word in text.split() if word in formal_vocabulary])
    }

Feature Selection and Optimization

Correlation-Based Selection

from sklearn.feature_selection import SelectKBest, f_classif
from scipy.stats import pearsonr

def select_best_features(X, y, k=100):
    """Select top k features using statistical tests"""
    selector = SelectKBest(score_func=f_classif, k=k)
    X_selected = selector.fit_transform(X, y)
    selected_features = X.columns[selector.get_support()]
    return X_selected, selected_features

def remove_correlated_features(X, threshold=0.95):
    """Remove highly correlated features"""
    corr_matrix = X.corr().abs()
    upper_tri = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1).astype(bool))
    to_drop = [column for column in upper_tri.columns if any(upper_tri[column] > threshold)]
    return X.drop(columns=to_drop)

Best Practices

Preprocessing Pipeline

  1. Text cleaning: Remove noise while preserving meaningful patterns
  2. Normalization: Handle case, Unicode, and encoding issues consistently
  3. Tokenization: Choose appropriate tokenization for your domain
  4. Stop words: Consider domain-specific stop words beyond standard lists
  5. Feature scaling: Normalize features with different scales before ML

Performance Optimization

  • Use sparse matrices for high-dimensional features (TF-IDF, n-grams)
  • Implement incremental learning for large datasets
  • Cache expensive computations (NLP models, readability scores)
  • Parallelize feature extraction across text chunks

Validation Strategies

  • Cross-validate feature selection to avoid overfitting
  • Use stratified sampling for imbalanced text datasets
  • Monitor feature importance in downstream models
  • Test features on held-out data from different time periods or sources

Common Pitfalls to Avoid

  • Data leakage: Ensure features don't contain future information
  • Overfitting: Regularize high-dimensional sparse features
  • Domain shift: Features may not generalize across different text sources
  • Computational complexity: Balance feature richness with processing time
  • Missing values: Handle texts of varying lengths and quality gracefully

Always validate extracted features through exploratory data analysis and correlation studies before feeding them into machine learning models.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.