ONLYOFFICEマクロを使用してテキストの見積もり読了時間を計算する方法
読了時間の推定は、テキストエディターにおいて非常に価値のある機能であり、文書を読むのにかかる時間を把握するのに役立ちます。弊社のインターンであるVukasinが、ONLYOFFICEドキュメントエディター内で読了時間を推定する、ユニークで効果的なマクロを作成しました。この記事では、そのマクロを分解し、各関数がどのように動作し、どのように組み合わさって正確な読了時間の推定を実現しているのかを解説します。
マクロの作成
まず、Api.GetDocument() メソッドを使用して文書オブジェクトを取得します:
try {
const document = Api.GetDocument();
定数の定義
// 読了速度計算用の定数
const WORDS_PER_MINUTE = 238; // 平均的な成人の読書速度(1分あたりの単語数)
const COMPLEX_WORD_LENGTH = 7; // 7文字以上の単語は複雑と見なす
ここでは、2つの定数を定義しています:
- WORDS_PER_MINUTE: 成人の平均読書速度(1分あたり238単語)。
- COMPLEX_WORD_LENGTH: 単語が複雑かどうかを判断するための文字数の閾値。
単語のカウント
次に、countWords 関数は、与えられたテキストを空白で区切って単語に分割し、空でない単語の数を数えます:
function countWords(text) {
if (!text) return 0;
return text.split(/\s+/).filter((word) => word.length > 0).length;
}
複雑さの推定
複雑さを推定するために、estimateComplexity 関数を使用します:
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();
// 読了速度計算用の定数
const WORDS_PER_MINUTE = 238; // 平均的な成人の読書速度
const COMPLEX_WORD_LENGTH = 7; // 7文字以上の単語は複雑と見なす
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の無料アカウントを登録する
オンラインでドキュメント、スプレッドシート、スライド、フォーム、PDFファイルの閲覧、編集、共同作業