C++学习之第六天-统计圣经文本的词频

二、编程题--统计圣经出现的单词以及词频。

1、统计一篇英文(The_Holy_Bible.txt)文章中出现的单词和词频,输入:某篇文章的绝对路径输出:词典(词典中的内容为每一行都是一个“单词 词频”)

词典的存储格式如下:


|   a 66          |
|   abandon 77    |
|   public 88     |
|    ......	      |
|_________________|

struct Record
{
	string _word;
	int _frequency;
};

class Dictionary
{
public:
	//......
    void read(const std::string &filename);
    void store(const std::string &filename);
private:
	vector<Record> _dict;
};

提示:因为我们需要统计圣经文件中单词以及该单词在文件中出现的次数,所以可以看去读圣经文件,然后将单词存到数据结构中,并记录单词的次数,如果单词第二次出现的时候,只需要修改单词的次数(也就是这里说的单词的频率),这样当统计完整个圣经文件后,数据都存在数据结构vector了。接着遍历vector数据结构就可以将单词以及单词次数(也就是频率)存储到另外一个文件。(当然如果不存到另外一个文件,就只能打印到终端了)

注意:在读圣经文件的时候,有可能字符串是不合法的,比如:abc123 abc?这样的字符串,处理方式两种:直接不统计这样的字符串或者将非法字母去掉即可。

最终得到结果类似:

a 10

public 20

welcome 30.......

main.cpp

主函数部分:

        1.读取圣经数据,统计单词词频,存入vector

        2.对vector进行排序

        3.遍历或者把结果直接存入文件

#include "Bible.h"

void test01()
{
	Dictionary s1;
	s1.read("bible.txt"); //1.读取圣经数据,统计单词词频,存入vector
	s1.dict_Sort();		//2.对vector中的单词进行排序
	//s1.foreach_vector();//3.遍历词典vector,把结果输出到控制台
	s1.store("my_dict.txt&#
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
简单处理过的圣经文本,需要可以下载ThebookofthegenerationofJesusChristthesonofDavidthesonofAbrahamAbrahambegatIsaacandIsaacbegatJacobandJacobbegatJudasandhisbrethrenAndJudasbegatPharesandZaraofThamarandPharesbegatEsromandEsrombegatAramAndArambegatAminadabandAminadabbegatNaassonandNaassonbegatSalmonAndSalmonbegatBoozofRachabandBoozbegatObedofRuthandObedbegatJesseAndJessebegatDavidthekingandDavidthekingbegatSolomonofherthathadbeenthewifeofUriasAndSolomonbegatRoboamandRoboambegatAbiaandAbiabegatAsaAndAsabegatJosaphatandJosaphatbegatJoramandJorambegatOziasAndOziasbegatJoathamandJoathambegatAchazandAchazbegatEzekiasAndEzekiasbegatManassesandManassesbegatAmonandAmonbegatJosiasAndJosiasbegatJechoniasandhisbrethrenaboutthetimetheywerecarriedawaytoBabylonAndaftertheywerebroughttoBabylonJechoniasbegatSalathielandSalathielbegatZorobabelAndZorobabelbegatAbiudandAbiudbegatEliakimandEliakimbegatAzorAndAzorbegatSadocandSadocbegatAchimandAchimbegatEliudAndEliudbegatEleazarandEleazarbegatMatthanandMatthanbegatJacobAndJacobbegatJosephthehusbandofMaryofwhomwasbornJesuswhoiscalledChristSoallthegenerationsfromAbrahamtoDavidarefourteengenerationsandfromDaviduntilthecarryingawayintoBabylonarefourteengenerationsandfromthecarryingawayintoBabylonuntoChristarefourteengenerationsNowthebirthofJesusChristwasonthiswiseWhenashismotherMarywasespousedtoJosephbeforetheycametogethershewasfoundwithchildoftheHolyGhostThenJosephherhusbandbeingajustmanandnotwillingtomakeherapublickexamplewasmindedtoputherawayprivilyButwhilehethoughtonthesethingsbeholdtheangeloftheLORDappeareduntohiminadreamsayingJosephthousonofDavidfearnottotakeuntotheeMarythywifeforthatwhichisconceivedinherisoftheHolyGhostAndsheshallbringforthasonandthoushaltcallhisnameJESUSforheshallsavehispeoplefromtheirsinsNowallthiswasdonethatitmightbefulfilledwhichwasspokenoftheLordbytheprophetsayingBeholdavirginshallbewithchildandshallbringforthasonandtheyshallcallhisnameEmmanuelwhichbeinginterpretedisGodwithusThenJosephbeingraisedfromsleepdidastheangeloftheLordhadbiddenhimandtookuntohimhiswifeAndknewhernottillshehadbroughtforthherfirstbornsonandhecalledhisnameJESUSMatthewNowwhenJesuswasborninBethlehemofJudaeainthedaysofHerodthekingbeholdtherecamewisemenfromtheeasttoJerusalemSayingWhereishethatisbornKingoftheJewsforwehaveseenhisstarintheeastandarecometoworshiphimWhenHerodthekinghadheardthesethingshewastroubledandallJerusalemwithhimAndwhenhehadgatheredallthechiefpriestsandscribesofthepeopletogetherhedemandedofthemwhereChristshouldbebornAndtheysaiduntohimInBethlehemofJudaeaforthusitiswrittenbytheprophetAndthouBethleheminthelandofJudaartnottheleastamongtheprincesofJudaforoutoftheeshallcomeaGovernorthatshallrulemypeopleIsraelThenHerodwhenhehadprivilycalledthewisemenenquiredofthemdiligentlywhattimethestarappearedAndhesentthemtoBethlehemandsaidGoandsearchdiligentlyfortheyoungchildandwhenyehavefoundhimbringmewordagainthatImaycomeandworshiphimalsoWhentheyhadheardthekingtheydepartedandlothestarwhichtheysawintheeastwentbeforethemtillitcameandstoodoverwheretheyoungchildwasWhentheysawthestartheyrejoicedwithexceedinggreatjoyAndwhentheywerecomeintothehousetheysawtheyoungchildwithMaryhismotherandfelldownandworshippedhimandwhentheyhadopenedtheirtreasurestheypresenteduntohimgiftsgoldandfrankincenseandmyrrhAndbeingwarnedofGodinadreamthattheyshouldnotreturntoHerodtheydepartedintotheirowncountryanotherwayAndwhentheyweredepartedbeholdtheangeloftheLordappearethtoJosephinadreamsayingAriseandtaketheyoungchildandhismotherandfleeintoEgyptandbethouthereuntilIbringtheewordforHerodwillseektheyoungchildtodestroyhimWhenhearosehetooktheyoungchildandhismotherbynightanddepartedintoEgyptAndwasthereuntilthedeathofHerodthatitmightbefulfilledwhichwasspokenoftheLordbytheprophetsayingOutofEgypthaveIcalledmysonThenHerodwhenhesawthathewasmockedofthewisemenwasexceedingwrothandsentforthandslewallthechildrenthatwereinBethlehemandinallthecoaststhereoffromtwoyearsoldandunderaccordingtothetimewhichhehaddiligentlyenquiredofthewisemenThenwasfulfilledthatwhichwasspokenbyJeremytheprophetsayingInRamawasthereavoiceheardlamentationandweepingandgreatmourningRachelweepingforherchildrenandwouldnotbecomfortedbecausetheyarenotButwhenHerodwasdeadbeholdanangeloftheLordappearethinadreamtoJosephinEgyptSayingAriseandtaketheyoungchildandhismotherandgointothelandofIsraelfortheyaredeadwhichsoughttheyoungchildslifeAndhearoseandtooktheyoungchildandhismotherandcameintothelandofIsraelButwhenheheardthatArchelausdidreigninJudaeaintheroomofhisfatherHerodhewasafraidtogothithernotwithstandingbeingwarnedofGodinadreamheturnedasideintothepartsofGalileeAndhecameanddweltinacitycalledNazareththatitmightbefulfilledwhichwasspokenbytheprophetsHeshallbecalledaNazareneMatthewInthosedayscameJohntheBaptistpreachinginthewildernessofJudaeaAndsayingRepentyeforthekingdomofheavenisathandForthisishethatwasspokenofbytheprophetEsaiassayingThevoiceofonecryinginthewildernessPrepareyethewayoftheLordmakehispathsstraightAndthesameJohnhadhisraimentofcamelshairandaleatherngirdleabouthisloinsandhismeatwaslocust

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给你。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值