Module 3: Text processing and data cleaning

  • 前言
  • Question 1: Strip it!
  • Question 2: Filter
  • Question 3: Vowel words
  • Question 4: Words with all vowels
  • Question 5: Stem it!
  • 总结

前言

我尽可能做详细,每个步骤讲清楚。答案不止一种,有大神可以留言。其他章节在我的主页可以查看。
第三章我们将集中讨论文本处理领域中的两个重要概念:转换和过滤。这两个任务通常应用于数据清理和数据挖掘,因此对我们而言,重要的是要理解这些概念背后的基本思想。
文中空行和#注释不算讲解的代码行数,查代码行数的时候可以直接跳过。


Question 1: Strip it!

For text processing and analysis, it is often irritating to have the carriage return (\n) character at the end of each line. Luckily, there is a way to remove this character!

Your task is to read in the file pride_and_prejudice.txt and transform each word such that the carriage return character is removed. Then print out the transformed word.

When you run your program, this is what the first few lines of the output should look like:


By removing the carriage return character, there should be no empty lines between the words.

要求 :把给的文本 pride_and_prejudice.txt(文本在grok里)每行打印出来。

#答案
for word in open('pride_and_prejudice.txt'):word1=word.strip()print(word1)

line1 : 遍历循环文件,将数据文件中的每一行分配给循环变量word 。
line2: word1一个新的变量,.strip()方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
链接: .strip()函数详细用法
line3: 打印新的变量


Question 2: Filter

Statistics show that “e” is the most common letter in the English language, with a frequency of about 11.1%, according to a study by Oxford Dictionaries. And in fact, about 44% of the words in our novel contain this letter.

Your task is to filter out words that contain the letter “e”. Using the supplied text file called pride_and_prejudice.txt, write a program that prints out every word which does not contain the letter “e”, or we could say instead: every word which has an “e” in it is not printed.

For the provided file, these are the words that your program should print out:


Strip the carriage return character from each word such that there are no blank lines between the words.

要求:给的文件pride_and_prejudice.txt 让把所有带e的单词去掉,然后从每个单词中删除回车符,以使单词之间没有空行。

#我的方法:
for word in open("pride_and_prejudice.txt"):letter="e"if not letter in word:word1=word.strip()print(word1)
#老师的方法:
for word in open("pride_and_prejudice.txt"):if "e" not in word:print(word.rstrip("\n"))

我的写复杂了仅供参考,用老师的方法
line1 : 遍历循环文件,将数据文件中的每一行分配给循环变量word。
line2: 条件判断e是不是在这个循环变量里。
line3: 打印注意代码缩进。.rstrip()删除 string 字符串末尾的指定字符(默认为空格),"\n"就是一个换行符,每次循环换一行。
链接: rstrip()函数使用方法


Question 3: Vowel words

We know that “e” is the most common letter in our alphabet, yet this letter is rarely found at the beginning of a word.

Your task is to filter the provided text file such that every word which does not start with the letter e is removed. For the words that do start with “e”, print out the length.

The output of your program should look like this:

要求: 打开文本文件pride_and_prejudice.txt过滤出e开头的字母,然后打印出来并且求出单词的长度。

#答案
for word in open('pride_and_prejudice.txt'):if word.startswith('e'):length = len(word.rstrip("\n"))  print(length)

line1: 遍历循环文件,将数据文件中的每一行分配给循环变量 word 。
line2: 条件判断startswith()是否单词以e开头。
line3: 求长度函数 len()方法返回对象(字符、列表、元组等)长度或项目个数。rstrip()和"\n"上面有详细讲解。
链接: len()函数的使用方法
line4: 打印新变量,注意代码块缩进。


Question 4: Words with all vowels

Compound filtering allows us to filter using many conditions at the same time. Your task is to apply a compound filter in order to find the words that contain all five vowels.

In English, the five vowels are the letters a, e, i, o, and u.

You can solve this problem either using nested if-statements or by using the logical operator and. After filtering, print out the words that remain.

For the supplied pride_and_prejudice.txt file, the output of your program should look like this:


Before you print the words, remove the carriage return character so that your program doesn’t print blank lines.

要求: 打开文本 pride_and_prejudice.txt然后过滤出带有元音’a’ , ‘e’ , ‘i’ , ‘o’ ,‘u’ 的所有单词,不分位置只要单词有这些元音就打印出来,然后不要忘了在打印单词之前,请删除回车符,以使程序不会打印空行。

#方法1
for word in open("pride_and_prejudice.txt"):if 'a' in word:if 'e' in word:if 'i' in word:if 'o' in word:if 'u' in word:print(word.rstrip("\n"))
#方法2
for word in open('pride_and_prejudice.txt'):if 'a' in word and 'e' in word \and 'i' in word and 'o' in word \and 'u' in word:print(word.rstrip("\n"))

方法1
line1: 遍历循环文件,将数据文件中的每一行分配给循环变量word。
line2–line6 : if条件判断每一个判断项目是不是在这个文本里,如果在就直接进行最后一行打印输出,如果不在就进行下一轮条件判断,直到所有的判断进行完毕为止。
line7: 打印输出,注意代码的缩进。

方法2
line1: 遍历循环打开文本文件。
line2–line4: if条件判断一次,每一个判断项目是否在word循环里,每一项用and保留字连接,\ 反斜杠表示跨行输入。
line5: 打印输出,注意代码缩进。


Question 5: Stem it!

In the context of information extraction from text-based data, it is crucial to be able to identify words that belong to the same semantic group. For example, the following words,

organise - organising - organisation - organiser

are all derived from the word organise. This is commonly referred to as “root” or “stem” of the word. The process of reducing words to their stem is called Stemming and is widely used in e.g. natural language processing or information retrieval. The Google Search engine also makes use of stemming to increase the range of the search.

Your task is to write a program that uses a simple suffix-stripping algorithm as described below to stem each word in the provided pride_and_prejudice.txt file.

Your program should remove the following suffixes:

  • ing
  • ed
  • ly

Ignore the words that don’t end up in either of these suffixes. For the ones that do, remove the suffix and print out the new word.
this is what the output of your program should look like:


Remember \n!
For this exercise it is important to keep in mind that every line you read from a file ends with the carriage return (\n) character. Make sure you either include “\n” in your condition or you strip it off right away. Your program should not print any empty lines between the words.

要求: 打开文本文件 provided pride_and_prejudice.txt,过滤出以ing,ed和ly结尾的单词,然后将ing,ed和ly后缀去掉,最后打印输出单词。

#方法1
for word in open('pride_and_prejudice.txt'):word_s = word.rstrip('\n')if word_s.endswith('ing'):print(word_s.rstrip('ing'))elif word_s.endswith('ed'):print(word_s.rstrip('ed'))elif word_s.endswith('ly'):print(word_s.rstrip('ly'))
#方法2
for word in open('pride_and_prejudice.txt'):word_s = word.rstrip('\n')if word_s.endswith('ing'):print(word_s[:-3])elif word_s.endswith('ed'):print(word_s[:-2])elif word_s.endswith('ly'):print(word_s[:-2])

主要讲解方法1,方法2用了索引切片。在后面的章节里我会细讲,现在可以先看看
line1: 遍历循环文件,将数据文件中的每一行分配给循环变量word。
line2: 获取新的变量word_s,用函数.rstrip(’\n’) 获取新的一行。
line3–line8: 多层条件判断语句,if语句endswith()判断单词是否以ing结尾,如果为TRUE打印输出,并且用.rstrip()分离后缀。第二层用elif判断,最后一层用else判断。
链接: if-elif-else的用法


总结

以上就是今天要讲的内容,本文仅仅简单介绍了过滤文本的使用,而后面更多的知识提供了大量能使我们快速便捷地处理数据的函数和方法。
有错误在所难免,但我会尽力去做好。希望各位大佬能提出意见,找出不足,我再去改进。

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

  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. 人工智能时代将至,教育或将发生大改变,未来教育会人工智能化?
  2. 模拟标准c++中的Rtti
  3. 操作系统课设——吃水果问题
  4. linux kill命令使用
  5. lambda表达式初步
  6. java中super关键字_java中super关键字有什么用法
  7. Cartographor定位-shell脚本:不停拉起死掉的程序和脚本
  8. 你说,Redis如何实现键值自动清理?
  9. jQuery实现一个优雅的返回顶部
  10. JavaScript中的类方法、对象方法、原型方法
  11. c语言中cot函数图像,cot函数图像
  12. 已知旋转矩阵求角度_解析几何|对称,平移和旋转
  13. OFD文件免费转PDF
  14. SpaceSniffer 内存管理 空间嗅探器内存清理神器
  15. LibreELEC 10.0.2发布
  16. 大言不惭 swank? talk about sth or speak too confidently
  17. Assertion failed: Protocol wrong type for socket [10041] zeromq 4.3.1\src\ip.cpp:417)错误
  18. 腾讯云服务器安装win10
  19. 第五人格显示服务器维护中请稍后登录怎么办,第五人格账号登录失败怎么办
  20. PS怎样把成图变成素描或者速写稿

热门文章

  1. __bridge,__bridge_transfer和__bridge_retained详解
  2. CodeForces 417D Cunning Gena 状压dp
  3. fast虚拟服务器ip地址设置,迅捷路由器静态ip怎么设置_迅捷固定IP地址怎么设置?-192路由网...
  4. 别再乱用了,这才是 @Validated 和 @Valid 的真正区别和用法!
  5. windows 启动修复总结
  6. dell计算机一直重启,dell开机无限循环重启如何处理
  7. 2021年应届生面试题(一文到底)
  8. 电源的输出纹波噪声究竟该取多少才合适?
  9. 《Java并发编程的艺术》读书笔记三
  10. 如何给PDF文件添加水印?