10 high-frequency word filtering and 10-Frequency Word Filtering

Source: Internet
Author: User

10 high-frequency word filtering and 10-Frequency Word Filtering

// # Include <iostream. h> # include <string. h> # include <fstream. h> struct Word // define a structure array to store words {char w [20]; int num;} Word [10000]; void main () // main function {ifstream infile ("test1.txt", ios: in); char a, t [20]; int n; int wordTypeNum = 0, wordNum = 0; int I; int j; int ijt; for (I = 0; I <10000; I ++) // assign the initial value 0 Word [I] to the number of frequencies in the structure array num. num = 0; strcpy (Word [0]. w, "wow \ 0"); // It is convenient for future programming. Assign the first structure member w a word that is definitely not a word if (! Infile) // determine whether the File is opened successfully. Enable cerr <"File open error" <endl; else {while (infile. get (a) // read each character {if (a = ''| a = ',' | a = ', '| a = '! '| A = '? '| A =' "'| a =': '| a ='; ') // These are word separators, incomplete {t [wordNum] = '\ 0'; // word end for (I = 0; I <= wordTypeNum; I ++) // compare it with the previously stored words {if (strcmp (t, Word [I]. w) = 0) // find the same Word and add num to 1 {Word [I]. num ++; wordNum = 0 ;}} if (strcmp (t, Word [I]. w )! = 0) // the same Word is not found. New words are stored in the new structure {wordTypeNum ++; strcpy (Word [wordTypeNum]. w, t); Word [wordTypeNum]. num ++; wordNum = 0 ;}} else // character composition word {t [wordNum] = a; wordNum ++ ;}}/* cout <endl; for (I = 1; I <= wordTypeNum; I ++) cout <Word [I]. w <"" <Word [I]. num <endl; */for (I = 1; I <wordTypeNum; I ++) // sort the structure array in ascending order of num members {for (j = 1; j <wordTypeNum-I; j ++) {if (Word [j]. num <Word [j + 1]. num) {ijt = Word [j]. num; Word [j]. num = Word [j + 1]. num; Word [j + 1]. num = ijt ;}}cout <"the top 10 words with the highest frequency are as follows:" <endl; for (I = 1; I <= 10; I ++) // output the first 10 {cout <Word [I]. w <"" <Word [I]. num <endl;} infile. close ();}}

  

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.