fix协议封装挑战-将一个消息实体编码为协议字符串

发布一个k8s部署视频:https://edu.csdn.net/course/detail/26967

课程内容:各种k8s部署方式。包括minikube部署,kubeadm部署,kubeasz部署,rancher部署,k3s部署。包括开发测试环境部署k8s,和生产环境部署k8s。

腾讯课堂连接地址https://ke.qq.com/course/478827?taid=4373109931462251&tuin=ba64518

第二个视频发布  https://edu.csdn.net/course/detail/27109

腾讯课堂连接地址https://ke.qq.com/course/484107?tuin=ba64518

介绍主要的k8s资源的使用配置和命令。包括configmap,pod,service,replicaset,namespace,deployment,daemonset,ingress,pv,pvc,sc,role,rolebinding,clusterrole,clusterrolebinding,secret,serviceaccount,statefulset,job,cronjob,podDisruptionbudget,podSecurityPolicy,networkPolicy,resourceQuota,limitrange,endpoint,event,conponentstatus,node,apiservice,controllerRevision等。

第三个视频发布:https://edu.csdn.net/course/detail/27574

详细介绍helm命令,学习helm chart语法,编写helm chart。深入分析各项目源码,学习编写helm插件


————————————————

消息实体如下:

 

package cs.test;

import java.text.SimpleDateFormat;
import java.util.Date;

import cs.mina.SeqenceNum;
import cs.mina.codec.msg.BaseMsg;
import cs.mina.codec.msg.DisclosureInstructionGrp;
import cs.mina.codec.msg.DisclosureInstructionGrpTag;
import cs.mina.codec.msg.InstrumentTag;
import cs.mina.codec.msg.NewOrderSingleMsg;
import cs.mina.codec.msg.OrderQtyDataTag;
import cs.mina.codec.msg.PartiesTag;
import cs.mina.codec.msg.Party;

/*
 *@author(huangxiaoping)
 *@date 2013-11-29
 */
public class TC_OM_04Test {
	public static BaseMsg createMsg(){
		 NewOrderSingleMsg msg=new NewOrderSingleMsg();
		 msg.getHeadEntity().getMsgSeqNum().setTagValue(SeqenceNum.getSeqence()+"");
	   	 msg.getClOrdID().setTagValue("13");
	   	 msg.getParties().setTagValue("1");
	   	 Party party=new Party();
	   	 party.getPartyID().setTagValue("1122");
	   	 party.getPartyIDSource().setTagValue("D");
	   	 party.getPartyRole().setTagValue("1");
	   	 ((PartiesTag)msg.getParties()).getChildren().add(party);
	   	 ((InstrumentTag)msg.getInstrument()).getSecurityID().setTagValue("36");
	   	 ((InstrumentTag)msg.getInstrument()).getSecurityIDSource().setTagValue("8");
	   	 ((InstrumentTag)msg.getInstrument()).getSecurityExchange().setTagValue("XHKG");
	   	 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS"); 
	   	 msg.getTransactTime().setTagValue(sdf.format(new Date()));
	   	 msg.getSide().setTagValue("2");
	   	 msg.getOrdType().setTagValue("2");
	   	 msg.getPrice().setTagValue("30.05");
	   	 ((OrderQtyDataTag)msg.getOrderQtyData()).getOrderQty().setTagValue("4000");
	   	 msg.getTimeInForce().setTagValue("0");
	   	 msg.getMaxPriceLevels().setTagValue("1");
	   	 msg.getExecInst().setTagValue("c x");
	   	 
	   	 DisclosureInstructionGrp dis=new DisclosureInstructionGrp();
	   	 dis.getDisclosureType().setTagValue("100");
	   	 dis.getDisclosureInstruction().setTagValue("1");
	   	((DisclosureInstructionGrpTag)msg.getDisclosureInstructionGrp()).setTagValue("1");
	   	 ((DisclosureInstructionGrpTag)msg.getDisclosureInstructionGrp()).getChildren().add(dis);
	   	 return msg;
	   	 
	   }
}

 

 

 

 

 

 

消息如下:

 

测试案例TC_OM_04
测试数据1
发送:
8=FIXT.1.1_9=206_35=D_49=CO99999901_56=HKEXCO_34=3_52=20131129-15:07:00.902_11=13_453=1_448=1122_447=D_452=1_18=c x_1090=1_48=36_22=8_207=XHKG_60=20131129-15:07:00.903_54=2_40=2_44=30.05_38=4000_59=0_1812=1_1813=100_1814=1_10=146_
响应:
8=FIXT.1.1_9=239_35=8_49=HKEXCO_56=CO99999901_34=3_52=20131129-07:07:01.327_1128=9_11=13_14=0_17=20701315_150=0_151=4000_1093=2_1090=1_453=1_448=1122_447=D_452=1_37=30701209_38=4000_39=0_40=2_44=30.05_207=XHKG_48=36_22=8_54=2_59=0_60=20131129-07:07:01.317_10=027_


说明:

 

消息的构造很简单,就是javabean,关键是如何把这个javabean解析为消息字符串。

解析原理如下:

 

 
package cs.mina.codec.msg; import java.util.HashSet; import java.util.List; import java.util.Set; import cs.mina.exception.InValidDataException; /* *@author(huangxiaoping) *@date 2013-10-24 */ public abstract class BaseMsg { public static final String SOH = "\u0001"; protected String head; protected String body; protected String tailer; protected Head headEntity=new Head(); protected Body bodyEntity=new Body(); protected Tailer tailerEntity=new Tailer(); public abstract void validate(); public String encode(){ String result=""; List<Tag> headTagList=this.headEntity.getHeadTagList(); for(int i=0;i<headTagList.size();i++){ Tag headTag=headTagList.get(i); result+=headTag.encode(); } List<Tag> bodyTagList=this.bodyEntity.getBodyTagList(); for(int i=0;i<bodyTagList.size();i++){ Tag bodyTag=bodyTagList.get(i); result+=bodyTag.encode(); } List<Tag> tailerTagList=this.tailerEntity.getTailerTagList(); for(int i=0;i<tailerTagList.size();i++){ Tag tailerTag=tailerTagList.get(i); result+=tailerTag.encode(); } return result; } public void decode(){ decodeHead(); decodeBody(); decodeTailer(); } public abstract void decodeBody(); public void decodeHead(){ Set<String> already=new HashSet<String>(); String[] headItems=this.head.split(SOH); for(int i=0;i<headItems.length;i++){ String headItem=headItems[i]; String[] headTagItems=headItem.split("="); if(headTagItems.length!=2){ throw new InValidDataException("消息格式错误"); } if(already.contains(headTagItems[0])){ throw new InValidDataException("消息格式错误"); } already.add(headTagItems[0]); if(headTagItems[0].equals("627")){ int skip=decodeGroup627(); i=i+skip-1; }else{ List<Tag> headTags=this.headEntity.getHeadTagList(); for(int j=0;j<headTags.size();j++){ Tag headTag=headTags.get(j); if(headTag.getTagId().equals(headTagItems[0])){ headTag.setTagValue(headTagItems[1]); } } } } } private int decodeGroup627(){ int index=head.indexOf("627="); String rest=head.substring(index); String[]tagItems=rest.split(BaseMsg.SOH); String[] numTagItems=tagItems[0].split("="); if(numTagItems.length!=2){ throw new InValidDataException("消息格式错误"); } if(!"627".equals(numTagItems[0])){ throw new InValidDataException("消息格式错误"); } int num=Integer.parseInt(numTagItems[1]); int count=1; int whileCount=0; ((HopGrpTag)this.headEntity.getHopGrpTag()).setTagValue(num+""); boolean end=false; for(int i=0;i<num;i++){ if(end){ if(i!=num){ throw new InValidDataException("消息格式错误"); }else{ break; } } HopGrp hopGrp=new HopGrp(); ((HopGrpTag)this.getHeadEntity().getHopGrpTag()).getChildren().add(hopGrp); while(true){ if(whileCount>=3){ whileCount=0; break; } if(tagItems.length==count){ break; } String[] tagIt=tagItems[count].split("="); if(tagIt.length!=2){ throw new InValidDataException("消息格式错误"); } if(whileCount==0){ if(!HopGrpTag.first.equals(tagIt[0])){ throw new InValidDataException("消息格式错误"); } } if(HopGrp.tagIdsSet.contains(tagIt[0])){ List<Tag> tagList=hopGrp.getTagList(); for(int j=0;j<tagList.size();j++){ Tag groupTag=tagList.get(j); if(groupTag.getTagId().equals(tagIt[0])){ groupTag.setTagValue(tagIt[1]); break; } } if(tagItems.length>count+1){ String[] nextTagIt=tagItems[count+1].split("="); if(nextTagIt.length!=2){ throw new InValidDataException("消息格式错误"); } if(HopGrpTag.first.equals(nextTagIt[0])){ whileCount=0; count++; break; } } count++; whileCount++; }else{ end=true; break; } } } return count; } public void decodeTailer(){ Set<String> already=new HashSet<String>(); String[] tailerItems=this.tailer.split(SOH); for(int i=0;i<tailerItems.length;i++){ String tailerItem=tailerItems[i]; String[] tailerTagItems=tailerItem.split("="); if(tailerTagItems.length!=2){ throw new InValidDataException("消息格式错误"); } if(already.contains(tailerTagItems[0])){ throw new InValidDataException("消息格式错误"); } already.add(tailerTagItems[0]); List<Tag> tailerTags=this.tailerEntity.getTailerTagList(); for(int j=0;j<tailerTags.size();j++){ Tag tailerTag=tailerTags.get(j); if(tailerTag.getTagId().equals(tailerTagItems[0])){ tailerTag.setTagValue(tailerTagItems[1]); } } } } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getTailer() { return tailer; } public void setTailer(String tailer) { this.tailer = tailer; } public Head getHeadEntity() { return headEntity; } public void setHeadEntity(Head headEntity) { this.headEntity = headEntity; } public Body getBodyEntity() { return bodyEntity; } public void setBodyEntity(Body bodyEntity) { this.bodyEntity = bodyEntity; } public Tailer getTailerEntity() { return tailerEntity; } public void setTailerEntity(Tailer tailerEntity) { this.tailerEntity = tailerEntity; } } 

 

 

 

 


消息头和消息尾是共用的,用通用的方法解析就可以了。关键是消息体,每个消息的消息体不一样,但是我们还是可以用递归的方式解析。

 

 

 
注意encode 方法

 

 



 

 

 

<think>好的,用户想了解Python的免费智能文本处理库。我需要先回忆有哪些常用的库,然后整理它们的用途和特点。 首先,自然语言处理方面,NLTK应该是最经典的库了,提供很多工具和数据集,适合教学和基础任务。然后spaCy,这个库效率高,适合工业级应用,有预训练模型。还有Gensim,主要用于主题建模和文档相似度,比如LDA模型。TextBlob可能也不错,对新手友好,封装了NLTK和Pattern,简化了常见任务。 接着是文本处理的基础库,比如正则表达式库re,字符串处理必备。Jieba和SnowNLP是针对中文的分词库,特别是Jieba用户量很大。这些可能需要单独提一下,因为用户可能处理中文文本。 然后是深度学习框架集成的库,比如Transformers库,Hugging Face的,支持BERT、GPT这些预训练模型,适合高级NLP任务。还有AllenNLP,基于PyTorch,方便研究使用。Scikit-learn虽然不是专门文本处理,但特征提取如TF-IDF也很重要。 另外,文本清洗的库可能也需要提到,比如ftfy修复编码问题,html解析库BeautifulSoup。这些在预处理阶段很有用。 需要分门别类,让用户清晰找到适合不同需求的库。要说明每个库的主要功能和特点,比如spaCy速度快,Transformers支持预训练模型,Jieba处理中文分词。还要注意免费和开源,符合用户的要求。 可能用户的需求是进行文本分析、情感分析、实体识别等任务,所以需要覆盖不同层次的库,从基础处理到高级模型。还要提醒安装方式和简单示例,帮助用户快速上手。 最后检查有没有遗漏的重要库,比如最近比较火的CoreNLP的Python接口?不过可能不算完全免费或者需要Java环境。可能还是集中在纯Python库上比较好。确保提到的库都是广泛使用且维护良好的。</think>以下是Python中常用的免费智能文本处理库分类介绍及使用示例: 一、自然语言处理(NLP)核心库 1. **NLTK (Natural Language Toolkit)** - 经典NLP库,包含50+语料库和词干提取/词形还原等工具 - 安装:`pip install nltk` - 示例:词性标注 ```python import nltk nltk.download('averaged_perceptron_tagger') text = "Python is amazing for text processing" print(nltk.pos_tag(nltk.word_tokenize(text))) ``` 2. **spaCy** - 工业级NLP库,支持多语言预训练模型 - 特点:处理速度快,支持命名实体识别(NER)、依存句法分析 - 安装:`pip install spacy && python -m spacy download en_core_web_sm` - 示例:实体识别 ```python import spacy nlp = spacy.load("en_core_web_sm") doc = nlp("Apple reached $2 trillion market cap in 2023") print([(ent.text, ent.label_) for ent in doc.ents]) ``` 3. **Gensim** - 专业文本向量化与主题建模 - 支持Word2Vec、Doc2Vec、LDA等算法 - 示例:文本相似度计算 ```python from gensim import corpora, models docs = ["健康 饮食", "健康 运动", "科技 创新"] dictionary = corpora.Dictionary([doc.split() for doc in docs]) corpus = [dictionary.doc2bow(doc.split()) for doc in docs] tfidf = models.TfidfModel(corpus) ``` 二、中文处理专项库 1. **Jieba** - 中文分词首选工具 - 安装:`pip install jieba` - 示例: ```python import jieba text = "自然语言处理技术发展迅速" print(list(jieba.cut(text))) ``` 2. **SnowNLP** - 中文情感分析/文本摘要 - 示例: ```python from snownlp import SnowNLP s = SnowNLP("这个产品体验非常好") print(s.sentiments) # 输出情感倾向值(0-1) ``` 三、深度学习支持 1. **Transformers (Hugging Face)** - 提供BERT、GPT等预训练模型接口 - 安装:`pip install transformers` - 示例:文本生成 ```python from transformers import pipeline generator = pipeline('text-generation', model='gpt2') print(generator("人工智能的未来是", max_length=50)) ``` 2. **AllenNLP** - 基于PyTorch的高级NLP框架 - 特点:可轻松实现自定义模型 - 示例:文本分类 ```python from allennlp.predictors import Predictor predictor = Predictor.from_path("https://storage.googleapis.com/allennlp-public-models/elmo-constituency-parser-2020.02.10.tar.gz") print(predictor.predict(sentence="This is a test sentence.")) ``` 四、实用工具库 1. **TextBlob** - 简化版NLTK,适合快速开发 - 示例:情感分析 ```python from textblob import TextBlob analysis = TextBlob("Python is amazing for text processing") print(analysis.sentiment) # 输出情感极性值 ``` 2. **Pattern** - 支持文本挖掘与网络分析 - 功能:词形还原/社交网络分析 - 示例: ```python from pattern.en import pluralize print(pluralize('analysis')) # 输出'analyses' ``` 五、文本预处理工具 1. **ftfy** - 修复文本编码问题 ```python import ftfy print(ftfy.fix_text('✔ No problems')) # 输出'✔ No problems' ``` 2. **BeautifulSoup** - HTML/XML解析清洗 ```python from bs4 import BeautifulSoup html = '<p>Some messy text</p>' print(BeautifulSoup(html, 'html.parser').get_text()) ``` 选择建议: - 基础处理:NLTK + Jieba - 生产环境:spaCy + Transformers - 中文场景:Jieba + SnowNLP - 学术研究:Gensim + AllenNLP 所有库均通过PyPI安装,建议配合虚拟环境使用。处理中文时注意编码问题,推荐统一使用UTF-8编码格式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hxpjava1

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

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

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

打赏作者

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

抵扣说明:

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

余额充值