File size: 24,900 Bytes
676533d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
#!/usr/bin/env python3
"""
Named Entity Recognition (NER) using Transformers
Extracts entities like PERSON, LOCATION, ORGANIZATION from text
"""
from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassification
import argparse
from typing import List, Dict, Any
import json
import os
import logging
# Set up logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
class TransformerNER:
# Predefined model configurations
MODELS = {
"dslim-bert": "dslim/bert-base-NER",
"dbmdz-bert": "dbmdz/bert-large-cased-finetuned-conll03-english",
"xlm-roberta": "xlm-roberta-large-finetuned-conll03-english",
"distilbert": "distilbert-base-cased-distilled-squad"
}
def __init__(self, model_name: str = "dslim/bert-base-NER", aggregation_strategy: str = "simple"):
"""
Initialize NER pipeline with specified model
Default model: dslim/bert-base-NER (lightweight BERT model fine-tuned for NER)
"""
self.logger = logging.getLogger(__name__)
self.current_model_name = model_name
self.cache_dir = os.path.join(os.path.dirname(__file__), "model_cache")
os.makedirs(self.cache_dir, exist_ok=True)
self._load_model(model_name, aggregation_strategy)
def _load_model(self, model_name: str, aggregation_strategy: str = "simple"):
"""Load or reload model with given parameters"""
# Resolve model name if it's a shorthand
if model_name in self.MODELS:
resolved_name = self.MODELS[model_name]
else:
resolved_name = model_name
self.current_model_name = model_name
self.aggregation_strategy = aggregation_strategy
self.logger.info(f"Loading model: {resolved_name}")
self.logger.info(f"Cache directory: {self.cache_dir}")
self.logger.info(f"Aggregation strategy: {aggregation_strategy}")
# Load tokenizer and model with cache directory
self.tokenizer = AutoTokenizer.from_pretrained(resolved_name, cache_dir=self.cache_dir)
self.model = AutoModelForTokenClassification.from_pretrained(resolved_name, cache_dir=self.cache_dir)
self.nlp = pipeline("ner", model=self.model, tokenizer=self.tokenizer, aggregation_strategy=aggregation_strategy)
self.logger.info("Model loaded successfully!")
def switch_model(self, model_name: str, aggregation_strategy: str = None):
"""Switch to a different model dynamically"""
if aggregation_strategy is None:
aggregation_strategy = self.aggregation_strategy
try:
self._load_model(model_name, aggregation_strategy)
return True
except Exception as e:
self.logger.error(f"Failed to load model '{model_name}': {e}")
return False
def change_aggregation(self, aggregation_strategy: str):
"""Change aggregation strategy for current model"""
try:
self._load_model(self.current_model_name, aggregation_strategy)
return True
except Exception as e:
self.logger.error(f"Failed to change aggregation to '{aggregation_strategy}': {e}")
return False
def _post_process_entities(self, entities: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Post-process entities to fix common boundary and classification issues
"""
corrected = []
for entity in entities:
text = entity["text"].strip()
entity_type = entity["entity"]
# Skip empty entities
if not text:
continue
# Fix common misclassifications
corrected_entity = entity.copy()
# Rule 1: Single person names should be PER, not ORG
if entity_type == "ORG" and len(text.split()) == 1:
# Common person names or single words that might be misclassified
if any(text.lower().endswith(suffix) for suffix in ['i', 'a', 'o']) or text.istitle():
corrected_entity["entity"] = "PER"
self.logger.debug(f"Fixed: '{text}' ORG -> PER")
# Rule 2: Known countries should be LOC
countries = ['India', 'China', 'USA', 'UK', 'Germany', 'France', 'Japan']
if text in countries and entity_type != "LOC":
corrected_entity["entity"] = "LOC"
self.logger.debug(f"Fixed: '{text}' {entity_type} -> LOC")
# Rule 3: Split incorrectly merged entities - Updated condition
words = text.split()
if len(words) >= 2 and entity_type == "ORG": # Changed from > 2 to >= 2
# Check if it looks like "PersonName ActionWord"
if words[0].istitle() and words[1].lower() in ['launches', 'announces', 'says', 'opens', 'creates', 'launch']:
# Split into person and skip the action
corrected_entity["text"] = words[0]
corrected_entity["entity"] = "PER"
corrected_entity["end"] = corrected_entity["start"] + len(words[0])
self.logger.info(f"Split entity: '{text}' -> PER: '{words[0]}'")
# Rule 4: Product/technology terms should be MISC
tech_terms = ['electric', 'suv', 'car', 'vehicle', 'app', 'software', 'ai', 'robot', 'global']
if any(term in text.lower() for term in tech_terms):
if entity_type != "MISC":
corrected_entity["entity"] = "MISC"
self.logger.info(f"Fixed: '{text}' {entity_type} -> MISC")
else:
self.logger.debug(f"Already MISC: '{text}'")
corrected.append(corrected_entity)
return corrected
def extract_entities(self, text: str, return_both: bool = False) -> Dict[str, List[Dict[str, Any]]]:
"""
Extract named entities from text
Returns list of entities with their labels, scores, and positions
If return_both=True, returns dict with 'cleaned' and 'corrected' keys
If return_both=False, returns just the corrected entities (backward compatibility)
"""
entities = self.nlp(text)
# Clean up entity groups
cleaned_entities = []
for entity in entities:
cleaned_entities.append({
"entity": entity["entity_group"],
"text": entity["word"],
"score": round(entity["score"], 4),
"start": entity["start"],
"end": entity["end"]
})
# Apply post-processing corrections
corrected_entities = self._post_process_entities(cleaned_entities)
if return_both:
return {
"cleaned": cleaned_entities,
"corrected": corrected_entities
}
else:
return corrected_entities
def extract_entities_debug(self, text: str) -> Dict[str, List[Dict[str, Any]]]:
"""
Extract entities and return both cleaned and corrected versions for debugging
"""
return self.extract_entities(text, return_both=True)
def extract_entities_by_type(self, text: str) -> Dict[str, List[str]]:
"""
Extract entities grouped by type
Returns dictionary with entity types as keys
"""
entities = self.extract_entities(text)
grouped = {}
for entity in entities:
entity_type = entity["entity"]
if entity_type not in grouped:
grouped[entity_type] = []
if entity["text"] not in grouped[entity_type]: # Avoid duplicates
grouped[entity_type].append(entity["text"])
return grouped
def format_output(self, entities: List[Dict[str, Any]], text: str) -> str:
"""
Format entities for display with context
"""
output = []
output.append("=" * 60)
output.append("NAMED ENTITY RECOGNITION RESULTS")
output.append("=" * 60)
output.append(f"\nOriginal Text:\n{text}\n")
output.append("-" * 40)
output.append("Entities Found:")
output.append("-" * 40)
if not entities:
output.append("No entities found.")
else:
for entity in entities:
output.append(f"β’ [{entity['entity']}] '{entity['text']}' (confidence: {entity['score']})")
return "\n".join(output)
def format_debug_output(self, debug_results: Dict[str, List[Dict[str, Any]]], text: str) -> str:
"""
Format debug output showing both cleaned and corrected entities
"""
output = []
output.append("=" * 70)
output.append("NER DEBUG: BEFORE & AFTER POST-PROCESSING")
output.append("=" * 70)
output.append(f"\nOriginal Text:\n{text}\n")
cleaned = debug_results["cleaned"]
corrected = debug_results["corrected"]
# Show raw cleaned entities
output.append("π BEFORE Post-Processing (Raw Model Output):")
output.append("-" * 50)
if not cleaned:
output.append("No entities found by model.")
else:
for entity in cleaned:
output.append(f"β’ [{entity['entity']}] '{entity['text']}' (confidence: {entity['score']})")
output.append("")
# Show corrected entities
output.append("β¨ AFTER Post-Processing (Corrected):")
output.append("-" * 50)
if not corrected:
output.append("No entities after correction.")
else:
for entity in corrected:
output.append(f"β’ [{entity['entity']}] '{entity['text']}' (confidence: {entity['score']})")
# Show differences
output.append("")
output.append("π Changes Made:")
output.append("-" * 25)
changes_found = False
# Create lookup for comparison
cleaned_lookup = {(e['text'], e['entity']) for e in cleaned}
corrected_lookup = {(e['text'], e['entity']) for e in corrected}
# Find what was changed
for corrected_entity in corrected:
corrected_key = (corrected_entity['text'], corrected_entity['entity'])
# Look for original entity with same text but different type
original_entity = None
for cleaned_entity in cleaned:
if (cleaned_entity['text'] == corrected_entity['text'] and
cleaned_entity['entity'] != corrected_entity['entity']):
original_entity = cleaned_entity
break
if original_entity:
output.append(f" Fixed: '{original_entity['text']}' {original_entity['entity']} β {corrected_entity['entity']}")
changes_found = True
# Find split entities (text changed)
for corrected_entity in corrected:
found_exact_match = False
for cleaned_entity in cleaned:
if (cleaned_entity['text'] == corrected_entity['text'] and
cleaned_entity['entity'] == corrected_entity['entity']):
found_exact_match = True
break
if not found_exact_match:
# Look for partial matches (entity splitting)
for cleaned_entity in cleaned:
if (corrected_entity['text'] in cleaned_entity['text'] and
corrected_entity['text'] != cleaned_entity['text']):
output.append(f" Split: '{cleaned_entity['text']}' β '{corrected_entity['text']}'")
changes_found = True
break
if not changes_found:
output.append(" No changes made by post-processing.")
return "\n".join(output)
def interactive_mode(ner: TransformerNER):
"""
Interactive mode that keeps the model loaded and processes multiple texts
"""
print("\n" + "=" * 60)
print("INTERACTIVE NER MODE")
print("=" * 60)
print("Enter text to analyze (or 'quit' to exit)")
print("Commands: 'help' for full list, 'model <name>' to switch models")
print("=" * 60)
grouped_mode = False
json_mode = False
debug_mode = False
def show_help():
print("\n" + "=" * 50)
print("INTERACTIVE COMMANDS")
print("=" * 50)
print("Output Modes:")
print(f" grouped - Toggle grouped output (currently: {'ON' if grouped_mode else 'OFF'})")
print(f" json - Toggle JSON output (currently: {'ON' if json_mode else 'OFF'})")
print(f" debug - Toggle debug mode - show before/after post-processing (currently: {'ON' if debug_mode else 'OFF'})")
print("\nModel Management:")
print(" model <name> - Switch to model (e.g., 'model dbmdz-bert')")
print(" models - List available model shortcuts")
print(" agg <strat> - Change aggregation (simple/first/average/max)")
print("\nFile Operations:")
print(" file <path> - Analyze text from file")
print("\nInformation:")
print(" info - Show current configuration")
print(" help - Show this help")
print(" quit - Exit interactive mode")
print("=" * 50)
def show_models():
print("\nAvailable model shortcuts:")
print("-" * 50)
for shortcut, full_name in TransformerNER.MODELS.items():
current = " (current)" if shortcut == ner.current_model_name or full_name == ner.current_model_name else ""
print(f" {shortcut:<15} -> {full_name}{current}")
print(f"\nUsage: 'model <shortcut>' (e.g., 'model dbmdz-bert')")
print(f"Aggregation strategies: {['simple', 'first', 'average', 'max']}")
print(f"Usage: 'agg <strategy>' (e.g., 'agg first')")
def show_info():
resolved_name = ner.MODELS.get(ner.current_model_name, ner.current_model_name)
print(f"\nCurrent Configuration:")
print(f" Model: {ner.current_model_name}")
print(f" Full name: {resolved_name}")
print(f" Aggregation: {ner.aggregation_strategy}")
print(f" Grouped mode: {'ON' if grouped_mode else 'OFF'}")
print(f" JSON mode: {'ON' if json_mode else 'OFF'}")
print(f" Debug mode: {'ON' if debug_mode else 'OFF'}")
print(f" Cache dir: {ner.cache_dir}")
def switch_model(model_name: str):
print(f"Switching to model: {model_name}")
if ner.switch_model(model_name):
print(f"β
Successfully switched to {model_name}")
return True
else:
print(f"β Failed to switch to {model_name}")
return False
def change_aggregation(strategy: str):
valid_strategies = ["simple", "first", "average", "max"]
if strategy not in valid_strategies:
print(f"β Invalid aggregation strategy. Valid options: {valid_strategies}")
return False
print(f"Changing aggregation to: {strategy}")
if ner.change_aggregation(strategy):
print(f"β
Successfully changed aggregation to {strategy}")
return True
else:
print(f"β Failed to change aggregation to {strategy}")
return False
def process_file(file_path: str):
try:
with open(file_path, 'r', encoding='utf-8') as f:
file_text = f.read()
print(f"π Processing file: {file_path}")
return file_text.strip()
except Exception as e:
print(f"β Error reading file '{file_path}': {e}")
return None
while True:
try:
print("\n> ", end="", flush=True)
user_input = input().strip()
if not user_input:
continue
# Parse command and arguments
parts = user_input.split(None, 1)
command = parts[0].lower()
args = parts[1] if len(parts) > 1 else ""
# Exit commands
if command in ['quit', 'exit', 'q']:
print("Goodbye!")
break
# Toggle commands
elif command == 'grouped':
grouped_mode = not grouped_mode
print(f"Grouped mode: {'ON' if grouped_mode else 'OFF'}")
continue
elif command == 'json':
json_mode = not json_mode
print(f"JSON mode: {'ON' if json_mode else 'OFF'}")
continue
elif command == 'debug':
debug_mode = not debug_mode
print(f"Debug mode: {'ON' if debug_mode else 'OFF'}")
continue
# Information commands
elif command in ['models', 'list-models']:
show_models()
continue
elif command == 'info':
show_info()
continue
elif command == 'help':
show_help()
continue
# Model management commands
elif command == 'model':
if not args:
print("β Please specify a model name. Use 'models' to see available options.")
continue
switch_model(args)
continue
elif command in ['agg', 'aggregation']:
if not args:
print("β Please specify an aggregation strategy: simple, first, average, max")
continue
change_aggregation(args)
continue
# File processing command
elif command == 'file':
if not args:
print("β Please specify a file path.")
continue
file_content = process_file(args)
if file_content:
user_input = file_content
else:
continue
# If we reach here, treat input as text to process
text = user_input if command != 'file' else file_content
# Process the text based on debug mode
if debug_mode:
# Debug mode: show both cleaned and corrected
debug_results = ner.extract_entities_debug(text)
debug_output = ner.format_debug_output(debug_results, text)
print(debug_output)
else:
# Normal mode
if grouped_mode:
entities = ner.extract_entities_by_type(text)
else:
entities = ner.extract_entities(text)
# Output results
if json_mode:
print(json.dumps(entities, indent=2))
elif grouped_mode:
print("\nEntities by type:")
print("-" * 30)
if not entities:
print("No entities found.")
else:
for entity_type, entity_list in entities.items():
print(f"{entity_type}: {', '.join(entity_list)}")
else:
if not entities:
print("No entities found.")
else:
print("\nEntities found:")
print("-" * 20)
for entity in entities:
print(f"β’ [{entity['entity']}] '{entity['text']}' (confidence: {entity['score']})")
except KeyboardInterrupt:
print("\n\nGoodbye!")
break
except EOFError:
print("\nGoodbye!")
break
except Exception as e:
logger.error(f"Error processing text: {e}")
def main():
parser = argparse.ArgumentParser(description="Extract named entities from text using Transformers")
parser.add_argument("--text", type=str, help="Text to analyze")
parser.add_argument("--file", type=str, help="File containing text to analyze")
parser.add_argument("--model", type=str, default="dslim/bert-base-NER",
help="HuggingFace model to use. Shortcuts: dslim-bert, dbmdz-bert, xlm-roberta")
parser.add_argument("--aggregation", type=str, default="simple",
choices=["simple", "first", "average", "max"],
help="Aggregation strategy for subword tokens (default: simple)")
parser.add_argument("--json", action="store_true", help="Output as JSON")
parser.add_argument("--grouped", action="store_true", help="Group entities by type")
parser.add_argument("--interactive", "-i", action="store_true", help="Start interactive mode")
parser.add_argument("--list-models", action="store_true", help="List available model shortcuts")
args = parser.parse_args()
# List available models
if args.list_models:
print("\nAvailable model shortcuts:")
print("-" * 40)
for shortcut, full_name in TransformerNER.MODELS.items():
print(f" {shortcut:<15} -> {full_name}")
print(f"\nDefault aggregation strategies: {['simple', 'first', 'average', 'max']}")
return
# Initialize NER (load model once)
ner = TransformerNER(model_name=args.model, aggregation_strategy=args.aggregation)
# Interactive mode
if args.interactive:
interactive_mode(ner)
return
# Get input text
if args.file:
with open(args.file, 'r') as f:
text = f.read()
elif args.text:
text = args.text
else:
# If no text provided, start interactive mode
interactive_mode(ner)
return
if not text.strip():
logging.error("No text provided")
return
# Extract entities
if args.grouped:
entities = ner.extract_entities_by_type(text)
else:
entities = ner.extract_entities(text)
# Output results
if args.json:
print(json.dumps(entities, indent=2))
elif args.grouped:
print("\n" + "=" * 60)
print("ENTITIES GROUPED BY TYPE")
print("=" * 60)
for entity_type, entity_list in entities.items():
print(f"\n{entity_type}:")
for item in entity_list:
print(f" β’ {item}")
else:
formatted = ner.format_output(entities, text)
print(formatted)
if __name__ == "__main__":
# Example sentences for testing
example_sentences = [
"Apple Inc. was founded by Steve Jobs in Cupertino, California.",
"Barack Obama was the 44th President of the United States.",
"The Eiffel Tower in Paris attracts millions of tourists each year.",
"Google's CEO Sundar Pichai announced new AI features at the conference in San Francisco.",
"Microsoft and OpenAI partnered to develop ChatGPT in Seattle."
]
# If no arguments provided, run demo
import sys
if len(sys.argv) == 1:
# Configure logging for demo
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logging.info("Running demo with example sentences...\n")
ner = TransformerNER()
for sentence in example_sentences:
print("\n" + "="*60)
print(f"Input: {sentence}")
print("-"*40)
entities = ner.extract_entities_by_type(sentence)
for entity_type, items in entities.items():
print(f"{entity_type}: {', '.join(items)}")
print("\n" + "="*60)
print("\nTo analyze your own text, use:")
print(" python ner_transformer.py --text 'Your text here'")
print(" python ner_transformer.py --file input.txt")
print(" python ner_transformer.py --json --grouped")
else:
# Configure logging for main function
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
main()
|