help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
named-entity-recognition
/
scripts
RSK World
named-entity-recognition
Named Entity Recognition Dataset - NER + Information Extraction + BIO Format + NLP
scripts
  • __pycache__
  • advanced_stats.py10.5 KB
  • api_server.py9.2 KB
  • batch_process.py8.1 KB
  • evaluate_model.py9 KB
  • export_data.py9 KB
  • load_dataset.py4.3 KB
  • train_model.py5.8 KB
  • visualize_ner.py6 KB
advanced_features.pycode_examples.jsonvisualize_ner.py__init__.pyevaluate_model.py
scripts/visualize_ner.py
Raw Download
Find: Go to:
"""
Named Entity Recognition Dataset - Visualization Script
Project: Named Entity Recognition Dataset
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Description: Script to visualize named entities in text using spaCy and matplotlib
"""

import json
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from collections import Counter
import os


def load_dataset(file_path: str):
    """Load NER dataset from JSON file."""
    with open(file_path, 'r', encoding='utf-8') as f:
        return json.load(f)


def visualize_entity_distribution(data, save_path='entity_distribution.png'):
    """
    Create a bar chart showing the distribution of entity types.
    
    Args:
        data: List of data samples with entities
        save_path: Path to save the visualization
    """
    entity_counts = Counter()
    for sample in data:
        for entity in sample.get('entities', []):
            entity_counts[entity['label']] += 1
    
    labels = list(entity_counts.keys())
    counts = list(entity_counts.values())
    
    plt.figure(figsize=(10, 6))
    plt.bar(labels, counts, color=['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8', '#F7DC6F'])
    plt.xlabel('Entity Type', fontsize=12)
    plt.ylabel('Count', fontsize=12)
    plt.title('Distribution of Named Entity Types', fontsize=14, fontweight='bold')
    plt.xticks(rotation=45, ha='right')
    plt.tight_layout()
    plt.grid(axis='y', alpha=0.3)
    plt.savefig(save_path, dpi=300, bbox_inches='tight')
    print(f"Visualization saved to {save_path}")
    plt.close()


def highlight_entities_html(text: str, entities: list) -> str:
    """
    Create HTML with highlighted entities.
    
    Args:
        text: Original text
        entities: List of entity dictionaries with start, end, label, text
        
    Returns:
        HTML string with highlighted entities
    """
    # Sort entities by start position (reverse to avoid index shifting)
    sorted_entities = sorted(entities, key=lambda x: x['start'], reverse=True)
    
    colors = {
        'PERSON': '#FF6B6B',
        'ORG': '#4ECDC4',
        'LOC': '#45B7D1',
        'DATE': '#FFA07A',
        'MONEY': '#98D8C8',
        'PERCENT': '#F7DC6F'
    }
    
    html_text = text
    for entity in sorted_entities:
        start = entity['start']
        end = entity['end']
        label = entity['label']
        color = colors.get(label, '#CCCCCC')
        
        entity_text = html_text[start:end]
        highlighted = f'<span style="background-color: {color}; padding: 2px 4px; border-radius: 3px; font-weight: bold;" title="{label}">{entity_text}</span>'
        html_text = html_text[:start] + highlighted + html_text[end:]
    
    return html_text


def create_visualization_report(data, output_file='ner_visualization.html'):
    """
    Create an HTML report with highlighted entities.
    
    Args:
        data: List of data samples with entities
        output_file: Path to save the HTML report
    """
    html_content = """
    <!DOCTYPE html>
    <html>
    <head>
        <title>NER Dataset Visualization</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                max-width: 1200px;
                margin: 0 auto;
                padding: 20px;
                background-color: #f5f5f5;
            }
            .sample {
                background: white;
                padding: 20px;
                margin: 20px 0;
                border-radius: 8px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .legend {
                background: white;
                padding: 15px;
                margin: 20px 0;
                border-radius: 8px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .legend-item {
                display: inline-block;
                margin: 5px 10px;
                padding: 5px 10px;
                border-radius: 3px;
                font-weight: bold;
            }
            h1 {
                color: #333;
            }
            .text {
                line-height: 1.8;
                font-size: 16px;
            }
        </style>
    </head>
    <body>
        <h1>Named Entity Recognition Dataset Visualization</h1>
        <div class="legend">
            <h3>Entity Types:</h3>
            <span class="legend-item" style="background-color: #FF6B6B;">PERSON</span>
            <span class="legend-item" style="background-color: #4ECDC4;">ORG</span>
            <span class="legend-item" style="background-color: #45B7D1;">LOC</span>
            <span class="legend-item" style="background-color: #FFA07A;">DATE</span>
            <span class="legend-item" style="background-color: #98D8C8;">MONEY</span>
            <span class="legend-item" style="background-color: #F7DC6F;">PERCENT</span>
        </div>
    """
    
    for i, sample in enumerate(data[:20], 1):  # Show first 20 samples
        highlighted_text = highlight_entities_html(sample['text'], sample['entities'])
        html_content += f"""
        <div class="sample">
            <h3>Sample {i}</h3>
            <div class="text">{highlighted_text}</div>
        </div>
        """
    
    html_content += """
    </body>
    </html>
    """
    
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(html_content)
    
    print(f"Visualization report saved to {output_file}")


if __name__ == '__main__':
    train_path = '../dataset/train.json'
    
    if os.path.exists(train_path):
        print("Loading dataset for visualization...")
        data = load_dataset(train_path)
        
        print("Creating entity distribution chart...")
        visualize_entity_distribution(data)
        
        print("Creating HTML visualization report...")
        create_visualization_report(data)
        
        print("\nVisualization complete!")

189 lines•6 KB
python
scripts/evaluate_model.py
Raw Download
Find: Go to:
"""
Named Entity Recognition Dataset - Model Evaluation Script
Project: Named Entity Recognition Dataset
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Description: Advanced model evaluation and comparison tools
"""

import json
import os
from typing import List, Dict, Any, Tuple
from collections import defaultdict
import numpy as np


def load_dataset(file_path: str) -> List[Dict[str, Any]]:
    """Load NER dataset from JSON file."""
    with open(file_path, 'r', encoding='utf-8') as f:
        return json.load(f)


def extract_entities_from_sample(sample: Dict[str, Any]) -> List[Tuple[int, int, str]]:
    """Extract entities as (start, end, label) tuples."""
    entities = []
    for entity in sample.get('entities', []):
        entities.append((
            entity.get('start', 0),
            entity.get('end', 0),
            entity.get('label', '')
        ))
    return entities


def calculate_metrics(true_entities: List[Tuple[int, int, str]], 
                      pred_entities: List[Tuple[int, int, str]]) -> Dict[str, float]:
    """
    Calculate precision, recall, and F1 score.
    
    Args:
        true_entities: List of (start, end, label) tuples for ground truth
        pred_entities: List of (start, end, label) tuples for predictions
        
    Returns:
        Dictionary with precision, recall, and F1 score
    """
    true_set = set(true_entities)
    pred_set = set(pred_entities)
    
    # True Positives: entities that are in both sets
    tp = len(true_set & pred_set)
    
    # False Positives: predicted entities not in true set
    fp = len(pred_set - true_set)
    
    # False Negatives: true entities not in predicted set
    fn = len(true_set - pred_set)
    
    # Calculate metrics
    precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0
    recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0
    f1 = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0.0
    
    return {
        'precision': precision,
        'recall': recall,
        'f1': f1,
        'tp': tp,
        'fp': fp,
        'fn': fn
    }


def evaluate_model_predictions(true_data: List[Dict[str, Any]], 
                              pred_data: List[Dict[str, Any]]) -> Dict[str, Any]:
    """
    Evaluate model predictions against ground truth.
    
    Args:
        true_data: Ground truth data
        pred_data: Predicted data (same structure as true_data)
        
    Returns:
        Dictionary with evaluation metrics
    """
    if len(true_data) != len(pred_data):
        raise ValueError("True and predicted data must have the same length")
    
    overall_metrics = defaultdict(lambda: {'tp': 0, 'fp': 0, 'fn': 0})
    per_entity_metrics = defaultdict(lambda: {'tp': 0, 'fp': 0, 'fn': 0})
    sample_metrics = []
    
    for true_sample, pred_sample in zip(true_data, pred_data):
        true_entities = extract_entities_from_sample(true_sample)
        pred_entities = extract_entities_from_sample(pred_sample)
        
        # Overall metrics
        true_set = set(true_entities)
        pred_set = set(pred_entities)
        
        overall_metrics['all']['tp'] += len(true_set & pred_set)
        overall_metrics['all']['fp'] += len(pred_set - true_set)
        overall_metrics['all']['fn'] += len(true_set - pred_set)
        
        # Per-entity-type metrics
        true_by_type = defaultdict(set)
        pred_by_type = defaultdict(set)
        
        for start, end, label in true_entities:
            true_by_type[label].add((start, end, label))
        
        for start, end, label in pred_entities:
            pred_by_type[label].add((start, end, label))
        
        all_types = set(true_by_type.keys()) | set(pred_by_type.keys())
        for entity_type in all_types:
            true_type_set = true_by_type[entity_type]
            pred_type_set = pred_by_type[entity_type]
            
            per_entity_metrics[entity_type]['tp'] += len(true_type_set & pred_type_set)
            per_entity_metrics[entity_type]['fp'] += len(pred_type_set - true_type_set)
            per_entity_metrics[entity_type]['fn'] += len(true_type_set - pred_type_set)
        
        # Sample-level metrics
        sample_metrics.append(calculate_metrics(true_entities, pred_entities))
    
    # Calculate overall metrics
    overall = overall_metrics['all']
    overall_precision = overall['tp'] / (overall['tp'] + overall['fp']) if (overall['tp'] + overall['fp']) > 0 else 0.0
    overall_recall = overall['tp'] / (overall['tp'] + overall['fn']) if (overall['tp'] + overall['fn']) > 0 else 0.0
    overall_f1 = 2 * (overall_precision * overall_recall) / (overall_precision + overall_recall) if (overall_precision + overall_recall) > 0 else 0.0
    
    # Calculate per-entity metrics
    per_entity_results = {}
    for entity_type, metrics in per_entity_metrics.items():
        precision = metrics['tp'] / (metrics['tp'] + metrics['fp']) if (metrics['tp'] + metrics['fp']) > 0 else 0.0
        recall = metrics['tp'] / (metrics['tp'] + metrics['fn']) if (metrics['tp'] + metrics['fn']) > 0 else 0.0
        f1 = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0.0
        
        per_entity_results[entity_type] = {
            'precision': precision,
            'recall': recall,
            'f1': f1,
            'tp': metrics['tp'],
            'fp': metrics['fp'],
            'fn': metrics['fn']
        }
    
    # Sample-level statistics
    avg_precision = np.mean([m['precision'] for m in sample_metrics])
    avg_recall = np.mean([m['recall'] for m in sample_metrics])
    avg_f1 = np.mean([m['f1'] for m in sample_metrics])
    
    return {
        'overall': {
            'precision': overall_precision,
            'recall': overall_recall,
            'f1': overall_f1,
            'tp': overall['tp'],
            'fp': overall['fp'],
            'fn': overall['fn']
        },
        'per_entity': per_entity_results,
        'sample_level': {
            'avg_precision': avg_precision,
            'avg_recall': avg_recall,
            'avg_f1': avg_f1,
            'std_precision': np.std([m['precision'] for m in sample_metrics]),
            'std_recall': np.std([m['recall'] for m in sample_metrics]),
            'std_f1': np.std([m['f1'] for m in sample_metrics])
        }
    }


def print_evaluation_report(results: Dict[str, Any]):
    """Print formatted evaluation report."""
    print("\n" + "="*70)
    print("MODEL EVALUATION REPORT")
    print("="*70)
    
    print("\n📊 OVERALL METRICS")
    print("-" * 70)
    overall = results['overall']
    print(f"  Precision: {overall['precision']:.4f}")
    print(f"  Recall:    {overall['recall']:.4f}")
    print(f"  F1 Score:  {overall['f1']:.4f}")
    print(f"\n  True Positives:  {overall['tp']}")
    print(f"  False Positives: {overall['fp']}")
    print(f"  False Negatives: {overall['fn']}")
    
    print("\n🏷️  PER-ENTITY METRICS")
    print("-" * 70)
    print(f"{'Entity Type':<15} {'Precision':<12} {'Recall':<12} {'F1 Score':<12}")
    print("-" * 70)
    for entity_type, metrics in sorted(results['per_entity'].items()):
        print(f"{entity_type:<15} {metrics['precision']:<12.4f} {metrics['recall']:<12.4f} {metrics['f1']:<12.4f}")
    
    print("\n📈 SAMPLE-LEVEL STATISTICS")
    print("-" * 70)
    sample = results['sample_level']
    print(f"  Average Precision: {sample['avg_precision']:.4f} (±{sample['std_precision']:.4f})")
    print(f"  Average Recall:    {sample['avg_recall']:.4f} (±{sample['std_recall']:.4f})")
    print(f"  Average F1 Score:  {sample['avg_f1']:.4f} (±{sample['std_f1']:.4f})")
    
    print("\n" + "="*70 + "\n")


def export_evaluation_results(results: Dict[str, Any], output_file: str):
    """Export evaluation results to JSON file."""
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(results, f, indent=2, ensure_ascii=False)
    print(f"Evaluation results exported to {output_file}")


if __name__ == '__main__':
    import argparse
    
    parser = argparse.ArgumentParser(description='Evaluate NER model predictions')
    parser.add_argument('--true', type=str, required=True, help='Ground truth JSON file')
    parser.add_argument('--pred', type=str, required=True, help='Predictions JSON file')
    parser.add_argument('--output', type=str, default='evaluation_results.json', help='Output file')
    
    args = parser.parse_args()
    
    print("Loading datasets...")
    true_data = load_dataset(args.true)
    pred_data = load_dataset(args.pred)
    
    print(f"Loaded {len(true_data)} ground truth samples")
    print(f"Loaded {len(pred_data)} prediction samples")
    
    print("\nEvaluating model...")
    results = evaluate_model_predictions(true_data, pred_data)
    
    print_evaluation_report(results)
    export_evaluation_results(results, args.output)
    
    print("Evaluation complete!")

241 lines•9 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer