Click here to Skip to main content
Click here to Skip to main content

Term frequency/Inverse document frequency implementation in C#

, 28 Oct 2005
Rate this:
Please Sign up or sign in to vote.
Text statistical-based measuring of similarity between two documents in a corpora.

Introduction

This code implements the Term Frequency/Inverse Document frequency (TF-IDF). The TF-IDF is a text statistical-based technique which has been widely used in many search engines and information retrieval systems. I will deal with the documents similarity problem in the next section. To understand the theory, please see this article: Wiki definition of TF/IDF for more details.

Solution

Assume that you have a corpora of 1000 documents and your task is to compute the similarity between two given documents (or a document and a query). The following describes the steps of acquiring the similarity value:

Document pre-processing steps

  • Tokenization: A document is treated as a string (or bag of words), and then partitioned into a list of tokens.
  • Removing stop words: Stop words are frequently occurring, insignificant words. This step eliminates the stop words.
  • Stemming word: This step is the process of conflating tokens to their root form (connection -> connect).

Document representation

  • We generate N-distinct words from the corpora and call them as index terms (or the vocabulary). The document collection is then represented as a N-dimensional vector in term space.

Computing Term weights

  • Term Frequency.
  • Inverse Document Frequency.
  • Compute the TF-IDF weighting.

Measuring similarity between two documents

  • We capture the similarity of two documents using cosine similarity measurement. The cosine similarity is calculated by measuring the cosine of the angle between two document vectors.

Using the code

The main class is TFIDFMeasure. This is the testing code:

void Test (string[] docs, int i, int j)
// docs is collection of parsed documents
{
    StopWordHandler stopWord=new StopWordsHandler() ;
    TFIDFMeasure tf=new TFIDFMeasure(doc) ;
    float simScore=tf.GetSimilarity( i, j);
      // similarity of two given documents at the 
      // position i,j respectively
}

Extension

This library also includes stemming (Martin Porter algorithm), and N-gram text generation modules. If a token-based system did not work as expected, then you can make another choice with N-gram based. Thus, instead of expanding the list of tokens from the document, we will generate a list of N-grams, where N should be a predefined number. That means we will hash into a table to find the counter for the N-gram, but not words (or tokens).

The extra N-gram based similarities (bi, tri, quad...-gram) also help you compare the result of the statistical-based method with the N-gram based method. Let us consider two documents as two flat texts and then run the measurement to compare.

Example of some N-grams for the word "TEXT":

  • uni(1)-gram: T, E, X, T
  • bi(2)-gram: T, TE, EX, XT, T
  • tri(3)-grams: TE, TEX, EXT, XT, T
  • quad(4)-grams: TEX, TEXT, EXT, XT, T

A string of length k, will have k+1 bi-grams, k+1 tri-grams, k+1 quad-grams, and so on.

Point of interest

No complex technique was used, I only utilized the hashtable indexing, and array binary search to solve this problem. The N-gram based text similarity also gives us interesting results.

Articles worth reading

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Thanh Dao
Software Developer
Vietnam Vietnam
I'm still alive...but temporarily moved to work on mobile & web stuffs(j2me/brew/php/flash...something not M$). things have just been very busy, and probably will continue...so don't have chance to maintain & respond. Hope will have time to try to write again, because many ideas with WPF &silver light are waiting. wish me luck Smile | :)
 
FYI:
- MESHSimPack project(c# library for measuring similarity among concepts of the MESH ontology):
http://sourceforge.net/projects/meshsimpack.

Comments and Discussions

 
QuestionVector space PinmemberMember 1052649623-Jan-14 13:01 
AnswerAlternative version of TF*IDF code Pinmemberkbomb98711-Sep-13 10:10 
Questionhelp for run program Pinmembermostafamosakhany15-Jul-13 20:27 
Questiontext categorization Pinmemberarjuncent11-Feb-13 15:43 
QuestionGood program but complex structure Pinmemberfalah ukm14-Apr-12 2:48 
Questionstopword removal issue Pinmembersianiparmaruli24-Jul-11 22:44 
AnswerRe: stopword removal issue Pinmemberfalah ukm7-May-12 3:45 
Questioni have a question pls read this!!!! Pinmemberdyeingbreed8-Jul-11 21:22 
Questionneed help for visual basic coding for tf-idf Pinmemberfifi fadli6-Jul-11 23:42 
GeneralMy vote of 1 Pinmembernaeem_libra24-Oct-10 22:23 
GeneralMy vote of 3 PinmemberMustafa M. Gamal29-Aug-10 19:47 
Question"Term frequency/Inverse document frequency implementation in C#" PinmemberKasunmit9-May-10 16:15 
QuestionHow to run the code Pinmemberaurangzeb khan27-Sep-09 18:48 
GeneralRelevance article PinmemberAdrian Pirvu4-Jun-09 21:13 
Hi,
 
The cosine similarity worth reading and its ussefull, still you may want to read some considerations about IDF in my article:
http://www.codeproject.com/KB/IP/AnatomyOfASearchEngine1.aspx
 
Cheers
QuestionPerformance Pinmembertohi21-May-09 3:57 
QuestionHow to use this code PinmemberNanomom24-Sep-08 21:11 
GeneralSir very thanx for this code at code project but would you like to help me Pinmemberchancomsats21-May-08 9:35 
QuestionStill maintained? PinmemberMarakai27-Dec-07 17:47 
AnswerRe: Still maintained? Pinmemberdaisy0001bc3-Jun-08 0:47 
AnswerRe: Still maintained? PinmemberMarakai3-Jun-08 15:40 
GeneralRe: Still maintained? PinmemberThanh Dao6-Jun-08 4:14 
GeneralRe: Still maintained? PinmemberMarakai15-Jan-09 17:52 
GeneralRe: Still maintained? Pinmemberme_project12-Jan-09 18:49 
GeneralRe: Still maintained? PinmemberMarakai15-Jan-09 17:50 
GeneralRe: Still maintained? Pinmemberme_project18-Jan-09 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 29 Oct 2005
Article Copyright 2005 by Thanh Dao
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid