Module 5: Advanced text processing 1

  • 前言
  • Question1: End in end
  • Question2:Rare words
  • Question3:Accumulating characters
  • Question4: It's all about perspective
  • 总结:

前言

我尽可能做详细,每个步骤讲清楚。答案不止一种,有大神可以留言。其他课程章节在我的主页可以查看。
第五章主要内容是处理文本行,该模块的模式如下: 过滤和转换文本行,提取单词,逐行摘要,基于固定单词位置的处理。
文中空行和#注释不算讲解的代码行数,查代码行数的时候可以直接跳过。


Question1: End in end

Your task is to write a program that loops through the full Jane Eyre novel and counts those lines which end in the word “end” and prints out the result. We stored the novel in the jane_eyre.txt file.
Let your program print out the following message:


Where 4 is the number of lines that your program should count. As a reference, these are the lines that you should find:


Make sure that you only count lines which end in the word “end”, not just the characters. To do this, you could for example put a white space infront of the word, to make sure you don’t get longer words whose last three letters happen to be “end”.

要求: 打开文本文件,过滤出‘end’结尾的句子,然后查找有多少行是end结尾的句子,按照要求打印出来。

count = 0
for line in open("jane_eyre.txt"):line_strip = line.rstrip()if line_strip.endswith(" end"):count += 1print("There are "+str(count)+" lines that end in 'end'.")

line1: 获得一个变量count,它的值为0,用来每次的循环计数。
line2: 遍历循环文件,将数据文件中的每一行分配给循环变量 line 。
line3: 用rstrip()函数删除字符串末尾的指定字符(默认为空格)。然后赋值给line_strip。
line4: 条件判断函数endswith()判断结尾是否以end结尾。
可以在单词的前面加上一个空格,以确保您不会得到后三个字母碰巧是“ end”的更长的单词。
line5: 如果每次有end结尾的单词,count计数循环加一。
line6: 打印输出理想结果。输出里必须是字符串格式,原本的count为整数格式,所以要加一个str(count)。


Question2:Rare words

Your task is to write a program that loops through the words in the provided jane_eyre.txt file and counts those words which fulfill both of the following conditions:

  • The word has more than 10 characters
  • The word does not contain the letter “e”

Once you’ve finished the aggregation, let your program print out the following message:


Again, as a reference here are the words that your program should find (and count):


If you’re unsure how to start this task, go back one slide and take another look at the read method, and then a few slides more where we covered looping over lists.

**要求:**找出文本里字母数量超过10个的和带有e的单词,过滤出来有多少个单词。还有一种打开文件的方式是read()。
方法1: 主要讲解

count = 0
f = open("jane_eyre.txt").read()
words = f.split()
for word in words:if len(word) > 10 and "e" not in word:count += 1print("There are "+str(count)+" long words without an 'e'.")

line1: 设置一个变量0,为了后面参加循环。
line2: 用open().read()函数来打开读取文件。
line3: 用split()函数把文本遇到空格分开列表形式。
line4: 用for in循环,遍历words在word里。
line5-line6: 条件判断单词的字数是否大于10 用and并列判断e是否在word里,如果两个条件都满足,计数循环加1.
line7: 输出理想结果。

方法2: 可参考

count = 0
for line in open("jane_eyre.txt"):line = line.rstrip()for word in line.split():if len(word) > 10 and "e" not in word:count += 1print("There are " + str(count) + " long words without an 'e'.")

Question3:Accumulating characters

To practise nested loops and in-loop aggregations, your task is to write a program that, for each line, sums up the number of characters in whichever words in the line do not contain the letter “e”.
For example, in the following line of words,


we have four words (“coming”, “was”, “my”, “day”) that don’t contain the letter “e”. Summing up the number of characters in each of these four words, we get 6+3+2+3, which is 14.

Let your program print out the number of characters for each line. Using the provided jane_eyre.txt, the output of your program should look like this:


Hint
You’ll need a nested for loop here - go back a few slides if you’re unsure how to solve this problem.

要求: 找出每行没有带e的单词,然后查出这些单词的有多少个字母,打印出来。

for line in open("jane_eyre.txt"):line_strip = line.strip()count = 0for word in line_strip.split():if "e" not in word:count += len(word)print(count)

line1: for in 循环打开文件。
line2: strip()函数去掉文本的首位空余部分。
line3: 设置一个计数循环0,在小循环的外面,大循环的里面。
line4: 第二次循环,用split()函数把line2 的文本变成列表格式。
line5: 条件判断e是否在文本里。
line6: 如果条件判断为真,line3的循环每次加符合条件的单词并通过len()函数计算单词长度。
line8: 输出理想结果。


Question4: It’s all about perspective

The novel Jane Eyre is written in the First Person perspective and, interestingly enough, about 20% of all sentences in this novel start with the word “I”. It also turns out that the most common last word is “me”.


Which words follow and precede “I” and “me”? Your task is to write a program that loops over the provided jane_eyre_sentences.txt file and prints out the second and second-last words of each sentence that starts with the word “I” and ends with the word “me”. Each line in the file is one sentence with the ending punctuation mark removed.

The file contains the first 500 sentences of our novel. The output of your program for those should look like this:


For example, the first sentence that starts with “I” and ends with “me” is:

I resisted all the way a new thing for me and a circumstance which greatly strengthened the bad opinion Bessie and Miss Abbot were disposed to entertain of me

In this sentence, the second word is resisted and the second-last word is of.

When you check the last word of each sentence, don’t forget about the newline and/or carriage return character. It’s probably best to strip each line first before looking at the individual words.

要求: 打开文本文件,找出句子是以I开头和me结尾的句子。然后过滤出这个句子的第二项和倒数第二项。有两种方法差距不大,前者老师的,后者我自己的。

方法1:

for line in open("jane_eyre_sentences.txt"):line_strip = line.strip()words = line_strip.split()if words[0] == "I" and words[-1] == "me":#连等号==可以用关键字in来替代print(words[1], words[-2])

line1: for in循环开文件
line2: 用strip()函数去掉首位的空格
line3: 用split()函数遇到空格就把line_strip变量分成列表形式。
line4: 条件判断,索引的[0] 和[-1]是开头和结尾,用==或者关键字in来判断是不是’I’和’me’。
line5: 因为已经通过split()函数变成列表,可以用[1],[-2]来索引出第二个字和倒数第二个字,最后输出理想结果。

方法2:(参考)

for word in open('jane_eyre_sentences.txt'):words=word.strip()if words.startswith('I '):if words.endswith(' me'):word=word.split() print(word[1],word[-2])

注释:只是把split()函数放在最后来把变量存储的值此变成列表。


总结:

这单元学的不难,了解索引分清split()和strip()函数就没有什么大问题。
我会持续更新,初心不变,记录学到的知识。上述所讲的内容如果有什么问题请留言或者私信我,小编看见会在第一时间更新。

USYD悉尼大学DATA1002 详细作业解析Module5相关推荐

  1. 从本科作业到Nature子刊:悉尼大学大二学生突破困扰量子计算近20年的纠错码难题...

    别人家孩子的本科生涯:悉尼大学的一位本科生在大二写物理作业时「一不小心」解决了一个量子计算难题,相关论文刚刚登上了<自然 - 通讯>杂志. >>>> 一作.悉尼大学 ...

  2. 悉尼大学计算机专业本科2019,悉尼大学2019 S1官方校历时间表……

    原标题:悉尼大学2019 S1官方校历时间表-- 悉尼大学 2019 S1校历 2019 S1 USYD Key Date 2019 年 第一学期校历一览表 2019.02.20-22 O-week ...

  3. 视觉+Transformer最新论文出炉,华为联合北大、悉尼大学发表

    作者 | CV君 来源 | 我爱计算机视觉 Transformer 技术最开始起源于自然语言处理领域,但今年5月份Facebook 的一篇文章将其应用于计算机视觉中的目标检测(DETR算法,目前已有7 ...

  4. 悉尼大学陶大程:遗传对抗生成网络有效解决GAN两大痛点

    来源:新智元 本文共7372字,建议阅读10分钟. 本文为你整理了9月20日的AI WORLD 2018 世界人工智能峰会上陶大程教授的演讲内容. [ 导读 ]悉尼大学教授.澳大利亚科学院院士.优必选 ...

  5. 悉尼大学计算机工程专业世界排名,2019QS澳洲计算机专业排名,7所大学进入世界百强!...

    原标题:2019QS澳洲计算机专业排名,7所大学进入世界百强! 说起计算机专业,很多学生会联想到好就业薪水丰厚,不仅是国内,在全球来看,计算机专业人才都非常受欢迎,所以这几年出国留学就读计算机专业学生 ...

  6. 华为联合北大、悉尼大学对 Visual Transformer 的最新综述

    Transformer 技术最开始起源于自然语言处理领域,但今年5月份Facebook 的一篇文章将其应用于计算机视觉中的目标检测(DETR算法,目前已有78次引用)使其大放异彩,并迅速得到CV研究社 ...

  7. ​东京大学商汤悉尼大学等提出融合了动态规划、分治算法的MIM,实现绿色高效层次Transformer!已开源!...

    关注公众号,发现CV技术之美 本文分享论文『Green Hierarchical Vision Transformer for Masked Image Modeling』,由东京大学&商汤& ...

  8. 悉尼大学计算机专业本科2019,悉尼大学开学时间是什么时候?2019-2020年时间表介绍...

    悉尼大学每年有两个开学时间,分别在2月底和8月初,具体的日期并未规定,每年都会有所差别,以下是2019-2020年悉尼大学课程时间表详细规定-- 一.2019年悉尼大学课程时间表 1.第一学期 2月1 ...

  9. 悉尼大学计算机研究生学制,悉尼大学研究生学制

    澳大利亚悉尼大学具有丰富的研究生专业课程,学制安排一般在1-2年时间. 悉尼大学硕士申请要求 要求非211大学申请者,暂不需清华认证 (毕业证.学位证.成绩单) 入学要求: 工程类专业(Enginee ...

  10. 澳大利亚悉尼大学徐畅教授招收深度学习方向全奖博士生

    来源:AI求职 悉尼大学 悉尼大学(The University of Sydney),坐落于澳大利亚新南威尔士州首府悉尼,是研究型大学.悉尼大学注重理论与实践相结合,教育.法学.医学.会计与金融 . ...

最新文章

  1. Maven向本地仓库导入官方仓库没有的jar包
  2. 前端实例练习 - 轮播图
  3. Markdown拾遗
  4. Mybatis实现分库分表
  5. git add .出现尚未暂存以备提交的变更
  6. 【20:30直播】网易老司机聊程序员的职场道路选择
  7. 由于这台计算机没有终端服务器客户端访问许可证,远程会话被中断解决办法...
  8. hasp运行不成功_提问:程序无报错或警告,但总是运行不成功,会卡住没反应...
  9. 企业实战05:Oracle数据库_操作表中数据
  10. SELinux入门:了解和配置SELinux
  11. linux入门_Linux从入门到入土(抽奖送10本新书)
  12. jdk TreeMap源码解析
  13. [pytorch] 深度学习分割网络U-net的pytorch模型实现 原创 2017年03月08日 21:48:21 标签: python / 深度学习 / 生物图
  14. weico.android批量转发,weico android|Weico新浪微博3.3.5 客户端_手机软件
  15. 双目立体匹配算法:ELAS
  16. 统计软件简介与数据操作
  17. 触动精灵 alilib
  18. 大数据分析平台洱源县_洱源县专项债可行性研究报告
  19. python判断word页码
  20. iPad网游输入优化

热门文章

  1. 可靠数据传输(RDT)的原理
  2. 2011年中国程序员薪水,蛋疼。
  3. MySQL数据库出现unknown error 1449错误原因及其解决办法
  4. (result, consumed) = self._buffer_decode(data, self.errors, final)
  5. A Survey on Vision Transformer
  6. 中通快递api,中通快递一件代发api,中通快递礼品商城api,中通快递空包api
  7. 鸿蒙系统多屏协同,华为EMUI 11支持畅连、多屏协同等功能,与鸿蒙设备实现交互...
  8. Facet Kernel详解、Random Walk随机游走算法详解
  9. 用Acrobat打印小册子
  10. HTML5期末大作业:蘑菇街网站设计——2021蘑菇街首页(1页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web期末作业设计网页_清新淡雅蘑菇街大学生网页设计作业成品