当前位置: 首页 > news >正文

socket编程-UDP(2)-设计翻译系统

socket编程-UDP(2)-设计翻译系统

实现一个简单的英译汉的网络字典

dictionary.txt

apple: 苹果
banana: 香蕉
cat: 猫
dog: 狗
book: 书
pen: 笔
happy: 快乐的
sad: 悲伤的
hello: 
: 你好run: 跑
jump: 跳
teacher: 老师
student: 学生
car: 汽车
bus: 公交车
love: 爱
hate: 恨
hello: 你好
goodbye: 再见
summer: 夏天
winter: 冬天

Dict.hpp

#pragma once#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include "Log.hpp"
#include "InetAddr.hpp"const std::string defaultdict = "./dictionary.txt";
const std::string sep = ": ";using namespace LogModule;class Dict
{
public:Dict(const std::string &path = defaultdict) : _dict_path(path){}bool LoadDict(){std::ifstream in(_dict_path);if (!in.is_open()){LOG(LogLevel::DEBUG) << "打开字典: " << _dict_path << " 错误";return false;}std::string line;while (std::getline(in, line)){// "apple: 苹果"auto pos = line.find(sep);if (pos == std::string::npos){LOG(LogLevel::WARNING) << "解析: " << line << " 失败";continue;}std::string english = line.substr(0, pos);std::string chinese = line.substr(pos + sep.size());if (english.empty() || chinese.empty()){LOG(LogLevel::WARNING) << "没有有效内容: " << line;continue;}_dict.insert(std::make_pair(english, chinese));LOG(LogLevel::DEBUG) << "加载: " << line;}in.close();return true;}std::string Translate(const std::string &word, InetAddr &client){auto iter = _dict.find(word);if (iter == _dict.end()){LOG(LogLevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << " : " << client.Port() << "]# " << word << "->None";return "None";}LOG(LogLevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << " : " << client.Port() << "]# " << word << "->" << iter->second;return iter->second;}~Dict(){}private:std::string _dict_path; // 路径+文件名std::unordered_map<std::string, std::string> _dict;
};

InetAddr.hpp

#pragma once
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
// 网络地址和主机地址之间进行转换的类class InetAddr
{
public:InetAddr(struct sockaddr_in &addr) : _addr(addr){_port = ntohs(_addr.sin_port);           // 从网络中拿到的!网络序列_ip = inet_ntoa(_addr.sin_addr); // 4字节网络风格的IP -> 点分十进制的字符串风格的IP}uint16_t Port() {return _port;}std::string Ip() {return _ip;}~InetAddr(){}private:struct sockaddr_in _addr;std::string _ip;uint16_t _port;
};
http://www.lryc.cn/news/606751.html

相关文章:

  • 中英混合的语音识别XPhoneBERT 监督的音频到音素的编码器结合 f0 特征LID
  • 【LeetCode】算法详解#11 ---相交链表
  • 《Java 程序设计》核心知识点梳理与深入探究
  • 深入理解C语言指针:从回调函数到数组指针笔试题全解析(下)
  • Canny边缘检测算法-个人记录
  • 【世纪龙科技】3D交互深度赋能-汽车整车维护仿真教学软件
  • 汽车供应链PPAP自动化审核指南:如何用AI实现规则精准匹配与文件智能校验
  • 【世纪龙科技】汽车整车维护仿真教学软件-智构整车维护实训
  • 目标检测检出率,误检率,ap,map等评估python代码
  • 防火墙安全策略实验一
  • 分类预测 | Matlab实现CPO-PNN冠豪猪算法优化概率神经网络多特征分类预测
  • Redis学习-----Redis的基本数据类型
  • 数学与应用数学的区别是什么
  • CSS font-weight:500不生效
  • Mysql join语句
  • 智慧能源管理平台的多层协同控制架构研究
  • ansible 在EE 容器镜像中运行
  • 在SQL SERVER 中,用SSMS 实现存储过程的每日自动调用
  • 守护数字核心:主机安全的重要性与全方位防护指南
  • Java 根据多个 MM-dd 日期计算总时长(包含当日和次日)
  • 新手小白如何快速检测IP 的好坏?
  • OSPF笔记整理
  • JavaWeb(苍穹外卖)--学习笔记16(定时任务工具Spring Task,Cron表达式)
  • 电子电气架构 --- 加速48V技术应用的平衡之道
  • SpringMVC的高级特性
  • mongodb中的哈希索引详解
  • Redis深度剖析:从基础到实战(下)
  • Unity3D数学第五篇:几何计算与常用算法(实用算法篇)
  • 【BFS】P7555 [USACO21OPEN] Maze Tac Toe S|普及+
  • 【C#学习Day16笔记】XML文件、 事件Event 、Json数据