一個文本分詞程序

?WordMap類從分詞庫中讀入分詞?

將分詞存入unordered_map<std::string, int> 中

#pragma once
#include<istream>
#include<unordered_map>
#include<string>
#include<ctime>
class WordMap {
public:WordMap(const std::string& filename);~WordMap();bool init();std::unordered_map<std::string, int> m_map;std::string m_filename;private:time_t difftime;std::string timestr( tm*);
};
#include"wordmap.h"
#include<fstream>
#include<iostream>
#include<sstream>
#include<ctime>
WordMap::WordMap(const std::string& filename):m_filename(filename), difftime(5)
{
}WordMap::~WordMap() {}bool WordMap::init()
{std::ifstream input(m_filename);std::istringstream inputstring;time_t last;time(&last);time_t cur;if (input.is_open()){std::string inputs;tm nowtime; localtime_s(&nowtime,&last);std::cout << "開始初始化分詞庫,當前時間" << timestr(&nowtime)<<std::endl;while (std::getline(input, inputs)){time(&cur);std::istringstream inputstring(inputs);int num;int num2;std::string word;inputstring >> num;inputstring >> word;inputstring >> num2;m_map[word] = num2;if (cur - last > difftime){std::cout << "已初始化分詞個數:" << m_map.size() << std::endl;last = cur;}}time(&cur);localtime_s(&nowtime, &cur);std::cout << "結束初始化分詞庫,當前時間" << timestr(&nowtime) << std::endl;}else{std::cerr << "can't not open file:" << m_filename;return false;}return true;
}std::string WordMap::timestr(tm* nowtime)
{std::ostringstream out;out << nowtime->tm_hour << ":" << nowtime->tm_min << ":" << nowtime->tm_sec;return  std::move(out.str());
}

從文本中讀入,對文本進行分詞,分詞方法詳見

http://yangshangchuan.iteye.com/blog/2031813

以下是實現

#pragma once
#include<string>
using std::string;
#include<vector>
using std::vector;
#include"wordmap.h"
class FindWord 
{
public:FindWord() {};~FindWord() {};vector<string> GetKeyWords(const string& filename,const WordMap& wordmap);
private:int wsize = 5;bool ischinese(const char* c);
public:int getlocalfindstring(const string& ostring, int begpos);
};
@ -0,0 +1,71 @@
#include "findword.h"
#include<fstream>
#include<sstream>
#include<iostream>
using std::ifstream;
using std::istringstream;
vector<string> FindWord::GetKeyWords(const string & filename, const WordMap& wordmap)
{vector<string> l_keyword;ifstream inputfile(filename);if (!inputfile.is_open()){std::cerr << "cann't not open file:" << filename;return l_keyword;}string sinput;string last;while (std::getline(inputfile, sinput)){last = sinput;int begpos = 0;int length;while ((length = getlocalfindstring(last, begpos)) != 0){int movelen = ischinese(&last[begpos]) ?  2:1;int findlen = -1;while (movelen<=length){string ls = last.substr(begpos, movelen);auto res = wordmap.m_map.find(ls);if (res != wordmap.m_map.end()){findlen = movelen;}movelen += ischinese(&last[begpos + movelen]) ? 2 : 1;}if (findlen != -1){l_keyword.push_back(last.substr(begpos, findlen));begpos = begpos + findlen;}else {begpos += length;}}}return l_keyword;
}bool FindWord::ischinese(const char* c)
{unsigned char cur = *c;unsigned char next = *(c + 1);if (next == 0)return false;return (cur >= 0xB0 && cur <= 0xF7) && (next >= 0xA1 && next <= 0xFE);
}int FindWord::getlocalfindstring(const string& ostring,int begpos)
{int size = wsize;int endpos = begpos;while (size > 0 && ostring[endpos]){if (ischinese(&ostring[endpos])) {endpos++;}size--;endpos++;}return endpos-begpos;
}

樣例程序

@ -0,0 +1,16 @@
#include"wordmap.h"
#include<iostream>
#include<string>
#include"findword.h"
using std::string;
int main()
{WordMap m_wordmap("../../../word/word1.txt");FindWord m_findword;if (!m_wordmap.init()) { return 0; };vector<string> res= m_findword.GetKeyWords("../../../inputfile/1999.txt", m_wordmap);for (auto elems : res)std::cout << elems << " ";return 0;
}

github:https://github.com/wuzhuorui/kjct.git

轉載于:https://www.cnblogs.com/creativityroom/p/8472253.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/284073.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/284073.shtml
英文地址,請注明出處:http://en.pswp.cn/news/284073.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

scala學習手記28 - Execute Around模式

我們訪問資源需要關注對資源的鎖定、對資源的申請和釋放&#xff0c;還有考慮可能遇到的各種異常。這些事項本身與代碼的邏輯操作無關&#xff0c;但我們不能遺漏。也就是說進入方法時獲取資源&#xff0c;退出方法時釋放資源。這種處理就進入了Execute Around模式的范疇。 在s…

【時序數據庫InfluxDB】Windows環境下配置InfluxDB+數據可視化,以及使用 C#進行簡單操作的代碼實例...

前言&#xff1a;如題。直接上手擼&#xff0c;附帶各種截圖&#xff0c;就不做介紹了。1、influxDB的官網下載地址 https://portal.influxdata.com/downloads/打開以后&#xff0c;如下圖所示&#xff0c;可以選擇版本號&#xff0c;以及平臺。此處咱們選擇windows平臺。不過…

官宣 微軟跨平臺 UI 框架 .NET MAUI 6 正式發布

微軟宣布 .NET MAUI 已正式 GA。 .NET MAUI (.NET Multi-platform App UI) 是一個跨平臺 UI 框架&#xff08;前身是 Xamarin.Forms&#xff09;&#xff0c;用于通過 C# 和 XAML 創建原生移動和桌面應用。基于 .NET MAUI&#xff0c;開發者可在單個共享代碼庫中創建同時支持 A…

92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m 2 and n 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt…

Reset

在常用的代碼中&#xff0c;我們使用AddForm.form.reset();或者AddForm.getForm().reset();來將FormPanel重置。 但是當頁面增加和修改公用一個formpanel時&#xff0c;當先點擊修改時&#xff0c;窗體修改顯示出數據&#xff0c;關閉窗體后&#xff08;window.hide()&#xff…

《.NET物聯網從零開始》系列

近日搞硬件網關時&#xff0c;那些殘存的數電、模電和通信原理的記憶時常在腦海中縈繞&#xff1b;想起來多年前看張高興的博客學會了.netcore樹莓派進行物聯網開發。使用dragonboard(龍板)搭載windows 10 iot系統&#xff0c;配合光電傳感器和rfid實現了一個項目原型。碰巧逛g…

設計好接口的 36 個錦囊(原則)

目錄 設計好接口的 36 個錦囊 | 接口參數校驗 | 修改老接口時&#xff0c;注意接口的兼容性 | 設計接口時&#xff0c;充分考慮接口的可擴展性 | 接口考慮是否需要防重處理 | 重點接口&#xff0c;考慮線程池隔離 | 調用第三方接口要考慮異常和超時處理 | 接口實現考慮…

嵌入式第11次實驗

嵌入式軟件設計第11次實驗報告 學號&#xff1a;140201236 姓名&#xff1a;沈樟偉 組別&#xff1a;第2組 實驗地點&#xff1a;D19 一、實驗目的&#xff1a; 1、了解短信AT指令的使用方法。 2、掌握使用短信AT指令驅動SIM900A發送和接收短信的方…

Linux文件系統之df

df用于查看當前掛載的文件系統-a 查看所有的文件系統可以自己指定容量單位&#xff0c;-BM -BG 但是還是h的選項好用-i 查看inode的使用信息-l(L) 顯示本地文件系統--output 可以指定管理員想要看的列--outputField_List可用的字段有source fstype itotal iused iavail ipcent …

普通老實人的生活

2019獨角獸企業重金招聘Python工程師標準>>> 有一個朋友&#xff0c;他家有一套營業房&#xff0c;租給了兩個年輕人&#xff0c;合同簽訂為半年&#xff0c;房租7000&#xff0c;合同到期當天&#xff0c;乙方一直沒有聯系甲方&#xff0c;說明續租或不續租&#x…

如何在 C# 中運行 Python 代碼

前言Python是一門強大的編程語言。特別的是&#xff0c;它還具有眾多出色的庫&#xff08;例如numPy&#xff0c;sciPy&#xff0c;pandas等&#xff09;&#xff0c;可以顯著簡化和加速開發。因此&#xff0c;在解決某些問題時&#xff0c;通過 Python 實現可能是最理想的方式…

Ubuntu開機默認進入命令行模式/用戶圖形界面

一、開機默認進入命令行模式 # 輸入命令&#xff1a; sudo systemctl set-default multi-user.target # 重啟&#xff1a; reboot要進入圖形界面&#xff0c;只需要輸入命令startx 從圖形界面切換回命令行&#xff1a;ctrlaltF7 二、開機默認進入圖形用戶界面 # 輸入命令&…

數組查找數字5

public class Second {/*** param args*/public static void main(String[] args) {// TODO Auto-generated method stubint []a{2,1,3,4,5};for (int i0;i<a.length-1;i){if(a[i]!5){i;}}System.out.println("這組數里有5呢"); }} 轉載于:https://www.cnblogs.co…

【QGIS入門實戰精品教程】10.2:QGIS中DEM三維顯示方法

QGIS中數字高程模型DEM三維顯示方法。 參考閱讀: 【ArcGIS Pro微課1000例】0006:ArcGIS Pro 2.5三維顯示DEM數字高程模型 【ArcGIS Pro微課1000例】0005:ArcGIS Pro 2.5基于矢量數據制作拉伸三維地圖案例 ArcGIS實驗教程——實驗二十六:ArcScene實現二維數據的三維顯示 文章…

AUTH password

為redis服務請求設置一個密碼。redis可以設置在客戶端執行commands請求前需要通過密碼驗證。通過修改配置文件的requirepass就可以設置密碼。 如果密碼與配置文件里面設置的密碼一致&#xff0c;服務端就會發會一個OK的狀態碼&#xff0c;接受客戶端發送其他的請求命令&#xf…

基于Dubbo框架構建分布式服務(三)

我們將上面開發的服務提供方服務&#xff0c;部署到2個獨立的節點上&#xff08;192.168.14.1和10.10.4.125&#xff09;&#xff0c;然后可以通過Dubbo管理中心查看對應服務的狀況&#xff0c;如圖所示&#xff1a; 上圖中可以看出&#xff0c;該服務有兩個獨立的節點可以提供…

開篇-開啟全新的.NET現代應用開發體驗

01云原生技術底座暢想DaprMASA Framework研發管理研發協作平臺&#xff1a;CI/CD、Application ManagementApplication Quota Management、Version Control、Troubleshooting、Testable網格應用程序管理API網關&#xff1a;Reverse Proxy、Traffic Control 、Security Strategy…

Ubuntu安裝docker詳細教程

1.安裝 sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common && curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && sudo add-apt-repository &qu…

java多線程編程——鎖優化

并發環境下進行編程時&#xff0c;需要使用鎖機制來同步多線程間的操作&#xff0c;保證共享資源的互斥訪問。加鎖會帶來性能上的損壞&#xff0c;似乎是眾所周知的事情。然而&#xff0c;加鎖本身不會帶來多少的性能消耗&#xff0c;性能主要是在線程的獲取鎖的過程。如果只有…

【QGIS入門實戰精品教程】10.3:QGIS柵格DEM數據重復類方法案例匯總

QGIS柵格DEM數據重復類方法案例匯總:按表格重分類、按圖層重分類。 參考閱讀: 【GlobalMapper精品教程】029:柵格重分類案例詳解 ArcGIS實驗教程——實驗四十三:ArcGIS柵格重分類(Reclass)案例詳解 ArcGIS怎樣獲取重分類后各類所占的像元個數 文章目錄 一、按表格分類二、…