C語言每日練習(xí)之統(tǒng)計(jì)文本單詞數(shù)及高頻詞
作業(yè)1:統(tǒng)計(jì)出txt文本里面的單詞數(shù),并找出頻率出現(xiàn)最高的單詞是哪個(gè)?
運(yùn)行結(jié)果:
上代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //文件打開 //string file = System.IO.File.ReadAllLines(@""); int count = 0; string tmp = ""; //初始化次數(shù) string words = "hihi hello,hihi,hello,hihi?"; var new_i = words.Split(new char[] { ' ', ',', '.', '?' },StringSplitOptions.RemoveEmptyEntries); Console.Write("總的單詞數(shù)量:{0}\n", new_i.Length); for (int i = 0; i < new_i.Length; i++) { //查詢每個(gè)單詞出現(xiàn)的次數(shù) var query = from key in new_i where key.ToUpperInvariant() == new_i[i].ToUpperInvariant() select key; int key_count = query.Count(); if (key_count > count) { count = key_count; tmp = new_i[i]; } } Console.Write("頻率出現(xiàn)最高的單詞是:{0}\n", tmp); Console.Write("次數(shù)為:{0}\n", count); Console.ReadKey(); } } }
基礎(chǔ)代碼運(yùn)行成功!通過外部打開!
創(chuàng)建11.txt
通過外部打開:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //文件打開 //string[] file_A = System.IO.File.ReadAllLines(@"C:\Users\Administrator\Desktop\11.txt"); string file_A = System.IO.File.ReadAllText(@"C:\Users\Administrator\Desktop\11.txt"); //Console.Write(file_A); int count = 0; string tmp = ""; //初始化次數(shù) var new_i = file_A.Split(new char[] { ' ', ',', '.', '?' }, StringSplitOptions.RemoveEmptyEntries); Console.Write("總的單詞數(shù)量:{0}\n", new_i.Length); for (int i = 0; i < new_i.Length; i++) { //查詢每個(gè)單詞出現(xiàn)的次數(shù) var query = from key in new_i where key.ToUpperInvariant() == new_i[i].ToUpperInvariant() select key; int key_count = query.Count(); if (key_count > count) { count = key_count; tmp = new_i[i]; } } Console.Write("頻率出現(xiàn)最高的單詞是:{0}\n", tmp); Console.Write("次數(shù)為:{0}\n", count); Console.ReadKey(); } } }
運(yùn)行截圖:
到此這篇關(guān)于C語言每日練習(xí)之統(tǒng)計(jì)文本單詞數(shù)及高頻詞的文章就介紹到這了,更多相關(guān)C語言統(tǒng)計(jì)文本單詞數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV邊緣提取算法流程的實(shí)現(xiàn)(附DEMO)
本文主要介紹了OpenCV邊緣提取算法流程的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08C語言內(nèi)嵌匯編API內(nèi)存搜索引擎實(shí)例
這篇文章主要介紹了C語言內(nèi)嵌匯編API內(nèi)存搜索引擎實(shí)例,涉及匯編語言與內(nèi)存相關(guān)操作,需要的朋友可以參考下2014-10-10C++解決大數(shù)組棧內(nèi)存不夠問題的方法分析
這篇文章主要介紹了C++解決大數(shù)組棧內(nèi)存不夠問題的方法,結(jié)合實(shí)例形式對(duì)比分析了C++針對(duì)大數(shù)組棧內(nèi)存不足情況的常見解決方法及其優(yōu)缺點(diǎn),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05c語言動(dòng)態(tài)內(nèi)存分配知識(shí)點(diǎn)及實(shí)例
在本篇文章里小編給大家整理的是關(guān)于c語言動(dòng)態(tài)內(nèi)存分配知識(shí)點(diǎn)及實(shí)例,需要的朋友們可以學(xué)習(xí)下。2020-03-03