C &&Linux 下簡單實現單詞統計,c語言

來源:互聯網
上載者:User

C &&Linux 下簡單實現單詞統計,c語言

/*

功能實現:

從文字文件裡讀取英文單詞,可能含有中文字元,

實現英文單詞,中文字元的數目統計

 

Author :賀榮偉

creat Time: 16:01 2015/7/10 星期五

 

#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 方法:char *fgets(char *string, int n, FILE *stream)      從檔案stream中讀取n-1個字元/一行(若一行不滿n-1個),string接收      字串      如果n <= 0,返回NULL      如果n == 1,返回" ",也就是一個空串      如果成功,傳回值等於string, 也就是獲得字串的首地址    //如果出錯,或者讀到FILE的結尾,返回NULL.     FILE *textfile=fopen("text.txt","r");///開啟一個檔案    while(fgets(str,str_len,textfile)!=NULL)    {        str[strlen(str)-1]='\0';        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");       / *開啟檔案 */    while((fp==NULL))                /*開啟失敗 */    {        puts("text.txt open failure");        exit(0);    }    while(fgets(str,str_len,fp)!=NULL) /*fgets 的定向輸入,擷取輸入資料流的長度 */    {        length=strlen(str);    }    fclose(fp);    for(int i=0; i<length; ++i)       /*判斷單詞 */    {        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("單詞個數為: %d 個\n",word_count);} void is_hanzi(){    int ch,hanzi_count=0;    FILE *fp=fopen("text.txt","r"); /*開啟檔案 */    while((fp==NULL))    {        puts("text.txt open failure"); /*開啟失敗 */        exit(0);    }    while(!feof(fp))    {        ch=fgetc(fp);                      ///ASCII 最大127  /*判斷漢字 */        if(ch>127) {            fgetc(fp);            hanzi_count++;        }    }    fclose(fp);    printf("中文字元為: %d 個\n",hanzi_count);}int main(){    is_word();  /*調用 is_word() 函數*/    is_hanzi(); /*調用 is_hanzi() 函數*/    return 0;}

測試:

 

 

Linux系統命令列統計文本單詞個數及出現頻率:

參數:wc  -w,  text.txt
列印單詞數(print the word counts)

測試:

常值內容:

We are two boy

結果 4

統計單詞出現頻率:

這個剛學shell,有些不懂,尋找書籍資料和網上資源,知道是這樣這樣寫:

命令列:

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

簡單分析:cat text.txt:表示建立一個檔案。

          |:表示重新導向,即把上一個命令的結果傳遞給下一個命令。

          tr 命令:tr是transform的縮寫,該命令是著名的流處理命令sed的簡化版,也是用來對文檔進行轉換的。

          tr -cs “[a-z][A-Z]" "\n"  -c表示取“[a-z][A-Z]"的補集(complement),-s 表示把連續的匹配壓縮成一個”\012“,*號表示將集合2中的字元補全,與集合1的字元長度一致,所以整個命令就是把除了字母外的其他字元一律壓縮成分行符號,如果有連續的匹配,則只轉換成一個分行符號。

tr A-Z a-z 把大寫統一轉換成小寫。

sort 排序 按字母順序

uniq 去重 該命令必須對排序好的文檔進行,-c 表示列印出字母的重複次數

然後再次 sort ,這次sort比較複雜,因為在uniq命令後 輸出結果已經變成了 如下形式:

n word (單詞的重複次數+空格+單詞)

所以 -k1nr表示對第一列(-k1)的數字形式(-n)的變數進行逆序(-r 從大到小)排列 , -k2表示在前面的排序基礎上對重複次數一致的單詞進行按字母順序的排列。

最後是head -n$1,表示只顯示結果的前$1行。

常值內容:

We are to boy

結果:

1 are

1 boy

1 to

1 we(均出現一次)

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.