File size: 5,175 Bytes
62a43ef
 
 
 
 
 
 
 
 
 
 
 
 
cfb6914
62a43ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfb6914
62a43ef
 
 
 
cfb6914
62a43ef
cfb6914
 
 
 
 
62a43ef
 
cfb6914
62a43ef
 
 
 
cfb6914
62a43ef
 
 
 
 
 
cfb6914
62a43ef
 
 
 
cfb6914
 
62a43ef
cfb6914
62a43ef
 
 
 
 
cfb6914
62a43ef
 
cfb6914
62a43ef
 
8118598
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
import os
import re
import io
import tempfile
from typing import Dict, List, Any, Union, Tuple, Optional
import pandas as pd
import numpy as np
from datetime import datetime

# المكتبات الخاصة بمعالجة أنواع المستندات المختلفة
import docx
import PyPDF2
import fitz  # PyMuPDF
import pdfplumber
from openpyxl import load_workbook
from PIL import Image
import pytesseract

# المكتبات الخاصة بالمعالجة الطبيعية للغة
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
from nltk import ngrams

# تحميل الموارد اللازمة للغة العربية
try:
    nltk.data.find('tokenizers/punkt')
    nltk.data.find('corpora/stopwords')
except LookupError:
    nltk.download('punkt')
    nltk.download('stopwords')

class DocumentProcessor:
    """
    فئة لمعالجة المستندات المختلفة وتحليلها واستخراج المعلومات منها
    تدعم الملفات بصيغة PDF, DOCX, XLSX, CSV, TXT
    """
    
    def __init__(self):
        """
        تهيئة معالج المستندات
        """
        # تحميل قائمة الكلمات الدلالية للمناقصات
        self.tender_keywords = self._load_tender_keywords()
        
        # تحميل قائمة المتطلبات الشائعة
        self.common_requirements = self._load_common_requirements()
        
        # الكلمات التوقفية في اللغة العربية
        self.arabic_stopwords = set(stopwords.words('arabic'))
        
    def process_document(self, file_content: bytes, file_extension: str, file_name: str) -> Dict[str, Any]:
        """
        معالجة المستند وتحليله حسب نوعه
        """
        with tempfile.NamedTemporaryFile(suffix=f".{file_extension}", delete=False) as temp_file:
            temp_file.write(file_content)
            temp_path = temp_file.name
        
        try:
            if file_extension.lower() == 'pdf':
                extracted_data = self._process_pdf(temp_path)
            elif file_extension.lower() in ['docx', 'doc']:
                extracted_data = self._process_docx(temp_path)
            elif file_extension.lower() in ['xlsx', 'xls']:
                extracted_data = self._process_excel(temp_path)
            elif file_extension.lower() == 'csv':
                extracted_data = self._process_csv(temp_path)
            elif file_extension.lower() == 'txt':
                extracted_data = self._process_txt(temp_path)
            else:
                extracted_data = {"error": f"نوع الملف {file_extension} غير مدعوم"}
            
            extracted_data["file_name"] = file_name
            extracted_data["file_type"] = file_extension
            extracted_data["processed_time"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            
            return extracted_data
            
        finally:
            if os.path.exists(temp_path):
                os.remove(temp_path)
    
    def _process_pdf(self, file_path: str) -> Dict[str, Any]:
        """
        معالجة ملف PDF واستخراج النص والبيانات منه
        """
        extracted_data = {"text": "", "metadata": {}, "images": [], "tables": [], "pages": []}
        try:
            with pdfplumber.open(file_path) as pdf:
                for page in pdf.pages:
                    extracted_text = page.extract_text()
                    if extracted_text:
                        extracted_data["text"] += extracted_text + "\n"
            
            if not extracted_data["text"].strip():
                extracted_data["text"] = self._apply_ocr_to_pdf(file_path)
        except Exception as e:
            extracted_data["error"] = f"خطأ في معالجة ملف PDF: {str(e)}"
        return extracted_data
    
    def _apply_ocr_to_pdf(self, file_path: str) -> str:
        """
        تطبيق OCR على ملف PDF لاستخراج النص من الصور
        """
        try:
            doc = fitz.open(file_path)
            ocr_text = ""
            for page in doc:
                pix = page.get_pixmap()
                img_data = pix.tobytes("png")
                with io.BytesIO(img_data) as img_stream:
                    img = Image.open(img_stream)
                    ocr_text += pytesseract.image_to_string(img, lang='ara+eng') + "\n"
            return ocr_text
        except Exception as e:
            return f"خطأ في OCR: {str(e)}"
    
    def _process_docx(self, file_path: str) -> Dict[str, Any]:
        """
        معالجة ملف Word (DOCX) واستخراج النص والبيانات منه
        """
        extracted_data = {"text": "", "metadata": {}, "images": [], "tables": [], "paragraphs": []}
        try:
            doc = docx.Document(file_path)
            extracted_data["text"] = "\n".join([para.text for para in doc.paragraphs if para.text.strip()])
        except Exception as e:
            extracted_data["error"] = f"خطأ في معالجة ملف DOCX: {str(e)}"
        return extracted_data