Word Frequency statistics

Source: Internet
Author: User

Function: Statistic the number and frequency of words in an English article;

Implementation: is written in C #, using a relatively stupid cycle comparison and counting to achieve;

Https:https://git.coding.net/li_yuhuan/wordfrequency.git

Ssh:[email Protected]:li_yuhuan/wordfrequency.git

Code:

Class Program
{


static void Main (string[] args)
{


Console.WriteLine ("Please copy the article to be counted into the Test1.txt file in the debug directory, press any key to continue");
Console.readkey ();
Frequency ();


}

static private void Frequency ()
{


list<string> composition = new list<string> ();//An array of words in the article, including repeated words
List<string> Word = new list<string> ();//An array of words in the article, including repeated words
list<string> Newword = new list<string> ();//The word array in the article, excluding duplicate words
list<int> count = new list<int> ();//Count the number of each word in the article

int n = 0; The total number of words in the file includes duplicate words
Char[] split = {', ', ', '? ', '? ‘, ‘.‘, ‘。 ‘, ‘-‘, ‘—‘, ‘"‘ };
string text = System.IO.File.ReadAllText (@ "Test1.txt");
Composition. AddRange (text. Split (split));

foreach (string compo in composition)
{
if (compo! = "" "" && compo! = null)
{
Word. ADD (compo. ToLower ()); Convert all the words to lowercase letters
}

}

Newword. ADD (Word[0]);
n = Word. Count;
Count. ADD (1);

for (int i = 1; i < n; i++)//Starting with the second word counting
{
int j = 0;

for (j = 0; J < Newword. Count; J + +)//compare with previous words to see if there is the same
{
if (word[i] = = Newword[j])
{
count[j]++;
Break Find the same word as before and end this comparison
}

}

if (j = = Newword. Count)//does not have the same word as the previous
{
Newword. ADD (Word[i]);
Count. ADD (1);
}

}

Console.WriteLine ("Statistical results: This article has a total of" + Newword. Count + "Different words (detailed statistics are shown in the table below)");

for (int t = 0; t < Newword. Count; t++)
{
Console.WriteLine (Newword[t] + ":" + count[t]. ToString ());
}

Console.WriteLine ("Statistics end, press any key to exit!") ");
Console.readkey ();


}


}

Examples of running results:

Word Frequency statistics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.