Python Forum
Extending my text file word count ranker and calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extending my text file word count ranker and calculator
#3
f-string can be used in all cases,then avoid rather ugly string like this.
print("A total of " + str(len(wordlist)) + " words can be found inside this text file.")
To make the code work,and look into that function can take arguments.
# word_count.py
from collections import Counter
import re

def word_count(text):
    wordlist = text.split()
    print(f"A total of {len(wordlist)} words can be found inside this text file.")

def rank_words(text):
    words = re.findall('\w+', text)
    top_10 = Counter(words).most_common(10)
    for word,count in top_10:
        print(f'{word:<4} {"-->":^4} {count:>4}')

def file_read():
    with open('alice.txt') as f:
        text = f.read().lower()
        return text

if __name__ == '__main__':
    text = file_read()
    word_count(text)
    rank_words(text)
Can now give a demo on how if __name__ == '__main__': work.
Running word_count.py script now.
Output:
A total of 29465 words can be found inside this text file. the --> 1818 and --> 940 to --> 809 a --> 690 of --> 631 it --> 610 she --> 553 i --> 543 you --> 481 said --> 462
Now will import the script.
>>> import word_count
>>> # Nothing happens
This is what we want when import code as module.
We don't want it run at import.
Use it like this:
>>> import word_count

>>> text = word_count.file_read()

>>> word_count.word_count(text)
A total of 29465 words can be found inside this text file.

>>> word_count.rank_words(text)
the  -->  1818
and  -->   940
to   -->   809
a    -->   690
of   -->   631
it   -->   610
she  -->   553
i    -->   543
you  -->   481
said -->   462
Now if remove if __name__ == '__main__':.
The code run when import it,in almost all cases this not wanted.
>>> import word_count
A total of 29465 words can be found inside this text file.
the  -->  1818
and  -->   940
to   -->   809
a    -->   690
of   -->   631
it   -->   610
she  -->   553
i    -->   543
you  -->   481
said -->   462
Reply


Messages In This Thread
RE: Extending my Python word count ranker and calculator - by snippsat - Jan-19-2019, 02:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 313 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Replace a text/word in docx file using Python Devan 4 4,174 Oct-17-2023, 06:03 PM
Last Post: Devan
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,221 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Row Count and coloumn count Yegor123 4 1,451 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,804 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  find some word in text list file and a bit change to them RolanRoll 3 1,624 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  python-docx regex: replace any word in docx text Tmagpy 4 2,370 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,832 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,243 Feb-11-2022, 12:38 AM
Last Post: atomxkai
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,611 Aug-12-2021, 04:25 PM
Last Post: palladium

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020