在GitHub上发现一些很有意思的项目,由于本人作为Python的初学者,编程代码能力相对薄弱,为了加强Python的学习,特此利用前辈们的学习知识成果,自己去亲自实现。
一周没有更新了,主要还是自己太懒,三分热度,我想如果我继续这样下去,做什么事都做不好,以此为戒,坚持一个月内练习完这些知识点!
来源:GitHub
Python练手小程序项目地址:https://github.com/Show-Me-the-Code/python
写作日期:2019.12.08
今天练习第0004题,题目如下:
这道题主要考察细节问题,比如:It’s/wasn’t/don’t……这类缩写的词怎么检测到,以及还原。
英文文件subtitle.txt,内容如下:
Make sure you know what
you're supposed to be doing. Okay, that sounds obvious and easy, right? This is not always true. If you are new to your job, you might not
have a feel for your responsibilities. If you have not done a certain
type of work before, you might not know how long
something is going to take. That's completely normal. So how do you know? Ask. Review your job responsibilities,
ask someone who is in the same roll or who has previously done this job, keep good
notes, keep track of how long something takes you so that you have a solid
estimate and you can use that next time. Remember, it also helps to take big
tasks and break them into smaller steps. Remember that research Sam was
starting for the sales reports? He might take that research and divide it
into steps, that way he can keep track of each step and he'll know what is
involved in completing that research. How do you know what your priorities are? Depending on the type of work you do,
your priorities come to you from your leadership or perhaps from a customer
who is, you're supporting. It's important to really understand
the priorities you're expected to support. So in your sample work plan, you're going
to have something, perhaps the priority, the description of what you're doing,
the due date, who you're doing it for, the steps, an estimate of how long
it's going to take, and a start date. And those are some good fields for
you to start out with. And you can create a table,
a spread sheet, and begin to work with this and see if this
helps you in completing your work. You might not really know how
long something is going to take. That's okay. So how do you know? Ask. Review your job responsibilities
with your supervisor, ask someone who's in the same role,
or who has previously done the job. Keep good notes when you do something,
keep notes, keep track of how long it takes you, and then you have something to refer back
to for a good estimate next time. Remember, it also helps to take big
tasks and break them into smaller steps. Remember when Sam was
working on that research, he started that research
with those sales reports? He might take that research and
divide it into steps, and that way he can keep track of
how long each step will take, and he's gonna know what is involved
in completing that research. How do you know what your priorities are? Well, depending on the type of work you
do, sometimes your priorities are gonna come to you from your leadership or
from a customer you support. It's important to really understand the
priorities you are expected to support. In our example with Sam,
it became more important for him to complete his status report
first and then his other report later. What if your boss can not or
will not tell you? You know sometimes the person you work for
wants to see you figure it out for yourself. Sometimes the person you work for doesn't
really know what the true priorities are. And if this is true, you're gonna
have to figure it out for yourself. Pay attention to what is discussed the
most in meetings and in announcements and in other communications. Pay attention to where your successful
colleagues are spending their time. There are clues around you waiting for
you to uncover them. Before you go,
I would like to share with you a story. I once had a co-worker who had
an important meeting after lunch and the meeting was with an executive. She was gonna facilitate the meeting and
give a presentation. During lunch, she remembered that she forgotten
to pick something up at the store. The item she forgot was something
her family needed that evening. Immediately, she ran out of the office to
the store and purchased the missing item. In doing so,
she was late to her own meeting and to make matters worse,
she explained why she was late. Her manager looked at her and said, if I had known you were gonna go do
that, I would've gone to the store for you so you could have been here
on time and been prepared. That's what we call
a career limiting moment. It was all because she forgot her plan,
she forgot her priorities, and she forgot the difference between something
that was urgent versus important. It may have been important that she bring
home that item that evening to her family, but it wasn't urgent. She could've picked it up
on the way home from work. What was both urgent and important
was being prepared for that meeting. Now, before we move to the next module,
why don't you consider taking a shot at creating your own plan that covers
at least the next five business days.
想要统计英文文件单词出现的个数:
'/''
遍历每一个单词元素去检测。Python处理代码如下:
import string
def extend_word(text):
"""
文件内容缩写检测和缩写还原
:param text:
:return:
"""
# 判断文件内容text中是否存在缩写
if text.find('\'') > 0:
old2new = dict()
words = text.split()
for i, word in enumerate(words):
# 判断每个单词元素是否存在缩写
if word.find('\'') > 0:
# 对缩写单词元素进行分隔成两部分
parts = word.split('\'')
# 根据规则穷举所有可能的缩写单词元素,并还原
if parts[1] == 'm':
parts[1] = 'am'
elif parts[1] == 's':
parts[1] = 'is'
elif parts[1] == 're':
parts[1] = 'are'
elif parts[1] == 't':
parts[1] = 'not'
elif parts[1] == 've':
parts[1] = 'have'
elif parts[1] == 'll':
parts[1] = 'will'
# 如果英文缩写是'd',那么需要考虑后面的单词,若是'better',那么是'had',否则是'would'
elif parts[1] == 'd':
if words[i + 1] == 'better':
parts[1] = 'had'
else:
parts[1] = 'would'
# 针对don't/doesn't等这些单词元素的还原
if parts[0].endswith('n'):
parts[0] = parts[0][:-1]
# 每个缩写的单词元素对应的还原后的单词组
# 备注一下,比如:I'd better/ I'd like ,这两个同时存在的话,键对应的值就不是唯一的,后者出现的值会覆盖前者,这里我没做考虑
old2new[word] = ' '.join(parts)
_text = text
# print(old2new)
# 对英文文件中的缩写单词替换成还原后的单词组
for old_word in old2new.keys():
_text = _text.replace(old_word, old2new[old_word])
return _text
return text
def count_num():
"""
去除特殊字符,并统计英文单词个数
:return:
"""
with open('subtitle2.txt', 'r') as file:
article = file.read()
no_pun_text = article
# string.punctuation 返回 '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
# 去掉'''
_punctuation = string.punctuation.replace('\'', '')
# 去掉英文文件内容中的特殊字符
for pun in _punctuation:
no_pun_text = no_pun_text.replace(pun, '')
complete_text = extend_word(no_pun_text)
records = dict()
for word in complete_text.lower().split():
records[word] = records.get(word, 0) + 1
return records
def show_in_order(records):
"""
对字典根据value的取值大小排序
:param records:
:return:
"""
items = sorted(records.items(), key=lambda x: x[1], reverse=True)
for item in items:
print(item[0], item[1])
if __name__ == '__main__':
records = count_num()
show_in_order(records)