Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:31:34+00:00 2026-06-13T06:31:34+00:00

So I have a text file which has the script of Act 1 from

  • 0

So I have a text file which has the script of Act 1 from a Romeo and Juliet play and I want to count how many times someone says a word.

Here is the text: http://pastebin.com/X0gaxAPK

There are 3 people speaking in the text: Gregory, Sampson, and Abraham.

Basically I want to make 3 different dictionaries (if that’s the best way to do it?) for each of the three speakers. Populate the dictionaries with the words the people say respectively, and then count how many times they say each word in the entire script.

How would I go about doing this? I think I can figure out the word count but I am a bit confused on how to separate who says what and put it into 3 different dictionaries for each person.

My output should look something like this (this is not correct but an example):

Gregory - 
25: the
15: a
5: from
3: while
1: hello
etc

Where the number is the frequency of the word said in the file.

Right now I have code written that reads the text file, strips the punctuation, and compiles the text into a list. I also don’t want to use any outside modules, I’d like to do it the old fashioned way to learn, thanks.

You don’t have to post exact code, just explain what I need to do and hopefully I can figure it out. I’m using Python 3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:31:36+00:00Added an answer on June 13, 2026 at 6:31 am
    import collections
    import string
    c = collections.defaultdict(collections.Counter)
    speaker = None
    
    with open('/tmp/spam.txt') as f:
      for line in f:
        if not line.strip():
          # we're on an empty line, the last guy has finished blabbing
          speaker = None
          continue
        if line.count(' ') == 0 and line.strip().endswith(':'):
          # a new guy is talking now, you might want to refine this event
          speaker = line.strip()[:-1]
          continue
        c[speaker].update(x.strip(string.punctuation).lower() for x in line.split())
    

    Example output:

    In [1]: run /tmp/spam.py
    
    In [2]: c.keys()
    Out[2]: [None, 'Abraham', 'Gregory', 'Sampson']
    
    In [3]: c['Gregory'].most_common(10)
    Out[3]: 
    [('the', 7),
     ('thou', 6),
     ('to', 6),
     ('of', 4),
     ('and', 4),
     ('art', 3),
     ('is', 3),
     ('it', 3),
     ('no', 3),
     ('i', 3)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which has a particular line something like sometext sometext
I have this xml file which has text init. i.e Hi my name is
i have a java ee project which has a text file that uses a
I'm having trouble with PHP text parsing I have a txt file which has
I have a text file which I want to filter using awk. The text
I have a text file which has hex values, one value on one separate
I have written a form which has 3 text inputs and one file input.
I have a text file which has the following structure: 341|18 Hello world|20090225230048AAnhStI|90|$0.30|10|289|2|2|2|Is that
I have a perl script which parses a text file and breaks it up
I've got a simple PHP script which reads specific phrases from a text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.