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

ex48——更复杂的用户输入/扫描输入

lexicon程序

defconvert_number(s):"""处理异常和数字的函数"""try:returnint(s)exceptValueError:returnNoneclassLexicon():# 因主程序中调用的是lexicon.scan(),因此只能是由定义类的属性延伸到函数的属性""" 功能介绍: 1. 名称:lexicon 2. 实参:用户输入的字符串 3. 返回值return:语汇元组格式 4. 若单词不在单词语汇表中,返回原单词 5. TOKEN错误标记,标记用户错误的地方 """def__init__(self):# 初始化,并设置单词语汇表self.direction=['north','south','east','west','down','up','left','right','back']self.verb=['go','stop','kill','eat']self.stop=['the','in','of','from','at','it']self.noun=['door','bear','princess','cabinet']defscan(self,stuff):# 定义方法scansentence=[]words=stuff.split()forwordinwords:ifwordinself.direction:sentence.append(('direction',word))elifwordinself.verb:sentence.append(('verb',word))elifwordinself.stop:sentence.append(('stop',word))elifwordinself.noun:sentence.append(('noun',word))elifconvert_number(word):sentence.append(('numb',int(word)))else:sentence.append(('error',word))returnsentence# lexicon是否有更好的调用方法???lexicon=Lexicon()
fromnose.toolsimport*fromex48importlexicondeftest_direction():# lexicon.scan(str)意味着对函数lexicon添加了属性,需要借用类的概念。# 类的其中一个方法是scanassert_equal(lexicon.scan("north"),[('direction','north')])result=lexicon.scan("north south east")assert_equal(result,[('direction','north'),('direction','south'),('direction','east')],)deftest_verbs():assert_equal(lexicon.scan("go"),[('verb','go')])result=lexicon.scan("go kill eat")assert_equal(result,[('verb','go'),('verb','kill'),('verb','eat')])deftest_stops():assert_equal(lexicon.scan("the"),[('stop','the')])result=lexicon.scan("the in of")assert_equal(result,[('stop','the'),('stop','in'),('stop','of')])deftest_nouns():assert_equal(lexicon.scan("bear"),[('noun','bear')])result=lexicon.scan("bear princess")assert_equal(result,[('noun','bear'),('noun','princess')])deftest_numbers():# 输入的整数第一个单位不能为0assert_equal(lexicon.scan("1234"),[('numb',1234)])result=lexicon.scan("12 432")assert_equal(result,[('numb',12),('numb',432)])deftest_errors():# 若输入的单词不在语汇表中,则保持原样返回,并报错显示其位置assert_equal(lexicon.scan("asdf23"),[('error','asdf23')])result=lexicon.scan("bear IAS princess 806")assert_equal(result,[('noun','bear'),('error','IAS'),('noun','princess'),('numb',806)])
http://www.cnnetsun.cn/news/3711012.html

相关文章:

  • Kubernetes生产环境的十个配置陷阱:从资源限制到探针配置的避坑手册
  • 【QA那些事儿】视频SDK测试方法-场景自动化
  • vLLM与SGLang:大模型推理框架的技术对比与应用指南
  • OpenCV4Android从源码编译:定制化构建与Android集成实战
  • PMP项目管理知识体系与实战应用解析
  • 六祎-Java文件的上传和下载原理
  • 网站改版时间线,旧站新站切换:301做了为什么流量掉50%
  • 焊接工艺---角焊缝
  • SpringBoot3微服务电商架构设计与实践
  • 电商砍价系统设计:防刷策略与高并发实践
  • GetQzonehistory:三步轻松备份QQ空间所有历史说说
  • python的五种输出格式(简单粗暴)
  • PAT甲级 1074 Reversing Linked List 反转链表
  • 上交大开源《动手学大模型》实战教程,真的把我当小孩教啊!
  • 注塑厂用的质量追溯软件有没有推荐的品牌?设备要能对接
  • Obsidian 笔记库安全策略:同步≠备份,4层防护体系保护你的知识资产
  • 多语言文本嵌入模型:paraphrase-multilingual-MiniLM与all-MiniLM对比
  • 物联网设备初级电池寿命优化方案与实测数据
  • Node.js 轻量化后端:独立产品的服务端演进路径
  • 物联网设备低功耗优化:NBM7100A与STM32F423RH方案解析
  • Python爬虫实战:爬取某微博用户动态,手把手教你突破登录限制
  • DeepPCB:1500对图像数据集开启PCB缺陷检测的AI革命
  • 为什么MemcardRex能成为PlayStation 1记忆卡编辑的终极工具?
  • 基于Web的餐饮食品安全监管平台的设计与实现
  • 计算机毕业设计之“鼻护灵”微信小程序的设计与开发
  • Codex智能体配置实战:从通用助手到专属项目专家的进阶指南
  • Dev-C++入门指南:轻量级C/C++开发环境配置与实战
  • SpringBoot+Vue养老院管理系统设计与实现
  • 训练一个分类器
  • SMS凭据中枢架构与落地路线图:四阶段推进