Count the english words of english article,

Source: Internet
Author: User

Count the english words of english article,

# -*- coding: utf-8 -*-

import string
def extend_word(text):
if text.find('\'') > 0:
old2new = dict()
words = text.split()
for word in 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'
elif parts[1] == 'd':
if words[words.index(word) +1] == 'better':
parts[1] = 'had'
else:
parts[1] = 'would'
if parts[0].endswith('n'):
parts[0] =parts[0][:-1]
old2new[word] = ' '.join(parts)
_text = text
for old_word in old2new.keys():
_text = _text.replace(old_word, old2new[old_word])
return _text


def show_in_order(record):
items = sorted(records.items(), key=lambda item:item[1], reverse=True)
for item in items:
print(item[0], item[1])

with open('1.txt', 'r') as file:
article = file.read()
no_pun_text = article
_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
show_in_order(records)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.