File size: 19,115 Bytes
e305028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c143b5
e305028
 
 
 
 
 
4c143b5
e305028
 
 
 
 
4c143b5
e305028
 
4c143b5
e305028
 
 
 
 
4c143b5
e305028
 
 
4c143b5
e305028
 
 
 
 
 
 
 
 
 
 
 
 
4c143b5
e305028
 
 
 
 
 
 
4c143b5
e305028
4c143b5
e305028
 
 
 
 
4c143b5
e305028
 
 
 
 
 
 
 
 
 
 
 
4c143b5
e305028
 
 
 
4c143b5
e305028
4c143b5
e305028
 
 
 
4c143b5
e305028
 
4c143b5
e305028
 
 
4c143b5
e305028
 
 
 
 
4c143b5
e305028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c143b5
 
 
 
e305028
 
 
4c143b5
 
e305028
 
 
4c143b5
 
e305028
 
 
4c143b5
 
92688b8
e305028
 
4c143b5
 
e305028
 
 
4c143b5
 
e305028
 
 
4c143b5
 
e305028
 
 
4c143b5
 
92688b8
 
e305028
 
 
 
4c143b5
 
 
 
 
 
 
 
 
 
 
 
 
92688b8
4c143b5
 
92688b8
e305028
 
 
4c143b5
e305028
 
 
4c143b5
 
92688b8
4c143b5
 
92688b8
4c143b5
 
 
 
 
 
92688b8
4c143b5
 
 
 
 
 
92688b8
e305028
 
 
4c143b5
92688b8
 
 
4c143b5
 
92688b8
4c143b5
92688b8
4c143b5
 
92688b8
e305028
 
 
4c143b5
e305028
 
 
 
 
 
 
4c143b5
e305028
 
 
 
 
4c143b5
e305028
 
 
4c143b5
e305028
 
 
 
 
4c143b5
e305028
 
 
 
 
4c143b5
e305028
 
 
4c143b5
e305028
 
4c143b5
e305028
 
 
4c143b5
e305028
 
 
 
 
4c143b5
e305028
 
 
4c143b5
e305028
 
4c143b5
e305028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c143b5
e305028
 
 
4c143b5
e305028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c143b5
e305028
 
 
 
 
 
 
 
 
 
 
 
 
 
4c143b5
e305028
 
 
 
4c143b5
e305028
 
 
 
 
 
 
4c143b5
e305028
 
 
4c143b5
e305028
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
"""
وحدة تحليل المستندات لنظام إدارة المناقصات - Hybrid Face
"""

import os
import re
import logging
import threading
from pathlib import Path
import datetime
import json

# تهيئة السجل
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('document_analysis')

class DocumentAnalyzer:
    """فئة تحليل المستندات"""
    
    def __init__(self, config=None):
        """تهيئة محلل المستندات"""
        self.config = config
        self.analysis_in_progress = False
        self.current_document = None
        self.analysis_results = {}
        
        # إنشاء مجلد المستندات إذا لم يكن موجوداً
        if config and hasattr(config, 'DOCUMENTS_PATH'):
            self.documents_path = Path(config.DOCUMENTS_PATH)
        else:
            self.documents_path = Path('data/documents')
            
        if not self.documents_path.exists():
            self.documents_path.mkdir(parents=True, exist_ok=True)
    
    def analyze_document(self, document_path, document_type="tender", callback=None):
        """تحليل مستند"""
        if self.analysis_in_progress:
            logger.warning("هناك عملية تحليل جارية بالفعل")
            return False
        
        if not os.path.exists(document_path):
            logger.error(f"المستند غير موجود: {document_path}")
            return False
        
        self.analysis_in_progress = True
        self.current_document = document_path
        self.analysis_results = {
            "document_path": document_path,
            "document_type": document_type,
            "analysis_start_time": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            "status": "جاري التحليل",
            "items": [],
            "entities": [],
            "dates": [],
            "amounts": [],
            "risks": []
        }
        
        # بدء التحليل في خيط منفصل
        thread = threading.Thread(
            target=self._analyze_document_thread,
            args=(document_path, document_type, callback)
        )
        thread.daemon = True
        thread.start()
        
        return True
    
    def _analyze_document_thread(self, document_path, document_type, callback):
        """خيط تحليل المستند"""
        try:
            # تحديد نوع المستند
            file_extension = os.path.splitext(document_path)[1].lower()
            
            if file_extension == '.pdf':
                self.analysis_results = self._analyze_pdf(document_path, document_type)
            elif file_extension == '.docx':
                self._analyze_docx(document_path, document_type)
            elif file_extension == '.xlsx':
                self._analyze_xlsx(document_path, document_type)
            elif file_extension == '.txt':
                self._analyze_txt(document_path, document_type)
            else:
                logger.error(f"نوع المستند غير مدعوم: {file_extension}")
                self.analysis_results["status"] = "فشل التحليل"
                self.analysis_results["error"] = "نوع المستند غير مدعوم"
            
            # تحديث حالة التحليل
            if self.analysis_results["status"] != "فشل التحليل":
                self.analysis_results["status"] = "اكتمل التحليل"
                self.analysis_results["analysis_end_time"] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            
            logger.info(f"اكتمل تحليل المستند: {document_path}")
            
        except Exception as e:
            logger.error(f"خطأ في تحليل المستند: {str(e)}")
            self.analysis_results["status"] = "فشل التحليل"
            self.analysis_results["error"] = str(e)
        
        finally:
            self.analysis_in_progress = False
            
            # استدعاء دالة الاستجابة إذا تم توفيرها
            if callback and callable(callback):
                callback(self.analysis_results)
    
    def _analyze_pdf(self, document_path, document_type):
        """تحليل مستند PDF باستخدام الذكاء الاصطناعي"""
        try:
            # استخراج النص من PDF
            text = self._extract_text_from_pdf(document_path)
            
            # تحليل متقدم للمستند
            analysis = {
                "file_info": {
                    "name": os.path.basename(document_path),
                    "type": "PDF",
                    "size": os.path.getsize(document_path),
                    "pages": self._count_pages(document_path),
                    "create_date": self._get_creation_date(document_path),
                    "modify_date": time.ctime(os.path.getmtime(document_path))
                },
                "content_analysis": {
                    "contract_terms": self._analyze_contract_terms(text),
                    "financial_analysis": self._analyze_financial_terms(text),
                    "legal_analysis": self._analyze_legal_terms(text),
                    "risk_analysis": self._analyze_risks(text),
                    "conditions_analysis": self._analyze_conditions(text),
                    "technical_specs": self._analyze_technical_specs(text),
                    "key_dates": self._extract_key_dates(text),
                    "important_figures": self._extract_figures(text),
                    "entities": self._extract_entities(text)
                },
                "statistical_analysis": {
                    "word_count": len(text.split()),
                    "unique_terms": self._analyze_unique_terms(text),
                    "topic_distribution": self._analyze_topics(text),
                    "complexity_score": self._calculate_complexity(text)
                },
                "compliance_check": {
                    "missing_sections": self._check_missing_sections(text),
                    "required_terms": self._check_required_terms(text),
                    "compliance_score": self._calculate_compliance_score(text)
                },
                "summary": self._generate_summary(text),
                "recommendations": self._generate_recommendations(text),
                "related_documents": self._find_related_documents(document_path),
                "version_info": self._get_version_info(document_path)
            }

            # إضافة تحليل متخصص حسب نوع المستند
            if document_type == "tender":
                analysis["tender_analysis"] = self._analyze_tender_specifics(text)
            elif document_type == "contract":
                analysis["contract_analysis"] = self._analyze_contract_specifics(text)
            elif document_type == "technical":
                analysis["technical_analysis"] = self._analyze_technical_specifics(text)

            return analysis

        except Exception as e:
            logger.error(f"خطأ في تحليل PDF: {str(e)}")
            raise

    def _extract_text_from_pdf(self, document_path):
        """استخراج النص من ملف PDF (تحتاج إلى مكتبة مثل PyPDF2 أو pdfplumber)"""
        #  Implementation using a PDF processing library like PyPDF2 or pdfplumber is needed here.
        # This is a placeholder.  Replace with actual PDF text extraction.
        return "Placeholder text extracted from PDF"

    def _analyze_contract_terms(self, text):
        """تحليل بنود العقد"""
        #  Implementation for contract term analysis is needed here.  This is a placeholder.
        return "Placeholder contract terms analysis"

    def _analyze_financial_terms(self, text):
        """تحليل الجزء المالي"""
        #  Implementation for financial term analysis is needed here.  This is a placeholder.
        return "Placeholder financial terms analysis"

    def _analyze_legal_terms(self, text):
        """تحليل القانوني للعقد"""
        #  Implementation for legal term analysis is needed here. This is a placeholder.
        return "Placeholder legal terms analysis"

    def _analyze_risks(self, text):
        """تحليل المخاطر"""
        #  Implementation for risk analysis is needed here.  This is a placeholder.
        return "Placeholder risk analysis"

    def _analyze_conditions(self, text):
        """دراسة كراسة الشروط"""
        #  Implementation for conditions analysis is needed here.  This is a placeholder.
        return "Placeholder conditions analysis"

    def _generate_summary(self, text):
        """توليد ملخص"""
        #  Implementation for summary generation is needed here. This is a placeholder.
        return "Placeholder summary"

    def _generate_recommendations(self, text):
        """توليد التوصيات"""
        #  Implementation for recommendation generation is needed here. This is a placeholder.
        return "Placeholder recommendations"



    def _analyze_docx(self, document_path, document_type):
        """تحليل مستند Word"""
        try:
            # محاكاة تحليل مستند Word
            logger.info(f"تحليل مستند Word: {document_path}")
            
            # في التطبيق الفعلي، سيتم استخدام مكتبة مثل python-docx
            # لاستخراج النص من ملف Word وتحليله
            
            # محاكاة استخراج البنود والكيانات والتواريخ والمبالغ والمخاطر
            # (مشابه لتحليل PDF)
            self.analysis_results["items"] = [
                {"id": 1, "name": "توريد معدات", "description": "توريد معدات المشروع", "unit": "مجموعة", "estimated_quantity": 10},
                {"id": 2, "name": "تركيب المعدات", "description": "تركيب وتشغيل المعدات", "unit": "مجموعة", "estimated_quantity": 10},
                {"id": 3, "name": "التدريب", "description": "تدريب الموظفين على استخدام المعدات", "unit": "يوم", "estimated_quantity": 20}
            ]
            
            # محاكاة استخراج الكيانات والتواريخ والمبالغ والمخاطر
            # (مشابه لتحليل PDF)
            
        except Exception as e:
            logger.error(f"خطأ في تحليل مستند Word: {str(e)}")
            raise
    
    def _analyze_xlsx(self, document_path, document_type):
        """تحليل مستند Excel"""
        try:
            # محاكاة تحليل مستند Excel
            logger.info(f"تحليل مستند Excel: {document_path}")
            
            # في التطبيق الفعلي، سيتم استخدام مكتبة مثل pandas أو openpyxl
            # لاستخراج البيانات من ملف Excel وتحليلها
            
            # محاكاة استخراج البنود
            self.analysis_results["items"] = [
                {"id": 1, "name": "بند 1", "description": "وصف البند 1", "unit": "وحدة", "estimated_quantity": 100},
                {"id": 2, "name": "بند 2", "description": "وصف البند 2", "unit": "وحدة", "estimated_quantity": 200},
                {"id": 3, "name": "بند 3", "description": "وصف البند 3", "unit": "وحدة", "estimated_quantity": 300}
            ]
            
            # محاكاة استخراج المبالغ
            self.analysis_results["amounts"] = [
                {"type": "item_cost", "amount": 10000, "currency": "SAR", "description": "تكلفة البند 1"},
                {"type": "item_cost", "amount": 20000, "currency": "SAR", "description": "تكلفة البند 2"},
                {"type": "item_cost", "amount": 30000, "currency": "SAR", "description": "تكلفة البند 3"}
            ]
            
        except Exception as e:
            logger.error(f"خطأ في تحليل مستند Excel: {str(e)}")
            raise
    
    def _analyze_txt(self, document_path, document_type):
        """تحليل مستند نصي"""
        try:
            # محاكاة تحليل مستند نصي
            logger.info(f"تحليل مستند نصي: {document_path}")
            
            # في التطبيق الفعلي، سيتم قراءة الملف النصي وتحليله
            
            # محاكاة استخراج البنود والكيانات والتواريخ والمبالغ والمخاطر
            # (مشابه للتحليلات الأخرى)
            
        except Exception as e:
            logger.error(f"خطأ في تحليل مستند نصي: {str(e)}")
            raise
    
    def get_analysis_status(self):
        """الحصول على حالة التحليل الحالي"""
        if not self.analysis_in_progress:
            if not self.analysis_results:
                return {"status": "لا يوجد تحليل جارٍ"}
            else:
                return {"status": self.analysis_results.get("status", "غير معروف")}
        
        return {
            "status": "جاري التحليل",
            "document_path": self.current_document,
            "start_time": self.analysis_results.get("analysis_start_time")
        }
    
    def get_analysis_results(self):
        """الحصول على نتائج التحليل"""
        return self.analysis_results
    
    def export_analysis_results(self, output_path=None):
        """تصدير نتائج التحليل إلى ملف JSON"""
        if not self.analysis_results:
            logger.warning("لا توجد نتائج تحليل للتصدير")
            return None
        
        if not output_path:
            # إنشاء اسم ملف افتراضي
            timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
            filename = f"analysis_results_{timestamp}.json"
            output_path = os.path.join(self.documents_path, filename)
        
        try:
            with open(output_path, 'w', encoding='utf-8') as f:
                json.dump(self.analysis_results, f, ensure_ascii=False, indent=4)
            
            logger.info(f"تم تصدير نتائج التحليل إلى: {output_path}")
            return output_path
        
        except Exception as e:
            logger.error(f"خطأ في تصدير نتائج التحليل: {str(e)}")
            return None
    
    def import_analysis_results(self, input_path):
        """استيراد نتائج التحليل من ملف JSON"""
        if not os.path.exists(input_path):
            logger.error(f"ملف نتائج التحليل غير موجود: {input_path}")
            return False
        
        try:
            with open(input_path, 'r', encoding='utf-8') as f:
                self.analysis_results = json.load(f)
            
            logger.info(f"تم استيراد نتائج التحليل من: {input_path}")
            return True
        
        except Exception as e:
            logger.error(f"خطأ في استيراد نتائج التحليل: {str(e)}")
            return False

    def _count_pages(self, document_path):
        """حساب عدد صفحات المستند"""
        try:
            import PyPDF2
            with open(document_path, 'rb') as file:
                reader = PyPDF2.PdfReader(file)
                return len(reader.pages)
        except:
            return 0

    def _get_creation_date(self, document_path):
        """استخراج تاريخ إنشاء المستند"""
        try:
            import PyPDF2
            with open(document_path, 'rb') as file:
                reader = PyPDF2.PdfReader(file)
                if '/CreationDate' in reader.metadata:
                    return reader.metadata['/CreationDate']
                return "غير متوفر"
        except:
            return "غير متوفر"

    def _analyze_technical_specs(self, text):
        """تحليل المواصفات الفنية"""
        specs = {
            "materials": self._extract_materials(text),
            "measurements": self._extract_measurements(text),
            "standards": self._extract_standards(text)
        }
        return specs

    def _extract_key_dates(self, text):
        """استخراج التواريخ المهمة"""
        import re
        date_pattern = r'\d{1,2}[-/]\d{1,2}[-/]\d{2,4}'
        dates = re.findall(date_pattern, text)
        return list(set(dates))

    def _extract_figures(self, text):
        """استخراج الأرقام والقيم المهمة"""
        import re
        # البحث عن القيم النقدية
        currency_pattern = r'[\d,]+\.?\d*\s*(?:ريال|دولار|SAR|USD)'
        currencies = re.findall(currency_pattern, text)
        
        # البحث عن النسب المئوية
        percentage_pattern = r'\d+\.?\d*\s*%'
        percentages = re.findall(percentage_pattern, text)
        
        return {
            "currencies": currencies,
            "percentages": percentages
        }

    def _analyze_unique_terms(self, text):
        """تحليل المصطلحات الفريدة"""
        words = set(text.split())
        return list(words)

    def _calculate_complexity(self, text):
        """حساب مستوى تعقيد النص"""
        words = text.split()
        avg_word_length = sum(len(word) for word in words) / len(words)
        sentences = text.split('.')
        avg_sentence_length = len(words) / len(sentences)
        
        # حساب درجة التعقيد (1-10)
        complexity = min((avg_word_length * 0.5 + avg_sentence_length * 0.2), 10)
        return round(complexity, 2)

    def _check_missing_sections(self, text):
        """التحقق من الأقسام المفقودة"""
        required_sections = [
            "نطاق العمل",
            "المواصفات الفنية",
            "الشروط العامة",
            "الضمانات",
            "الغرامات",
            "شروط الدفع"
        ]
        
        missing = []
        for section in required_sections:
            if section not in text:
                missing.append(section)
        
        return missing

    def _find_related_documents(self, document_path):
        """البحث عن المستندات المرتبطة"""
        directory = os.path.dirname(document_path)
        base_name = os.path.basename(document_path)
        related = []
        
        for file in os.listdir(directory):
            if file != base_name and file.startswith(base_name.split('_')[0]):
                related.append(file)
        
        return related