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
multi-language-chatbot
/
modules
RSK World
multi-language-chatbot
Multi-language Chatbot - Python + Flask + OpenAI API + NLP + Translation + Language Detection + Cultural Adaptation
modules
  • __pycache__
  • __init__.py194 B
  • analytics_engine.py28.6 KB
  • chatbot_core.py10.8 KB
  • collaboration_manager.py22.3 KB
  • conversation_memory.py25.2 KB
  • cultural_adapter.py12.3 KB
  • document_analyzer.py21.5 KB
  • language_detector.py5.8 KB
  • multimodal_processor.py32.7 KB
  • personality_engine.py33.6 KB
  • sentiment_analyzer.py16.9 KB
  • translator.py7.5 KB
  • voice_processor.py13.2 KB
document_analyzer.py
modules/document_analyzer.py
Raw Download
Find: Go to:
"""
Document Analysis Module
Author: RSK World (https://rskworld.in)
Founder: Molla Samser
Designer & Tester: Rima Khatun
Contact: help@rskworld.in, +91 93305 39277
Year: 2026
Description: Advanced document analysis with multi-format support and content extraction
"""

import os
import logging
import tempfile
import mimetypes
from typing import Dict, List, Optional, Any, Tuple
import json
import hashlib
from datetime import datetime
import PyPDF2
import docx
import pandas as pd
from PIL import Image
import pytesseract
import cv2
import numpy as np
from io import BytesIO
import chardet
import openai
from langdetect import detect

logger = logging.getLogger(__name__)

class DocumentAnalyzer:
    def __init__(self):
        self.openai_api_key = os.getenv('OPENAI_API_KEY')
        self.supported_formats = {
            'text': ['.txt', '.md', '.csv', '.json', '.xml', '.html', '.htm'],
            'pdf': ['.pdf'],
            'word': ['.doc', '.docx'],
            'excel': ['.xls', '.xlsx'],
            'image': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'],
            'powerpoint': ['.ppt', '.pptx']
        }
        
        # Analysis settings
        self.max_file_size = 50 * 1024 * 1024  # 50MB
        self.ocr_languages = ['eng', 'hin', 'ben', 'spa', 'fra', 'deu', 'chi_sim', 'jpn', 'ara']
        
        # Initialize OCR
        try:
            pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'  # Windows path
        except Exception as e:
            logger.warning(f"Tesseract not found: {str(e)}")
    
    def analyze_document(self, file_path: str, user_id: str = None) -> Dict:
        """
        Analyze document and extract content
        """
        try:
            # Validate file
            if not os.path.exists(file_path):
                return {'error': 'File not found'}
            
            file_size = os.path.getsize(file_path)
            if file_size > self.max_file_size:
                return {'error': f'File too large. Maximum size: {self.max_file_size // (1024*1024)}MB'}
            
            # Get file info
            file_info = self._get_file_info(file_path)
            
            # Extract content based on file type
            content = self._extract_content(file_path, file_info['type'])
            
            if not content:
                return {'error': 'Could not extract content from file'}
            
            # Analyze content
            analysis = self._analyze_content(content, file_info)
            
            # Generate summary
            summary = self._generate_summary(content, file_info['type'])
            
            # Extract key information
            key_info = self._extract_key_information(content, file_info['type'])
            
            # Detect language
            language = self._detect_language(content)
            
            result = {
                'file_info': file_info,
                'content': content,
                'analysis': analysis,
                'summary': summary,
                'key_information': key_info,
                'language': language,
                'timestamp': datetime.now().isoformat(),
                'user_id': user_id
            }
            
            return result
            
        except Exception as e:
            logger.error(f"Document analysis error: {str(e)}")
            return {'error': f'Analysis failed: {str(e)}'}
    
    def analyze_document_from_bytes(self, file_bytes: bytes, filename: str, 
                                  user_id: str = None) -> Dict:
        """
        Analyze document from bytes data
        """
        try:
            # Create temporary file
            with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(filename)[1]) as temp_file:
                temp_file.write(file_bytes)
                temp_file_path = temp_file.name
            
            try:
                # Analyze the temporary file
                result = self.analyze_document(temp_file_path, user_id)
                result['file_info']['original_filename'] = filename
                return result
            finally:
                # Clean up temporary file
                os.unlink(temp_file_path)
                
        except Exception as e:
            logger.error(f"Document analysis from bytes error: {str(e)}")
            return {'error': f'Analysis failed: {str(e)}'}
    
    def _get_file_info(self, file_path: str) -> Dict:
        """Get file information"""
        try:
            file_stat = os.stat(file_path)
            mime_type, _ = mimetypes.guess_type(file_path)
            file_ext = os.path.splitext(file_path)[1].lower()
            
            # Determine file type
            file_type = 'unknown'
            for type_name, extensions in self.supported_formats.items():
                if file_ext in extensions:
                    file_type = type_name
                    break
            
            return {
                'path': file_path,
                'name': os.path.basename(file_path),
                'extension': file_ext,
                'size': file_stat.st_size,
                'mime_type': mime_type,
                'type': file_type,
                'created': datetime.fromtimestamp(file_stat.st_ctime).isoformat(),
                'modified': datetime.fromtimestamp(file_stat.st_mtime).isoformat()
            }
            
        except Exception as e:
            logger.error(f"Error getting file info: {str(e)}")
            return {'type': 'unknown'}
    
    def _extract_content(self, file_path: str, file_type: str) -> Optional[str]:
        """Extract content from file based on type"""
        try:
            if file_type == 'text':
                return self._extract_text_content(file_path)
            elif file_type == 'pdf':
                return self._extract_pdf_content(file_path)
            elif file_type == 'word':
                return self._extract_word_content(file_path)
            elif file_type == 'excel':
                return self._extract_excel_content(file_path)
            elif file_type == 'image':
                return self._extract_image_content(file_path)
            elif file_type == 'powerpoint':
                return self._extract_powerpoint_content(file_path)
            else:
                logger.warning(f"Unsupported file type: {file_type}")
                return None
                
        except Exception as e:
            logger.error(f"Error extracting content: {str(e)}")
            return None
    
    def _extract_text_content(self, file_path: str) -> str:
        """Extract content from text files"""
        try:
            # Detect encoding
            with open(file_path, 'rb') as file:
                raw_data = file.read()
                encoding_result = chardet.detect(raw_data)
                encoding = encoding_result['encoding']
            
            # Read with detected encoding
            with open(file_path, 'r', encoding=encoding) as file:
                return file.read()
                
        except Exception as e:
            logger.error(f"Error extracting text content: {str(e)}")
            return ""
    
    def _extract_pdf_content(self, file_path: str) -> str:
        """Extract content from PDF files"""
        try:
            content = []
            with open(file_path, 'rb') as file:
                pdf_reader = PyPDF2.PdfReader(file)
                
                for page_num in range(len(pdf_reader.pages)):
                    page = pdf_reader.pages[page_num]
                    page_text = page.extract_text()
                    content.append(f"Page {page_num + 1}:\n{page_text}")
            
            return '\n\n'.join(content)
            
        except Exception as e:
            logger.error(f"Error extracting PDF content: {str(e)}")
            return ""
    
    def _extract_word_content(self, file_path: str) -> str:
        """Extract content from Word documents"""
        try:
            doc = docx.Document(file_path)
            content = []
            
            for paragraph in doc.paragraphs:
                if paragraph.text.strip():
                    content.append(paragraph.text)
            
            # Extract tables
            for table in doc.tables:
                table_content = []
                for row in table.rows:
                    row_content = []
                    for cell in row.cells:
                        row_content.append(cell.text.strip())
                    table_content.append(' | '.join(row_content))
                content.append('Table:\n' + '\n'.join(table_content))
            
            return '\n\n'.join(content)
            
        except Exception as e:
            logger.error(f"Error extracting Word content: {str(e)}")
            return ""
    
    def _extract_excel_content(self, file_path: str) -> str:
        """Extract content from Excel files"""
        try:
            content = []
            
            # Read all sheets
            excel_file = pd.ExcelFile(file_path)
            
            for sheet_name in excel_file.sheet_names:
                df = pd.read_excel(file_path, sheet_name=sheet_name)
                
                content.append(f"Sheet: {sheet_name}")
                content.append(df.to_string(index=False))
                content.append("")  # Empty line between sheets
            
            return '\n'.join(content)
            
        except Exception as e:
            logger.error(f"Error extracting Excel content: {str(e)}")
            return ""
    
    def _extract_image_content(self, file_path: str) -> str:
        """Extract content from images using OCR"""
        try:
            # Open image
            image = Image.open(file_path)
            
            # Preprocess image for better OCR
            image = self._preprocess_image(image)
            
            # Perform OCR
            text = pytesseract.image_to_string(image, lang='eng+hin+ben')
            
            return text.strip()
            
        except Exception as e:
            logger.error(f"Error extracting image content: {str(e)}")
            return ""
    
    def _extract_powerpoint_content(self, file_path: str) -> str:
        """Extract content from PowerPoint presentations"""
        try:
            # This would require python-pptx library
            # For now, return placeholder
            return "PowerPoint content extraction requires additional libraries."
            
        except Exception as e:
            logger.error(f"Error extracting PowerPoint content: {str(e)}")
            return ""
    
    def _preprocess_image(self, image: Image.Image) -> Image.Image:
        """Preprocess image for better OCR"""
        try:
            # Convert to grayscale
            if image.mode != 'L':
                image = image.convert('L')
            
            # Convert to numpy array
            img_array = np.array(image)
            
            # Apply threshold
            _, img_array = cv2.threshold(img_array, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
            
            # Convert back to PIL Image
            return Image.fromarray(img_array)
            
        except Exception as e:
            logger.error(f"Error preprocessing image: {str(e)}")
            return image
    
    def _analyze_content(self, content: str, file_type: str) -> Dict:
        """Analyze extracted content"""
        try:
            analysis = {
                'word_count': len(content.split()),
                'character_count': len(content),
                'line_count': len(content.split('\n')),
                'paragraph_count': len([p for p in content.split('\n\n') if p.strip()]),
                'file_type': file_type
            }
            
            # Advanced analysis based on content type
            if file_type in ['text', 'pdf', 'word']:
                analysis.update(self._analyze_text_content(content))
            elif file_type == 'excel':
                analysis.update(self._analyze_data_content(content))
            
            return analysis
            
        except Exception as e:
            logger.error(f"Error analyzing content: {str(e)}")
            return {}
    
    def _analyze_text_content(self, content: str) -> Dict:
        """Analyze text content"""
        try:
            # Basic statistics
            sentences = content.split('.')
            questions = [s for s in sentences if '?' in s]
            exclamations = [s for s in sentences if '!' in s]
            
            # Find common words
            words = content.lower().split()
            word_freq = {}
            for word in words:
                word = word.strip('.,!?;:"\'()[]{}')
                if len(word) > 3:
                    word_freq[word] = word_freq.get(word, 0) + 1
            
            # Get top words
            top_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:10]
            
            return {
                'sentence_count': len(sentences),
                'question_count': len(questions),
                'exclamation_count': len(exclamations),
                'top_words': top_words,
                'avg_sentence_length': sum(len(s.split()) for s in sentences) / max(len(sentences), 1)
            }
            
        except Exception as e:
            logger.error(f"Error analyzing text content: {str(e)}")
            return {}
    
    def _analyze_data_content(self, content: str) -> Dict:
        """Analyze data content (Excel/CSV)"""
        try:
            lines = content.split('\n')
            data_rows = [line for line in lines if '|' in line or '\t' in line]
            
            return {
                'data_rows': len(data_rows),
                'estimated_columns': len(data_rows[0].split('|')) if data_rows else 0,
                'has_headers': any(':' in row for row in data_rows[:3]) if data_rows else False
            }
            
        except Exception as e:
            logger.error(f"Error analyzing data content: {str(e)}")
            return {}
    
    def _generate_summary(self, content: str, file_type: str) -> str:
        """Generate summary of content"""
        try:
            # Truncate content if too long
            max_length = 2000
            if len(content) > max_length:
                content = content[:max_length] + "..."
            
            # Use OpenAI for advanced summary if available
            if self.openai_api_key:
                return self._generate_ai_summary(content, file_type)
            else:
                return self._generate_basic_summary(content, file_type)
                
        except Exception as e:
            logger.error(f"Error generating summary: {str(e)}")
            return "Summary generation failed."
    
    def _generate_ai_summary(self, content: str, file_type: str) -> str:
        """Generate AI-powered summary"""
        try:
            prompt = f"""
            Summarize the following {file_type} content in a concise and informative way:
            
            Content: {content}
            
            Provide a summary that includes:
            1. Main topics/themes
            2. Key points
            3. Important details
            4. Overall purpose/meaning
            
            Keep the summary under 200 words.
            """
            
            response = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": "You are an expert at summarizing documents of various formats."},
                    {"role": "user", "content": prompt}
                ],
                max_tokens=300,
                temperature=0.3
            )
            
            return response.choices[0].message.content.strip()
            
        except Exception as e:
            logger.error(f"Error generating AI summary: {str(e)}")
            return self._generate_basic_summary(content, file_type)
    
    def _generate_basic_summary(self, content: str, file_type: str) -> str:
        """Generate basic summary without AI"""
        try:
            word_count = len(content.split())
            
            if file_type == 'image':
                return f"Image contains {word_count} words of extracted text."
            
            # Get first few sentences
            sentences = content.split('.')[:3]
            preview = '. '.join(sentences).strip()
            
            if len(preview) > 200:
                preview = preview[:200] + "..."
            
            return f"Document contains {word_count} words. Preview: {preview}"
            
        except Exception as e:
            logger.error(f"Error generating basic summary: {str(e)}")
            return "Unable to generate summary."
    
    def _extract_key_information(self, content: str, file_type: str) -> Dict:
        """Extract key information from content"""
        try:
            key_info = {}
            
            # Extract dates
            import re
            date_pattern = r'\d{1,2}[/-]\d{1,2}[/-]\d{2,4}|\d{4}[/-]\d{1,2}[/-]\d{1,2}'
            dates = re.findall(date_pattern, content)
            if dates:
                key_info['dates'] = list(set(dates))
            
            # Extract emails
            email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
            emails = re.findall(email_pattern, content)
            if emails:
                key_info['emails'] = list(set(emails))
            
            # Extract phone numbers
            phone_pattern = r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b|\+\d{1,3}[-.]?\d{3}[-.]?\d{3}[-.]?\d{4}\b'
            phones = re.findall(phone_pattern, content)
            if phones:
                key_info['phone_numbers'] = list(set(phones))
            
            # Extract URLs
            url_pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
            urls = re.findall(url_pattern, content)
            if urls:
                key_info['urls'] = list(set(urls))
            
            # Extract numbers/amounts
            number_pattern = r'\$?\d{1,3}(?:,\d{3})*(?:\.\d{2})?'
            numbers = re.findall(number_pattern, content)
            if numbers:
                key_info['numbers'] = numbers[:10]  # Limit to first 10
            
            return key_info
            
        except Exception as e:
            logger.error(f"Error extracting key information: {str(e)}")
            return {}
    
    def _detect_language(self, content: str) -> str:
        """Detect language of content"""
        try:
            if len(content) < 50:
                return 'en'  # Default for short content
            
            return detect(content)
            
        except Exception as e:
            logger.error(f"Error detecting language: {str(e)}")
            return 'en'
    
    def get_supported_formats(self) -> Dict[str, List[str]]:
        """Get list of supported file formats"""
        return self.supported_formats.copy()
    
    def is_supported_format(self, filename: str) -> bool:
        """Check if file format is supported"""
        file_ext = os.path.splitext(filename)[1].lower()
        
        for extensions in self.supported_formats.values():
            if file_ext in extensions:
                return True
        
        return False
    
    def batch_analyze(self, file_paths: List[str], user_id: str = None) -> List[Dict]:
        """Analyze multiple documents"""
        results = []
        
        for file_path in file_paths:
            result = self.analyze_document(file_path, user_id)
            results.append(result)
        
        return results
    
    def search_in_documents(self, query: str, file_paths: List[str]) -> List[Dict]:
        """Search for query in multiple documents"""
        search_results = []
        
        for file_path in file_paths:
            try:
                content = self._extract_content(file_path, self._get_file_info(file_path)['type'])
                
                if content and query.lower() in content.lower():
                    # Find context around the query
                    lines = content.split('\n')
                    matching_lines = []
                    
                    for i, line in enumerate(lines):
                        if query.lower() in line.lower():
                            context_start = max(0, i - 2)
                            context_end = min(len(lines), i + 3)
                            context = '\n'.join(lines[context_start:context_end])
                            matching_lines.append({
                                'line_number': i + 1,
                                'context': context
                            })
                    
                    search_results.append({
                        'file_path': file_path,
                        'file_name': os.path.basename(file_path),
                        'matches': matching_lines,
                        'match_count': len(matching_lines)
                    })
                    
            except Exception as e:
                logger.error(f"Error searching in {file_path}: {str(e)}")
        
        return search_results
567 lines•21.5 KB
python
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

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