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