An intelligent chatbot system for automated information extraction from text messages using regular expressions and genetic algorithms.
This project was developed as part of the Natural Language Processing course (Spring 2024) at Sharif University of Technology. It implements a sophisticated information extraction system that automatically categorizes and extracts structured data from chat messages in Persian and English.
- Overview
- Features
- Architecture
- Technologies
- Installation
- Usage
- Project Structure
- Documentation
- Contributors
- License
This system combines multiple NLP techniques to extract meaningful information from unstructured text:
- Regex-based Information Extraction: Identifies emails, phone numbers, addresses, and other structured data
- Genetic Algorithm for Regex Generation: Automatically generates optimized regex patterns from example text
- Custom Pattern Management: User-defined regex patterns stored in SQLite database
- Matrix Chatbot Integration: Interactive bot interface using the Opsdroid framework
- π§ Email Extraction: Detects and extracts email addresses from text
- π± Phone Number Recognition: Identifies mobile and landline numbers (supports Persian/Arabic numerals)
- π Address Extraction: Recognizes Persian addresses using entity-based pattern matching
- π€ Automated Regex Generation: Creates optimal regex patterns using genetic algorithms
- πΎ Custom Pattern Storage: Manages user-defined patterns in a SQLite database
- π Multi-language Support: Handles both Persian and English text
- π¬ Interactive Chatbot: Matrix protocol integration for real-time interaction
- Population-based evolution (configurable generations and population size)
- 21 distinct gene types representing regex building blocks
- Fitness-based selection and optimization
- Returns top-3 best-performing patterns
βββββββββββββββββββββββββββββββββββββββββββββββ
β Opsdroid Chatbot Layer β
β (Matrix Protocol, Command Handlers) β
ββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
ββββββββββ΄βββββββββ
β β
βββββΌβββββ ββββββββΌββββββ ββββββββββββ
β Info β β Regex β β New β
β Module β β Generator β β Regex β
β β β β β Module β
β Extractβ β Genetic β β Database β
β Contactβ β Algorithm β β Patterns β
β Info β β β β β
ββββββββββ ββββββββββββββ ββββββββββββ
Generates optimized regex patterns through evolutionary algorithms:
- Genes: 21 types (digits, letters, spaces, character ranges, etc.)
- Evolution: Population-based selection over multiple generations
- Output: Top-3 regex patterns ranked by fitness score
Specialized extractors for structured data:
number.py: Mobile/landline phone number extractionaddress.py: Persian address recognition using entity keywordsextractor.py: Email extraction and message classification
SQLite-based management of custom regex patterns:
- Store user-defined patterns with unique names
- Test messages against all stored patterns
- Validate custom regex on text
Opsdroid bot command handlers:
/generate- Generate regex from examples/info- Extract contact information/add- Store custom pattern/checkall- Test against stored patterns/checkone- Validate custom regex/help- Display available commands
- Python 3.8+ - Core programming language
- Opsdroid - Chatbot framework with Matrix protocol support
- SQLAlchemy 2.0.30 - ORM for database management
- Regular Expressions - Pattern matching and text extraction
- Genetic Algorithms - Automated regex optimization
- Matrix Protocol - Decentralized communication
- SQLite - Lightweight pattern database
- Python 3.8 or higher
- pip package manager
- Matrix account (for chatbot functionality)
-
Clone the repository
git clone https://github.com/NLP-1403/HW2.git cd HW2 -
Install dependencies
pip install -r new_regex/requirements.txt pip install opsdroid
-
Configure the bot
Edit
configuration.yamland update the Matrix credentials:connectors: matrix: mxid: '@your_bot:matrix.server' password: 'your_password' homeserver: 'https://matrix.server'
-
Initialize the database
python new_regex/main.py
-
Run the chatbot
opsdroid start
from info.extractor import Extractor
text = ["Ψ§ΫΩ
ΫΩ Ω
Ω test@example.com Ψ§Ψ³Ψͺ Ω Ψ΄Ω
Ψ§Ψ±Ω ΨͺΩΩΩ Ω
Ω 09123456789 Ψ§Ψ³Ψͺ"]
results = Extractor(text, char_threshold=100)
print(results)from regex_generator.generator import generator
examples = ["test123", "demo456", "sample789"]
patterns = generator(examples, population=100, generation=10)
print(patterns) # Returns top-3 regex patternsfrom new_regex.main import add_regex, check_message_patterns
add_regex("postal_code", r"\d{10}")
matches = check_message_patterns("Ϊ©Ψ― ΩΎΨ³ΨͺΫ: 1234567890")Once the bot is running, interact via Matrix chat:
/generate 100 10 test@email.com test2@email.com
β Generates regex patterns from examples
/info 50 My email is test@example.com
β Extracts emails, phone numbers, addresses
/add postal_code \d{10}
β Stores custom regex pattern
/checkall Test message 1234567890
β Tests against all stored patterns
/help
β Shows all available commands
HW2/
βββ configuration.yaml # Opsdroid bot configuration
βββ NLP_Spring1403_HW2.pdf # Assignment specification
βββ LICENSE # MIT License
βββ README.md # This file
β
βββ info/ # Information extraction module
β βββ AddressEntities.txt # Persian address entity keywords
β βββ AreaCodes.txt # Iranian area codes
β βββ address.py # Address extraction
β βββ number.py # Phone number extraction
β βββ extractor.py # Main extraction orchestrator
β
βββ regex_generator/ # Genetic algorithm engine
β βββ const.py # Gene type definitions
β βββ genetic.py # Genetic algorithm core (21 gene types)
β βββ generator.py # Main regex generator
β βββ parser.py # Gene-to-regex conversion
β βββ decoder.py # Pattern decoder
β βββ formatter.py # Output formatter
β βββ evalute.py # Fitness evaluation
β βββ utils.py # Utility functions
β
βββ new_regex/ # Custom pattern database
β βββ main.py # Database operations
β βββ requirements.txt # SQLAlchemy dependencies
β βββ message_patterns.db # SQLite database
β
βββ skills/ # Opsdroid bot commands
β βββ accept_invite.py # Auto-accept invites
β βββ join_room.py # Join Matrix rooms
β βββ help.py # Help command
β βββ regex_generator.py # /generate command
β βββ info.py # /info command
β βββ add_regex.py # /add command
β βββ check_with_new_regexes.py # /checkall command
β βββ check_message_with_regex.py # /checkone command
β
βββ document/ # LaTeX report
βββ Report.tex # Technical documentation
βββ solutionclass.cls # LaTeX template
βββ img/ # Report images
- Detailed Report: See the PDF assignment specification for project requirements
- Technical Documentation: See
document/Report.texfor implementation details - Code Comments: Inline documentation in Python modules
Genetic Algorithm Genes (21 types):
0x00:\d(digits)0x01:[A-Z](uppercase)0x02:[a-z](lowercase)0x03:[A-Za-z](letters)0x06:\w(word characters)0x07:\s(whitespace)0x09:.(any character)0x0c-0x14: Character ranges and column-based patterns
This project was developed by:
- Ilia Hashemi Rad - Student ID: 99102456
- AmirMohammad Fakhimi - Student ID: 99170531
- AmirMahdi Namjoo - Student ID: 97107212
Course: Natural Language Processing (Spring 2024)
Institution: Sharif University of Technology
Department: Computer Engineering
This project is licensed under the MIT License - see the LICENSE file for details.
- Sharif University of Technology NLP Course Staff
- Opsdroid Framework Community
- Matrix Protocol Contributors
Note: This is an educational project developed for academic purposes. The genetic algorithm implementation demonstrates automated regex pattern generation, while the information extraction showcases practical NLP applications for Persian and English text.