Programming C#

Term Frequency – Inverse Document Frequency (TF-IDF)

Xây dựng thư viện TfidfTransformer như mộ hình UML như hình sau

tf-idf

Input

Đầu vào là một tập (corpus) các văn bản

Ví dụ

This I Can't is the first document.
This document is the second document.
And this is the third one.
Is this the first document?

string[] corpus = {
    "This I Can't is the first document."
    "This document is the second document."
    "And this is the third one."
    "Is this the first document?"
};

B1. Thực hiện thống kê tần suất từ input đầu vào (2.5 điểm)

Tạo ra ma trân thống kê tần suất như sau

thisIcan’tIsthefirstdocumentsecondandthirdone
document 111111110000
document 210011021000
document 310011000111
document 410011110000

B2. Tính Term Frequency (2.5 điểm)

$$tf(t,d) = \frac{f_{t,d}}{\sum_{t'∈d}{f_{t',d}}}$$

Trong đó ft,d là số lượng của 1 term trong 1 document

thisIcan’tisthefirstdocumentsecondandthirdone
tf(t, d1)$$\frac{1}{7}$$$$\frac{1}{7}$$$$\frac{1}{7}$$$$\frac{1}{7}$$$$\frac{1}{7}$$$$\frac{1}{7}$$$$\frac{1}{7}$$0000
tf(t, d2)$$\frac{1}{6}$$00$$\frac{1}{6}$$$$\frac{1}{6}$$0$$\frac{2}{6}$$$$\frac{1}{6}$$000
tf(t, d3)$$\frac{1}{6}$$00$$\frac{1}{6}$$$$\frac{1}{6}$$000$$\frac{1}{6}$$$$\frac{1}{6}$$$$\frac{1}{6}$$
tf(t, d4)$$\frac{1}{5}$$00$$\frac{1}{5}$$$$\frac{1}{5}$$$$\frac{1}{5}$$$$\frac{1}{5}$$0000

B3. Tính Inverse document frequency (2.5 điểm)

$$idf(t,D) = log \frac{N}{|{d∈D:t∈d}|}$$

Trong đó: N là số lượng document trong corpus N = |D|, {d 𝜖 D: t 𝜖 d} là số lượng document chứa term t

thisIcan’tisthefirstdocumentsecondandthirdone
Idf(t,D)$$log\frac{⁡4}{4}$$$$log\frac{⁡4}{1}$$$$log\frac{⁡4}{1}$$$$log\frac{⁡4}{4}$$$$log\frac{⁡4}{4}$$$$log\frac{⁡4}{2}$$$$log\frac{⁡4}{3}$$$$log\frac{⁡4}{1}$$$$log\frac{⁡4}{1}$$$$log\frac{⁡4}{1}$$$$log\frac{⁡4}{1}$$

B4. Tính Term frequency–Inverse document frequency (2.5 điểm)

$$Tfidf(t,d, D) = tf(t,d) . idf(t,D)$$

thisIcan’tisthefirstdocumentsecondandthirdone
tfidf(t,d1,D)$$\frac{1}{7} log\frac{4}{4}$$$$\frac{1}{7} log\frac{4}{1}$$$$\frac{1}{7} log\frac{4}{1}$$$$\frac{1}{7} log\frac{4}{4}$$$$\frac{1}{7} log\frac{4}{4}$$$$\frac{1}{7} log\frac{4}{2}$$$$\frac{1}{7} log\frac{4}{3}$$0000
tfidf(t,d1,D)$$\frac{1}{6} log\frac{4}{4}$$00$$\frac{1}{6} log\frac{4}{4}$$$$\frac{1}{6} log\frac{4}{4}$$0$$\frac{2}{6} log \frac{⁡4}{3}$$$$\frac{1}{6} log\frac{4}{1}$$000
tfidf(t,d1,D)$$\frac{1}{6} log\frac{4}{4}$$00$$\frac{1}{6} log\frac{4}{4}$$$$\frac{1}{6} log\frac{4}{4}$$000$$\frac{1}{6} log\frac{4}{1}$$$$\frac{1}{6} log\frac{4}{1}$$$$\frac{1}{6}$$
tfidf(t,d1,D)$$\frac{1}{5} log\frac{4}{4}$$00$$\frac{1}{5} log\frac{4}{4}$$$$\frac{1}{5} log\frac{4}{4}$$$$\frac{1}{5} log\frac{4}{2}$$$$\frac{1}{5} log\frac{4}{3}$$0000

Link tham khảo

https://en.wikipedia.org/wiki/Tf%E2%80%93idf