Googlechromeの拡張機能を製作中スタック トレース(無名関数) が永久に出てくる

実現したいこと

プログラムからスタックトレース(無名関数)が出ることを防ぎたい。

発生している問題・分からないこと

Googlechromeの拡張機能として実行すると スタック トレース
content.js:130 (無名関数) とで続ける

エラーメッセージ

error

1スタック トレース 2content.js:130 (無名関数)

該当のソースコード

// テキストを前処理する関数 function preprocess(text) { return text.toLowerCase(); } // テキスト類似性を計算するための TF-IDF ベクトル化クラス class TfidfVectorizer { constructor(documents) { this.vocabulary = {}; this.idf = {}; this.documents = documents; this.word_set = new Set(); } fit_transform(docs) { this._build_vocabulary(docs); const tfidf = docs.map(doc => this._tfidf(doc)); return tfidf; } _build_vocabulary(docs) { docs.forEach(doc => this._addWordsToSet(doc)); let index = 0; this.word_set.forEach(word => { this.vocabulary[word] = index++; }); } _addWordsToSet(doc) { const addWord = word => this.word_set.add(word); doc.split(' ').forEach(word => addWord(word)); } _tfidf(doc) { const tf = this._term_frequency(doc); const tfidf = {}; for (let term in tf) { tfidf[term] = tf[term] * this._inverse_document_frequency(term); } return tfidf; } _term_frequency(doc) { const term_counts = {}; const incrementTermCount = term => { term_counts[term] = (term_counts[term] || 0) + 1; }; const terms = doc.split(' '); terms.forEach(term => incrementTermCount(term)); const tf = {}; for (let term in term_counts) { tf[term] = term_counts[term] / terms.length; } return tf; } _inverse_document_frequency(term) { if (!this.idf[term]) { let doc_count = 0; const incrementDocCount = doc => { if (doc.split(' ').includes(term)) { doc_count++; } }; this.documents.forEach(doc => incrementDocCount(doc)); this.idf[term] = Math.log(this.documents.length / (1 + doc_count)); } return this.idf[term]; } cosine_similarity(vecA, vecB) { const dotProduct = this._dotProduct(vecA, vecB); const magnitudeA = Math.sqrt(this._magnitude(vecA)); const magnitudeB = Math.sqrt(this._magnitude(vecB)); return dotProduct / (magnitudeA * magnitudeB); } _dotProduct(vecA, vecB) { return vecA.reduce((sum, a, i) => this._sumProduct(sum, a, vecB[i]), 0); } _sumProduct(sum, a, b) { return sum + a * b; } _magnitude(vec) { return vec.reduce((sum, a) => this._sumSquare(sum, a), 0); } _sumSquare(sum, a) { return sum + a * a; } } // ページ内のテキストを取得する関数 function getPageText() { return document.body.innerText; } // テキストの類似性を計算し、ハイライトする関数 function highlightSimilarText(doc) { const vectorizer = new TfidfVectorizer(documents); const threshold = 0.5; // 類似度の閾値 const pageText = preprocess(getPageText()); const docText = preprocess(doc); const vectors = vectorizer.fit_transform([pageText, docText]); const similarity = vectorizer.cosine_similarity(vectors[0], vectors[1]); if (similarity > threshold) { highlightText(docText); } } function highlightText(docText) { const regex = createRegex(docText); document.body.innerHTML = document.body.innerHTML.replace(regex, '<span style="background-color: yellow;">$1</span>'); } function createRegex(docText) { return new RegExp(`(${docText})`, 'gi'); } // 名前付き関数に変更 function processDocuments(documents) { for (let i = 0; i < documents.length; i++) { highlightSimilarText(documents[i]); } } // 名前付き関数で処理 processDocuments(documents);

試したこと・調べたこと

上記の詳細・結果

GPT君に聞いたが同じコードを繰り返し吐き続ける。

補足

特になし

コメントを投稿

0 コメント