احسبوا وقت قراءة نصوصكم بسهولة باستخدام ماكرو في “ONLYOFFICE”
تقدير وقت القراءة من الميزات المفيدة في محرري النصوص، إذ يساعدكم على معرفة المدة التي قد تستغرقها قراءة مستندكم. ابتكر متدربنا فوكاشين ماكرو بسيطًا وفعالًا لحساب وقت القراءة التقديري داخل محرر مستندات “ONLYOFFICE”. في هذا المقال، سنستعرض كيفية عمل هذا الماكرو، ونوضح دور كل وظيفة فيه للوصول إلى تقدير دقيق لوقت القراءة.
إنشاء الماكرو
أولًا، نستدعي كائن المستند باستخدام الطريقة “Api.GetDocument()”:
try {
const document = Api.GetDocument();
تعريف الثوابت
// Constants for reading speed calculations
const WORDS_PER_MINUTE = 238; // Average adult reading speed
const COMPLEX_WORD_LENGTH = 7; // Words with this many chars or more are considered complex
يتم هنا تحديد ثابتين رئيسيين:
- “WORDS_PER_MINUTE”: متوسط سرعة القراءة للشخص البالغ، والتي تبلغ 238 كلمة في الدقيقة.
- “COMPLEX_WORD_LENGTH”: حدٌّ يُستخدم لتحديد ما إذا كانت الكلمة معقدة.
حساب عدد الكلمات
تعمل دالة “countWords” على تقسيم النص إلى كلمات استنادًا إلى المسافات البيضاء، ثم تحسب عدد الكلمات غير الفارغة.
function countWords(text) {
if (!text) return 0;
return text.split(/\s+/).filter((word) => word.length > 0).length;
}
تقدير تعقيد النص
لتقدير مستوى التعقيد، نستخدم الدالة “estimatedComplexity”:
function estimateComplexity(text) {
if (!text) return 0;
const words = text.split(/\s+/).filter((word) => word.length > 0);
if (words.length === 0) return 0;
const complexWords = words.filter(
(word) => word.length >= COMPLEX_WORD_LENGTH
).length;
return complexWords / words.length;
}
تعمل هذه الدالة على قياس تعقيد النص من خلال:
- تقسيمه إلى كلمات.
- استبعاد الكلمات التي يقل طولها عن 7 أحرف.
- حساب نسبة الكلمات المعقدة إلى إجمالي عدد الكلمات.
استرداد الفقرات ومعالجتها
بعد ذلك، نتابع معالجة النص من خلال:
const paragraphs = document.GetAllParagraphs();
let totalWords = 0;
let totalText = "";
paragraphs.forEach((paragraph) => {
const text = paragraph.GetText();
totalWords += countWords(text);
totalText += text + " ";
});
في هذه المرحلة، نقوم بما يلي:
- استرداد جميع الفقرات من المستند.
- المرور على كل فقرة لاستخراج النص منها.
- حساب عدد الكلمات في كل فقرة وتجميع العدد الإجمالي.
- تخزين النص بالكامل لتقدير مستوى التعقيد.
تعديل القراءة بناءً على مستوى التعقيد
يتم هنا استخدام عامل التعقيد لتعديل سرعة القراءة، حيث يؤدي ارتفاع مستوى التعقيد إلى تقليل السرعة الفعلية للقراءة.
const complexityFactor = estimateComplexity(totalText);
const complexityAdjustment = 1 - complexityFactor * 0.3;
const effectiveWPM = WORDS_PER_MINUTE * complexityAdjustment;
حساب وقت القراءة
بعد ذلك، نحول إجمالي عدد الكلمات إلى دقائق، ثم إلى ساعات إذا لزم الأمر.
const readingTimeMinutes = totalWords / effectiveWPM;
const readingTimeHours = readingTimeMinutes / 60;
تنسيق النتيجة
نقوم بعد ذلك بتنسيق وقت القراءة بناءً على القيم المحسوبة.
let readingTimeText;
if (readingTimeMinutes < 1) {
readingTimeText = `less than 1 minute`;
} else if (readingTimeMinutes < 60) {
readingTimeText = `${Math.ceil(readingTimeMinutes)} minute${Math.ceil(readingTimeMinutes) !== 1 ? "s" : ""
}`;
} else {
const hours = Math.floor(readingTimeHours);
const remainingMinutes = Math.ceil((readingTimeHours - hours) * 60);
readingTimeText = `${hours} hour${hours !== 1 ? "s" : ""}${remainingMinutes > 0
? ` and ${remainingMinutes} minute${remainingMinutes !== 1 ? "s" : ""
}`
: ""
}`;
}
إدراج النتيجة في المستند
بعد ذلك، نقوم بإدراج النتيجة المنسقة في المستند:
const infoText = `Reading Time: ${readingTimeText} (${totalWords} words at ${Math.round(
effectiveWPM
)} words per minute)`;
const oParagraph = Api.CreateParagraph();
oParagraph.AddText(infoText);
oParagraph.SetBold(true);
oParagraph.SetItalic(true);
oParagraph.SetFontFamily("Arial");
document.InsertContent([oParagraph], 0);
} catch (error) {
console.log("Error: " + error.message);
}
})();
في هذه المرحلة:
- يتم إنشاء نص منسق يعرض تفاصيل وقت القراءة.
- يُضاف النص إلى فقرة جديدة داخل المستند.
- تُطبق بعض التنسيقات مثل الخط العريض والمائل وخط “Arial”.
- تُدرج الفقرة في بداية المستند.
وفيما يلي كود الماكرو بالكامل:
(function () {
try {
const document = Api.GetDocument();
// Constants for reading speed calculations
const WORDS_PER_MINUTE = 238; // Average adult reading speed
const COMPLEX_WORD_LENGTH = 7; // Words with this many chars or more are considered complex
function countWords(text) {
if (!text) return 0;
return text.split(/\s+/).filter((word) => word.length > 0).length;
}
function estimateComplexity(text) {
if (!text) return 0;
const words = text.split(/\s+/).filter((word) => word.length > 0);
if (words.length === 0) return 0;
const complexWords = words.filter(
(word) => word.length >= COMPLEX_WORD_LENGTH
).length;
return complexWords / words.length;
}
const paragraphs = document.GetAllParagraphs();
let totalWords = 0;
let totalText = "";
paragraphs.forEach((paragraph) => {
const text = paragraph.GetText();
totalWords += countWords(text);
totalText += text + " ";
});
const complexityFactor = estimateComplexity(totalText);
const complexityAdjustment = 1 - complexityFactor * 0.3;
const effectiveWPM = WORDS_PER_MINUTE * complexityAdjustment;
const readingTimeMinutes = totalWords / effectiveWPM;
const readingTimeHours = readingTimeMinutes / 60;
let readingTimeText;
if (readingTimeMinutes < 1) {
readingTimeText = `less than 1 minute`;
} else if (readingTimeMinutes < 60) {
readingTimeText = `${Math.ceil(readingTimeMinutes)} minute${Math.ceil(readingTimeMinutes) !== 1 ? "s" : ""
}`;
} else {
const hours = Math.floor(readingTimeHours);
const remainingMinutes = Math.ceil((readingTimeHours - hours) * 60);
readingTimeText = `${hours} hour${hours !== 1 ? "s" : ""}${remainingMinutes > 0
? ` and ${remainingMinutes} minute${remainingMinutes !== 1 ? "s" : ""
}`
: ""
}`;
}
const infoText = `Reading Time: ${readingTimeText} (${totalWords} words at ${Math.round(
effectiveWPM
)} words per minute)`;
const oParagraph = Api.CreateParagraph();
oParagraph.AddText(infoText);
oParagraph.SetBold(true);
oParagraph.SetItalic(true);
oParagraph.SetFontFamily("Arial");
document.InsertContent([oParagraph], 0);
} catch (error) {
console.log("Error: " + error.message);
}
})();
يقدم هذا الماكرو طريقة ذكية لتقدير وقت القراءة في “ONLYOFFICE”، ونأمل أن يكون إضافة مفيدة لأدواتكم. تتيح ماكروهات “ONLYOFFICE” توسيع الوظائف الافتراضية بشكل كبير وإنشاء سكريبتات مخصصة تناسب احتياجاتكم. ندعوكم لاستكشاف مكتبة أساليب “API” وتجربة تطوير ماكروهاتكم الخاصة. إذا كانت لديكم أي أسئلة أو أفكار، لا تترددوا في التواصل معنا! نحن دائمًا منفتحون على الاقتراحات والتعاون. نتمنى لكم تجربة إبداعية موفقة!
عن مؤلف الماكرو
ONLYOFFICE ١. أنشئ حسابك المجاني من
،٢. قم بعرض و تحرير أو التعاون على المستندات، الجداول ، العروض التقديمية