Files
xSaherelmWorkspace/Documents/Docs/Developer/XRTFFile Content Extractor.html
T

1048 lines
47 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>مستند پیاده‌سازی XRtfFileContentExtractor | پروژه xAiApi</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 950px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.document-header::before {
content: '';
position: absolute;
top: 0; right: 0;
width: 200px; height: 200px;
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
border-radius: 50%;
transform: translate(50%, -50%);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge { display: flex; align-items: center; gap: 12px; }
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta { text-align: left; font-size: 12px; opacity: 0.9; }
.doc-meta div { margin-bottom: 3px; }
.document-title { text-align: center; position: relative; z-index: 1; padding: 20px 0; }
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 { font-size: 28px; font-weight: 800; margin-bottom: 10px; letter-spacing: -0.5px; }
.document-title h2 { font-size: 16px; font-weight: 400; opacity: 0.9; }
.document-body { padding: 40px; }
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section { margin-bottom: 40px; page-break-inside: avoid; }
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 { font-size: 19px; font-weight: 700; color: var(--primary); margin-bottom: 2px; }
.section-title .subtitle { font-size: 12px; color: var(--text-muted); }
.section-content { padding-right: 10px; }
.section-content p { margin-bottom: 12px; text-align: justify; text-indent: 20px; }
.section-content p.no-indent { text-indent: 0; }
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box strong { color: var(--accent-dark); }
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead { background: linear-gradient(135deg, var(--primary), var(--primary-light)); color: white; }
.styled-table th { padding: 14px 16px; text-align: right; font-weight: 600; font-size: 13px; }
.styled-table td { padding: 13px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row { background: #fffaf0 !important; font-weight: 600; }
.conclusion-box {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 30px;
border-radius: 12px;
margin: 30px 0;
text-align: center;
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
}
.conclusion-box h3 { font-size: 22px; font-weight: 800; margin-bottom: 15px; color: var(--accent); }
.conclusion-box p { font-size: 14px; line-height: 1.8; text-indent: 0 !important; margin-bottom: 15px; }
.conclusion-box .highlight {
background: rgba(201,169,97,0.2);
padding: 15px 20px;
border-radius: 8px;
margin-top: 20px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand { font-weight: 700; color: var(--accent); margin-bottom: 5px; font-size: 14px; }
.footer-divider { width: 60px; height: 2px; background: var(--accent); margin: 10px auto; }
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
code {
font-family: 'Consolas', 'Monaco', monospace;
background: #edf2f7;
padding: 2px 6px;
border-radius: 4px;
font-size: 12px;
direction: ltr;
display: inline-block;
}
pre {
background: #1a202c;
color: #e2e8f0;
padding: 18px 20px;
border-radius: 8px;
overflow-x: auto;
direction: ltr;
text-align: left;
font-size: 12px;
line-height: 1.7;
margin: 15px 0;
font-family: 'Consolas', 'Monaco', monospace;
}
.step-list { list-style: none; padding: 0; counter-reset: step; }
.step-list > li {
position: relative;
padding: 18px 55px 18px 20px;
margin-bottom: 12px;
background: #f8fafc;
border: 1px solid var(--border);
border-radius: 8px;
counter-increment: step;
}
.step-list > li::before {
content: counter(step);
position: absolute;
right: 15px;
top: 18px;
width: 28px; height: 28px;
background: var(--primary);
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-weight: 700;
font-size: 13px;
}
.step-list > li strong { color: var(--primary); }
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: TECH-1405-045</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: ۱ آبان ۱۴۰۵</div>
<div>طبقه‌بندی: محرمانه</div>
</div>
</div>
<div class="document-title">
<span class="label">IMPLEMENTATION GUIDE</span>
<h1>مستند پیاده‌سازی XRtfFileContentExtractor</h1>
<h2>راهنمای کامل گام‌به‌گام افزودن Extractor اختصاصی RTF به سامانه xAiApi</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>به کارفرمای محترم،</strong><br>
این مستند حاصل طراحی و پیاده‌سازی کامل <code>XRtfFileContentExtractor</code> به‌عنوان یکی از Extractor های اولویت متوسط در زیرسیستم <code>xAiApi/Providers/Extractors</code> است. فرمت RTF (Rich Text Format) یک فرمت اسناد متنی پرکاربرد است که توسط Microsoft Word و بسیاری از ابزارهای اداری تولید می‌شود و در محیط‌های سازمانی برای تبادل اسناد متنی ساده به کار می‌رود. در پیاده‌سازی فعلی، فایل‌های RTF توسط <code>XPlainTextFileContentExtractor</code> به‌صورت متن خام خوانده می‌شوند که منجر به استخراج کدهای کنترلی RTF (مانند <code>\rtf1\ansi\deff0</code>) به‌همراه متن می‌شود. هدف این مستند، ارائه راهنمای گام‌به‌گام برای جایگزینی این رفتار با یک Extractor اختصاصی است که متن تمیز و قابل خواندن را از فایل‌های RTF استخراج نماید.
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>خلاصه اجرایی</h3>
<div class="subtitle">Executive Summary</div>
</div>
</div>
<div class="section-content">
<p>فرمت RTF (Rich Text Format) یک فرمت متن غنی است که مایکروسافت آن را در سال ۱۹۸۷ معرفی کرد و تا امروز به‌عنوان یک فرمت استاندارد تبادل اسناد در محیط‌های اداری و سازمانی مورد استفاده قرار می‌گیرد. این فرمت با پسوند <code>.rtf</code> و MIME Type <code>application/rtf</code> (و گاهی <code>text/rtf</code>) شناخته می‌شود[reference:0][reference:1].</p>
<p>در حال حاضر سامانه xAiApi فایل‌های RTF را با استفاده از <code>XPlainTextFileContentExtractor</code> پردازش می‌کند که تنها محتوای خام فایل را با <code>StreamReader</code> می‌خواند. این روش هرچند ساده است، اما متن استخراج‌شده شامل کدهای کنترلی RTF (مانند <code>\rtf1</code>، <code>\ansi</code>، <code>\deff0</code>، <code>\fonttbl</code>) و دستورات قالب‌بندی (مانند <code>\b</code>، <code>\i</code>، <code>\par</code>) خواهد بود که برای موتور LLM مطلوب نیست.</p>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value">۲<br>MIME Type</div>
<div class="stat-label">application/rtf و text/rtf</div>
</div>
<div class="stat-card">
<div class="stat-value">۱<br>کتابخانه</div>
<div class="stat-label">RtfPipe (MIT، Cross-platform)</div>
</div>
<div class="stat-card">
<div class="stat-value">۳<br>نوع محتوا</div>
<div class="stat-label">متن، جداول، لیست‌ها</div>
</div>
</div>
<div class="highlight-box">
<strong>نتیجه کلیدی:</strong> پس از پیاده‌سازی <code>XRtfFileContentExtractor</code>، سامانه قادر خواهد بود متن تمیز و قابل خواندن را از فایل‌های RTF استخراج کند، کدهای کنترلی را حذف نماید و ساختار سند (پاراگراف‌ها، جداول و لیست‌ها) را تا حد امکان حفظ کند. این Extractor با ساختار DI موجود کاملاً یکپارچه می‌شود و نیازی به تغییر در سایر بخش‌های سامانه ندارد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>وضعیت موجود و چالش‌ها</h3>
<div class="subtitle">Current State & Issues</div>
</div>
</div>
<div class="section-content">
<p>در پیاده‌سازی فعلی، فایل‌های RTF توسط <code>XPlainTextFileContentExtractor</code> پردازش می‌شوند. آرایه <code>SupportedMimeTypes</code> در این Extractor شامل <code>"application/rtf"</code> نیست، اما اگر MIME Type فایل توسط کلاینت به‌درستی ارسال نشود یا به‌صورت <code>text/plain</code> گزارش شود، محتوای خام RTF به‌عنوان متن ساده خوانده می‌شود.</p>
<p>حتی اگر <code>"application/rtf"</code> به آرایه اضافه شود، متد <code>ExtractAsync</code> صرفاً محتوای خام را با <code>StreamReader</code> می‌خواند:</p>
<pre>public async Task&lt;string&gt; ExtractAsync(
Stream fileStream,
string mimeType,
CancellationToken cancellationToken = default
)
{
using var reader = new StreamReader(fileStream);
return await reader.ReadToEndAsync();
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">مشکلات این رویکرد</h4>
<table class="styled-table">
<thead>
<tr>
<th>#</th>
<th>مشکل</th>
<th>توضیح</th>
<th>تأثیر بر LLM</th>
</tr>
</thead>
<tbody>
<tr>
<td>۱</td>
<td>حضور کدهای کنترلی RTF</td>
<td>خروجی شامل <code>\rtf1\ansi\deff0</code>، <code>\fonttbl</code>، <code>\colortbl</code> و ... است</td>
<td style="color: var(--danger); font-weight: 600;">مصرف توکن بالا، نویز زیاد</td>
</tr>
<tr>
<td>۲</td>
<td>دستورات قالب‌بندی</td>
<td><code>\b</code>، <code>\i</code>، <code>\par</code>، <code>\line</code> و ... در خروجی ظاهر می‌شوند</td>
<td style="color: var(--danger); font-weight: 600;">سردرگمی مدل</td>
</tr>
<tr>
<td>۳</td>
<td>عدم رمزگشایی کاراکترها</td>
<td><code>\'e2</code> و کدهای هگز مشابه به‌صورت خام باقی می‌مانند</td>
<td style="color: var(--warning); font-weight: 600;">کاهش دقت درک متن</td>
</tr>
<tr>
<td>۴</td>
<td>از دست رفتن ساختار</td>
<td>پاراگراف‌ها، جداول و لیست‌ها قابل تشخیص نیستند</td>
<td style="color: var(--warning); font-weight: 600;">از دست رفتن سلسله‌مراتب محتوا</td>
</tr>
<tr class="highlight-row">
<td>۵</td>
<td>عدم پشتیبانی رسمی از RTF</td>
<td><code>"application/rtf"</code> در لیست MIME موجود نیست</td>
<td style="color: var(--danger); font-weight: 600;">عدم پردازش اختصاصی</td>
</tr>
</tbody>
</table>
<div class="warning-box">
<strong>⚠ ریسک عملیاتی:</strong> در سناریوهای تحلیل اسناد سازمانی و RAG، حضور کدهای کنترلی RTF در متن استخراج‌شده می‌تواند کیفیت پاسخ LLM را به‌طور قابل‌توجهی کاهش دهد. همچنین مصرف بی‌رویه توکن برای کدهای کنترلی، هزینه عملیاتی را افزایش می‌دهد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>تحلیل گزینه‌ها و انتخاب کتابخانه</h3>
<div class="subtitle">Library Analysis & Selection</div>
</div>
</div>
<div class="section-content">
<p>برای استخراج متن از RTF در .NET، چندین گزینه وجود دارد. جدول زیر گزینه‌های اصلی را با معیارهای کلیدی مقایسه می‌کند:</p>
<table class="styled-table">
<thead>
<tr>
<th>کتابخانه</th>
<th>Cross-platform</th>
<th>وابستگی Windows Forms</th>
<th>مجوز</th>
<th>بلوغ</th>
<th>ارزیابی</th>
</tr>
</thead>
<tbody>
<tr class="highlight-row">
<td><strong>RtfPipe</strong></td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✗ (ندارد)</td>
<td>MIT (رایگان)</td>
<td>بالغ (۲۰۱۷ به بعد)</td>
<td>بهترین گزینه برای Cross-platform</td>
</tr>
<tr>
<td>RichTextBox (WinForms)</td>
<td style="color: var(--danger); font-weight: 600;">✗ (فقط Windows)</td>
<td style="color: var(--danger); font-weight: 600;">✓ (وابسته)</td>
<td>—</td>
<td>—</td>
<td>مناسب Docker/Linux نیست</td>
</tr>
<tr>
<td>Aspose.Words</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✗</td>
<td style="color: var(--danger); font-weight: 600;">تجاری (پرهزینه)</td>
<td>بالغ</td>
<td>لایسنس تجاری مانع استفاده است</td>
</tr>
<tr>
<td>Syncfusion DocIO</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✗</td>
<td style="color: var(--danger); font-weight: 600;">تجاری</td>
<td>بالغ</td>
<td>هزینه لایسنس بالا</td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">دلایل انتخاب RtfPipe</h4>
<p><strong>RtfPipe</strong> به‌عنوان کتابخانه اصلی انتخاب می‌شود. طبق مستندات رسمی، این کتابخانه یک «کتابخانه .NET برای پردازش جریان‌های Rich Text Format (RTF) و تبدیل آن‌ها به HTML» است[reference:2]. نکات کلیدی:</p>
<div class="success-box">
<strong>✓ دلیل ۱ — Cross-platform و بدون وابستگی Windows Forms:</strong> RtfPipe یک کتابخانه کاملاً Managed و Cross-platform است و برخلاف RichTextBox، به Windows Forms یا GDI+ وابسته نیست. این ویژگی آن را برای استقرار در Docker/Linux ایده‌آل می‌کند. یک منبع معتبر تأکید می‌کند: «RtfPipe (مجوز MIT، نصب از NuGet) به Windows Forms وابسته نیست، پیاده‌سازی خالص C# دارد و بر “RTF → متن” تمرکز دارد»[reference:3].
</div>
<div class="success-box">
<strong>✓ دلیل ۲ — مجوز MIT و بلوغ:</strong> مجوز MIT استفاده تجاری بدون هزینه را ممکن می‌سازد. این کتابخانه از سال ۲۰۱۷ توسعه یافته و به‌طور فعال نگهداری می‌شود (آخرین به‌روزرسانی: ۲۰۲۴)[reference:4].
</div>
<div class="success-box">
<strong>✓ دلیل ۳ — پشتیبانی از ویژگی‌های کلیدی RTF:</strong> طبق مستندات رسمی، RtfPipe از قالب‌بندی کاراکتر (بولد، ایتالیک، رنگ)، جداول (شامل جداول تودرتو)، لیست‌ها، لینک‌ها، تصاویر و سطوح سرتیتر پشتیبانی می‌کند[reference:5]. این پوشش برای استخراج متن از اسناد سازمانی کافی است.
</div>
<div class="info-box">
<strong>💡 نکته درباره خروجی:</strong> RtfPipe به‌طور پیش‌فرض RTF را به HTML تبدیل می‌کند. برای استخراج متن ساده، دو راه وجود دارد: (۱) خروجی HTML را با یک تگ‌زدا ساده به متن تبدیل کنیم، یا (۲) از کتابخانه <code>BracketPipe</code> (که RtfPipe با آن یکپارچه می‌شود) برای تبدیل مستقیم به Markdown یا متن ساده استفاده کنیم. در این مستند، از روش اول (تبدیل HTML به متن) استفاده می‌شود تا وابستگی اضافی به BracketPipe ایجاد نشود[reference:6].
</div>
<div class="warning-box">
<strong>⚠ نکته مهم برای .NET Core:</strong> طبق مستندات رسمی RtfPipe، هنگام استفاده در .NET Core باید بسته <code>System.Text.Encoding.CodePages</code> را نیز نصب کرده و خط زیر را قبل از فراخوانی هر تابع کتابخانه اجرا کنید[reference:7]:
<pre style="margin-top: 10px;">Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);</pre>
این کار برای پشتیبانی از کدپیج‌های مختلف (مانند Windows-1256 برای فارسی) ضروری است.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>طراحی اینترفیس و کلاس</h3>
<div class="subtitle">Class & Interface Design</div>
</div>
</div>
<div class="section-content">
<p>طراحی <code>XRtfFileContentExtractor</code> با الگوی موجود در پروژه کاملاً هم‌راستا است. یک اینترفیس اختصاصی که از <code>IXFileContentExtractorBase</code> ارث‌بری می‌کند و یک کلاس پیاده‌ساز مطابق الگوی سایر Extractor ها ایجاد می‌شود.</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">۱. اینترفیس اختصاصی</h4>
<p>فایل <code>Interfaces/Extractors/IXRtfFileContentExtractor.cs</code>:</p>
<pre>namespace xAiApi.Interfaces.Extractors
{
/// &lt;summary&gt;
/// Extractor for Rich Text Format (RTF) documents
/// (application/rtf, text/rtf)
/// &lt;/summary&gt;
public interface IXRtfFileContentExtractor : IXFileContentExtractorBase
{ }
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۲. طراحی کلاس اصلی</h4>
<p>کلاس <code>XRtfFileContentExtractor</code> ساختار زیر را خواهد داشت:</p>
<table class="styled-table">
<thead>
<tr>
<th>عضو</th>
<th>نوع</th>
<th>توضیح</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>SupportedMimeTypes</code></td>
<td>static string[]</td>
<td>آرایه ثابت شامل <code>application/rtf</code> و <code>text/rtf</code></td>
</tr>
<tr>
<td><code>CanExtract(string mimeType)</code></td>
<td>bool</td>
<td>بررسی پشتیبانی بر اساس MIME</td>
</tr>
<tr>
<td><code>ExtractAsync(...)</code></td>
<td>Task&lt;string&gt;</td>
<td>استخراج متن ساده</td>
</tr>
<tr>
<td><code>ExtractRichAsync(...)</code></td>
<td>Task&lt;XFileExtractionResult&gt;</td>
<td>استخراج متن + مدیریت خطا</td>
</tr>
<tr>
<td><code>ExtractRtfAsync(...)</code></td>
<td>private Task&lt;string&gt;</td>
<td>منطق اصلی استخراج با RtfPipe</td>
</tr>
<tr>
<td><code>HtmlToPlainText(string)</code></td>
<td>private string</td>
<td>تبدیل خروجی HTML به متن ساده</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>پیاده‌سازی گام‌به‌گام</h3>
<div class="subtitle">Step-by-Step Implementation</div>
</div>
</div>
<div class="section-content">
<p>مراحل زیر به‌ترتیب برای افزودن کامل Extractor RTF به پروژه xAiApi انجام می‌شود:</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۱ — افزودن بسته‌های NuGet</h4>
<p>در فایل <code>xAiApi.csproj</code>، دو بسته زیر را اضافه کنید. دستور نصب از طریق .NET CLI:</p>
<pre>dotnet add package RtfPipe
dotnet add package System.Text.Encoding.CodePages</pre>
<p>یا مستقیماً در فایل پروژه:</p>
<pre>&lt;PackageReference Include="RtfPipe" Version="2.0.7677.4303" /&gt;
&lt;PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" /&gt;</pre>
<div class="info-box">
<strong>💡 نکته:</strong> بسته <code>System.Text.Encoding.CodePages</code> برای ثبت <code>CodePagesEncodingProvider</code> در .NET Core ضروری است و باید یک‌بار در شروع برنامه فراخوانی شود. نسخه پایدار فعلی RtfPipe در NuGet، نسخه <code>2.0.7677.4303</code> است[reference:8].
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۲ — ثبت CodePagesEncodingProvider در Startup</h4>
<p>در فایل <code>Program.cs</code> (یا هر نقطه ورود برنامه)، قبل از ساخت Host، خط زیر را اضافه کنید:</p>
<pre>using System.Text;
using System.Text.Encoding.CodePages;
// در متد Main، قبل از CreateHostBuilder
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);</pre>
<p>مثال کامل:</p>
<pre>public class Program
{
public static void Main(string[] args)
{
// Register CodePages for RtfPipe and ExcelDataReader
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =&gt;
{
webBuilder.UseStartup&lt;Startup&gt;();
});
}</pre>
<div class="warning-box">
<strong>⚠ نکته مهم:</strong> اگر این خط را فراموش کنید، RtfPipe ممکن است در پردازش فایل‌های RTF با کدپیج‌های غیر UTF-8 (مانند Windows-1256 برای فارسی) با خطا مواجه شود. همچنین <code>ExcelDataReader</code> که در پروژه استفاده می‌شود نیز به این ثبت نیاز دارد، بنابراین این تغییر به کل پروژه کمک می‌کند.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۳ — ایجاد اینترفیس اختصاصی</h4>
<p>فایل جدید <code>Interfaces/Extractors/IXRtfFileContentExtractor.cs</code> را با محتوای نشان‌داده‌شده در بخش ۴ (بالا) ایجاد کنید.</p>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۴ — پیاده‌سازی کلاس اصلی</h4>
<p>فایل <code>Providers/Extractors/XRtfFileContentExtractor.cs</code> را با ساختار زیر ایجاد کنید:</p>
<pre>using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using xAiModels.Models;
using xCommons.Extensions;
using System.Threading.Tasks;
using RtfPipe;
using System.Text.RegularExpressions;
using xAiApi.Interfaces.Extractors;
namespace xAiApi.Providers.Extractors
{
/// &lt;summary&gt;
/// Extracts clean text content from RTF files
/// using RtfPipe library.
/// &lt;/summary&gt;
public class XRtfFileContentExtractor : IXRtfFileContentExtractor
{
private static readonly string[] SupportedMimeTypes =
[
"application/rtf",
"text/rtf"
];
public bool CanExtract(string mimeType)
{
return SupportedMimeTypes.Contains(
mimeType?.ToLowerInvariant() ?? string.Empty
);
}
public async Task&lt;string&gt; ExtractAsync(
Stream fileStream,
string mimeType,
CancellationToken cancellationToken = default
)
{
var result = await ExtractRichAsync(
fileStream,
"document.rtf",
mimeType,
cancellationToken
);
return result.Text;
}
public async Task&lt;XFileExtractionResult&gt; ExtractRichAsync(
Stream fileStream,
string fileName,
string mimeType,
CancellationToken cancellationToken = default
)
{
var result = new XFileExtractionResult
{
FileName = fileName,
MimeType = mimeType
};
try
{
result.Text = await ExtractRtfAsync(
fileStream,
cancellationToken
);
}
catch (Exception ex)
{
result.ErrorMessage =
$"RTF extraction failed: {ex.Message}";
}
return result;
}
private async Task&lt;string&gt; ExtractRtfAsync(
Stream fileStream,
CancellationToken cancellationToken
)
{
// Read RTF content as string
using var reader = new StreamReader(
fileStream,
Encoding.UTF8,
detectEncodingFromByteOrderMarks: true,
leaveOpen: true
);
var rtfContent = await reader.ReadToEndAsync();
if (string.IsNullOrWhiteSpace(rtfContent))
{
return string.Empty;
}
// Convert RTF to HTML using RtfPipe
var html = Rtf.ToHtml(rtfContent);
// Convert HTML to plain text
var plainText = HtmlToPlainText(html);
return plainText;
}
private string HtmlToPlainText(string html)
{
if (string.IsNullOrWhiteSpace(html))
{
return string.Empty;
}
// Remove HTML tags
var text = Regex.Replace(
html,
"&lt;.*?&gt;",
string.Empty,
RegexOptions.Singleline
);
// Decode HTML entities
text = System.Net.WebUtility.HtmlDecode(text);
// Normalize whitespace
text = Regex.Replace(text, @"\s+", " ").Trim();
return text;
}
}
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۵ — توضیح تکمیلی متدهای کلیدی</h4>
<p><strong>متد <code>ExtractRtfAsync</code>:</strong> این متد ابتدا محتوای RTF را از <code>Stream</code> به‌صورت رشته می‌خواند. سپس با استفاده از <code>Rtf.ToHtml(rtfContent)</code>، محتوای RTF را به HTML تبدیل می‌کند. این تبدیل توسط RtfPipe انجام می‌شود و تمام کدهای کنترلی RTF را به تگ‌های HTML استاندارد (مانند <code>&lt;p&gt;</code>، <code>&lt;b&gt;</code>، <code>&lt;i&gt;</code>، <code>&lt;table&gt;</code>) تبدیل می‌کند[reference:9].</p>
<p><strong>متد <code>HtmlToPlainText</code>:</strong> این متد خروجی HTML را به متن ساده تبدیل می‌کند. دو عمل مهم انجام می‌دهد: نخست، تمام تگ‌های HTML را با استفاده از Regex حذف می‌کند. دوم، موجودیت‌های HTML (مانند <code>&amp;amp;</code> به <code>&amp;</code> و <code>&amp;lt;</code> به <code>&lt;</code>) را رمزگشایی می‌کند. در نهایت، فاصله‌های اضافی، تب‌ها و خطوط جدید تکراری را به یک فاصله واحد تبدیل می‌کند تا متن خروجی یکنواخت و خوانا باشد.</p>
<div class="success-box">
<strong>✓ نکته درباره <code>Rtf.ToHtml</code>:</strong> این متد در RtfPipe، یک متد استاتیک است که مستقیماً یک رشته RTF را دریافت و رشته HTML برمی‌گرداند. استفاده از آن بسیار ساده است: <code>var html = Rtf.ToHtml(rtf);</code>[reference:10][reference:11].
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۶ — حذف MIME Type از XPlainTextFileContentExtractor (در صورت وجود)</h4>
<p>اگر <code>"application/rtf"</code> یا <code>"text/rtf"</code> در آرایه <code>SupportedMimeTypes</code> در <code>XPlainTextFileContentExtractor</code> وجود دارد، آن را حذف کنید تا تداخلی پیش نیاید. بررسی کنید:</p>
<pre>// در XPlainTextFileContentExtractor.cs — این خطوط را حذف کنید (اگر وجود دارند)
// "application/rtf",
// "text/rtf",</pre>
<div class="warning-box">
<strong>⚠ نکته مهم:</strong> در کد فعلی <code>XPlainTextFileContentExtractor</code>، MIME Type مربوط به RTF وجود ندارد. بنابراین این مرحله احتمالاً نیازی به تغییر ندارد، اما برای اطمینان بررسی کنید.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۷ — ثبت در Dependency Injection</h4>
<p>در فایل <code>DI/XDIHelperExtension.cs</code>، در متد <code>AddXAiFileContentExtractors</code>، خط زیر را اضافه کنید:</p>
<pre>source.AddSingleton&lt;IXFileContentExtractorBase, XRtfFileContentExtractor&gt;();</pre>
<p>پس از افزودن، بخش ثبت Extractor ها به‌صورت زیر خواهد بود:</p>
<pre>// Register File Content Extractors ...
source.AddSingleton&lt;IXFileContentExtractorBase, XPdfFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XDocxFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XImageFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XAudioFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XExcelFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XVisionFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XPlainTextFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XPowerPointFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XHtmlFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XRtfFileContentExtractor&gt;(); // ← جدید
source.AddSingleton&lt;IXFileContentExtractor, XFileContentExtractor&gt;();</pre>
<div class="success-box">
<strong>✓ بدون نیاز به تغییرات دیگر:</strong> کلاس <code>XFileContentExtractor</code> (Composite Extractor) به‌طور خودکار تمام پیاده‌سازی‌های <code>IXFileContentExtractorBase</code> را دریافت و مدیریت می‌کند. بنابراین تنها ثبت در DI کافی است و نیازی به تغییر در منطق Composite Extractor نیست.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۸ — تنظیمات اختیاری در appsettings.json</h4>
<p>در صورت نیاز به کنترل رفتار استخراج، می‌توانید بخش پیکربندی اختصاصی اضافه کنید (اختیاری، اما برای آینده توصیه می‌شود):</p>
<pre>{
"AiApiConfiguration": {
"Rtf": {
"MaxFileSizeBytes": 10485760,
"NormalizeWhitespace": true
}
}
}</pre>
<div class="info-box">
<strong>💡 نکته:</strong> این تنظیمات برای فاز فعلی اختیاری هستند و در صورت عدم وجود، مقادیر پیش‌فرض استفاده می‌شود. در فاز بعدی می‌توان یک <code>XRtfDescriptor</code> مشابه <code>XPDFToImageDescriptor</code> اضافه کرد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>تست و اعتبارسنجی</h3>
<div class="subtitle">Testing & Validation</div>
</div>
</div>
<div class="section-content">
<p>پس از پیاده‌سازی، تست‌های زیر برای تأیید صحت عملکرد Extractor ضروری هستند:</p>
<table class="styled-table">
<thead>
<tr>
<th>#</th>
<th>سناریو تست</th>
<th>ورودی</th>
<th>خروجی مورد انتظار</th>
</tr>
</thead>
<tbody>
<tr>
<td>۱</td>
<td>CanExtract برای RTF</td>
<td><code>application/rtf</code></td>
<td><code>true</code> برگرداند</td>
</tr>
<tr>
<td>۲</td>
<td>CanExtract برای text/rtf</td>
<td><code>text/rtf</code></td>
<td><code>true</code> برگرداند</td>
</tr>
<tr>
<td>۳</td>
<td>CanExtract برای MIME نامربوط</td>
<td><code>application/pdf</code></td>
<td><code>false</code> برگرداند</td>
</tr>
<tr>
<td>۴</td>
<td>استخراج RTF ساده</td>
<td>RTF با متن ساده و پاراگراف</td>
<td>متن بدون کد کنترلی</td>
</tr>
<tr>
<td>۵</td>
<td>حذف کدهای کنترلی</td>
<td>RTF شامل <code>\rtf1\ansi\deff0</code></td>
<td>کدها در خروجی نباشند</td>
</tr>
<tr>
<td>۶</td>
<td>رمزگشایی کاراکترها</td>
<td>RTF شامل <code>\'e2</code> (حرف فارسی)</td>
<td>کاراکتر صحیح در خروجی</td>
</tr>
<tr>
<td>۷</td>
<td>پردازش جدول</td>
<td>RTF شامل جدول</td>
<td>محتوای جدول در خروجی</td>
</tr>
<tr>
<td>۸</td>
<td>RTF با کدپیج Windows-1256</td>
<td>RTF فارسی با <code>\ansicpg1256</code></td>
<td>متن فارسی صحیح</td>
</tr>
<tr>
<td>۹</td>
<td>فایل خالی یا خراب</td>
<td>Stream نامعتبر</td>
<td>ErrorMessage تنظیم شود، استثناء پرتاب نشود</td>
</tr>
<tr class="highlight-row">
<td>۱۰</td>
<td>اجرا در Docker/Linux</td>
<td>ایمیج ساخته‌شده</td>
<td>بدون خطای <code>PlatformNotSupportedException</code></td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">معیارهای موفقیت</h4>
<div class="success-box">
<strong>✓ معیار پذیرش نهایی:</strong>
<ul style="padding-right: 20px; margin-top: 10px;">
<li>هر دو MIME Type (<code>application/rtf</code> و <code>text/rtf</code>) به‌درستی پشتیبانی شوند</li>
<li>کدهای کنترلی RTF در خروجی حضور نداشته باشند</li>
<li>کاراکترهای فارسی (با کدپیج Windows-1256) به‌درستی رمزگشایی شوند</li>
<li>متن خروجی تمیز، خوانا و عاری از نویز باشد</li>
<li>هیچ استثنایی به‌صورت Unhandled پرتاب نشود</li>
<li>اجرای موفق در محیط Docker/Linux</li>
</ul>
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>جمع‌بندی نهایی</h3>
<div class="subtitle">Final Summary</div>
</div>
</div>
<div class="section-content">
<p>پیاده‌سازی <code>XRtfFileContentExtractor</code> با استفاده از <strong>RtfPipe</strong>، شکاف موجود در پردازش فایل‌های RTF را به‌طور کامل پر می‌کند. این Extractor با حذف کدهای کنترلی، رمزگشایی کاراکترها و نرمال‌سازی متن، خروجی با کیفیت بالا و مناسب برای موتور LLM فراهم می‌آورد.</p>
<table class="styled-table">
<thead>
<tr>
<th>محور</th>
<th>قبل از پیاده‌سازی</th>
<th>پس از پیاده‌سازی</th>
</tr>
</thead>
<tbody>
<tr>
<td>پشتیبانی از RTF</td>
<td style="color: var(--warning); font-weight: 600;">غیررسمی (متن خام)</td>
<td style="color: var(--success); font-weight: 600;">اختصاصی با RtfPipe</td>
</tr>
<tr>
<td>حذف کدهای کنترلی</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr>
<td>رمزگشایی کاراکترها</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr>
<td>پشتیبانی از کدپیج‌ها</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓ (CodePages)</td>
</tr>
<tr>
<td>Cross-platform</td>
<td style="color: var(--warning); font-weight: 600;">—</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr class="highlight-row">
<td><strong>کیفیت خروجی برای LLM</strong></td>
<td>پایین</td>
<td style="color: var(--success); font-weight: 700; font-size: 15px;">بالا</td>
</tr>
</tbody>
</table>
<div class="conclusion-box">
<h3>🎯 پیشنهاد نهایی</h3>
<p>با توجه به تحلیل‌های انجام‌شده، RtfPipe به‌عنوان کتابخانه اصلی برای پیاده‌سازی XRtfFileContentExtractor انتخاب می‌شود. این انتخاب مبتنی بر Cross-platform بودن، مجوز MIT، بلوغ و پشتیبانی از ویژگی‌های کلیدی RTF است.</p>
<div class="highlight">
<strong>💎 ارزش پیشنهادی:</strong><br>
استخراج متن تمیز از RTF + حذف کامل کدهای کنترلی + پشتیبانی از کدپیج‌های فارسی + Cross-platform + یکپارچگی کامل با معماری موجود
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> در صورت تایید این مستند، آماده‌ام پیاده‌سازی کامل را آغاز نمایم. پیشنهاد می‌شود پس از پیاده‌سازی، تست‌های ۱ تا ۱۰ انجام شود و سپس در محیط Docker/Linux نهایی گردد.
</div>
</div>
</div>
<div class="page-info">
این مستند محرمانه بوده و صرفاً جهت ارائه به کارفرمای محترم تهیه شده است. هرگونه کپی‌برداری یا افشا بدون اجازه کتبی ممنوع است.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">طراحی و توسعه: هادی خزاعی اصل</div>
<div class="footer-divider"></div>
<div>تاریخ تهیه: ۱ آبان ۱۴۰۵ &nbsp;|&nbsp; نسخه: ۱.۰ &nbsp;|&nbsp; طبقه‌بندی: محرمانه</div>
</footer>
</div>
</body>
</html>