C &&linux Simple implementation of Word statistics

Source: Internet
Author: User

/*

Feature implementation:

Reading English words from a text document may contain Chinese characters,

Implement the number of Chinese characters in English words

Author: He Rongwei

creat time:16:01 2015/7/10 Friday

#include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include < ctype.h> const int Str_len=1010;char str[str_len];bool Vis[str_len]; typedef long Long Ll;typedef unsigned long long LLU; int word_count,hanzi_count; void Is_word (); void Is_hanzi ();      /*//fgets method: Char *fgets (char *string, int n, file *stream) reads n-1 characters/line from the file stream (if one row is dissatisfied with n-1), string receives the strings     If n <= 0, return null if n = = 1, return "", that is, an empty string if successful, the return value is equal to string, that is, the first address of the string is obtained//if an error occurs, or read to the end of file, return null. File *textfile=fopen ("Text.txt", "R"),/////Open a document while (Fgets (str,str_len,textfile)!=null) {Str[strlen (str) -1]=        ' N ';        printf ("%s \ n", str);    printf ("The text size is%d\n", strlen (str));    } fclose (Textfile); */void Is_word () {char ch;    BOOL flag=0;    int word_count=0,length=0;       FILE *fp=fopen ("Text.txt", "R");        /* Open File */while ((Fp==null))/* Open failed */{puts ("Text.txt open Failure"); Exit0);    } while (Fgets (STR,STR_LEN,FP)!=null)/*fgets The directed input, gets the length of the input stream */{Length=strlen (str);    } fclose (FP);            for (int i=0; i<length; ++i)/* Determine the word */{if (Isalpha (Str[i]) &&flag==0) {word_count++;        flag=1;        } if (!isalpha (Str[i])) {flag=0;        }}/* while (!feof (FP)) {ch=fgetc (FP); if (Isalpha (CH)) if (ch>= ' a ' &&ch<= ' Z ' | |        ch>= ' A ' &&ch<= ' Z ') {flag==0;            } else if ((flag==0) && (ch!= '-' &&ch!= '/' &&ch!= ') ') {word_count++;        flag=1;    }} fclose (FP); */printf ("Number of words:%d \ n", Word_count);}    void Is_hanzi () {int ch,hanzi_count=0; FILE *fp=fopen ("Text.txt", "R");    /* Open File */while ((Fp==null)) {puts ("Text.txt open Failure");/* Open failed */exit (0);                      } while (!feof (FP)) {ch=fgetc (FP);    ASCII Maximum 127/* Kanji */    if (ch>127) {fgetc (FP);        hanzi_count++;    }} fclose (FP); printf ("Chinese characters:%d \ n", Hanzi_count);}  int main () {Is_word (); /* Call Is_word () function */Is_hanzi (); /* Call the Is_hanzi () function */return 0;}

Test:

Linux system command line Statistics text Word number and occurrence frequency:

Parameters: Wc-w, Text.txt
Print the number of words (print the word counts)

Test:

Text content:

We are both boy

Results 4

Statistical word occurrence Frequency:

This just learned the shell, some do not understand, find books and online resources, know that is so written:

Command line:

Cat text.txt |tr-cs "[A-z][a-z]" "[\012*]" |tr A-Z a-z|sort|uniq-c|sort-k1nr-k2|head-10

Simple analysis: Cat Text.txt: Represents the creation of a file.

|: Represents a redirect that passes the result of the previous command to the next command.

TR command: TR is the abbreviation of transform, which is a simplified version of the famous Stream Processing command sed and is also used to convert the document.

Tr-cs "[a-z][a-z]" "\ n"-C means to take "[a-z][a-z]" complement set (complement),-s means to compress a continuous match into a "\012", the * number indicates that the character in set 2 is complete, and the character length of the set 1 is consistent, So the whole command is to compress all characters except the letter into a newline character, and if there is a continuous match, it is converted to a newline character only.

TR A-Z converts uppercase unification to lowercase.

Sort by alphabetical order

Uniq the command must be done with a sorted document,-C indicates the number of repetitions of the printed letter

Then sort again, this time the sort is more complicated, because the output after the Uniq command has become the following form:

N Word (number of repetitions + spaces + words)

So-K1NR indicates that the variable in the number form (-N) of the first column (-K1) is sorted in reverse order (-R from large to small), and-K2 indicates that the repeating number of words is arranged alphabetically based on the previous sort.

The last is head-n$1, which indicates that only the first rows of the result are displayed.

Text content:

We is to Boy

Results:

1 is

1 boy

1 to

1 We (both appear once)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C &&linux Simple implementation of Word statistics

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.