Question

Write a program using regular expressions to extract as many of the surnames as you can...

Write a program using regular expressions to extract as many of the surnames as you can from the story in in Jane Austen's Pride and Prejudice, and compute a frequency table with how many times each one occurs. To complete this part of the assignment, turn in your program's source code, your frequency table, and a paragraph describing your approach to solving this problem.

In Python Language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The above problem can be solved as follows using the mentioned steps-

STEP 1- We will first construct the regex to find all names in string using the assumption that names are written in a particular fashion. That fashion is two words with both beginning with Capital letter and rest letters are small. Also, there is space between first name and last name. Also, names always have white spaces left and right of them.

So the regex is,

regex = "\b([A-Z][a-z]+) ([A-Z][a-z]+)\b"

\b used to suggest that names have trailing and leading white spaces.

then two parts, each having first letter as [A-Z] and rest as [a-z]. Small letters can be 1 or more. '+' sign signifies 1 or more.

Space is there between both parts to suggest first name and surname have space between them.

STEP 2- The regex will return an array of names in the form of [(first name),(surname)]. From this, we will extract the surnames.

STEP 3- We will make a dictionary that stores all the surnames. While iterating over all names, if surnames comes for first time, we will store it in dictionary and give it value 1. If the surname is repeated, then we will increment the value by 1.

STEP 4- Print the dictionary of surnames that have surname and their occurrence count.

Python Code-

import re
#stored a paragraph of Pride and Prejudice
#for example purpose
prideAndPrejudiceParagraph = '''Thee ladies of Longbourn soon waited on those of Netherfield.
The visit was soon returned in due form. Miss Bennet’s pleasing manners grew on the goodwill
of Mrs Hurst and Miss Bingley; and though the mother was found to be intolerable,
and the younger sisters not worth speaking to, a wish of being better acquainted with them was
expressed towards the two eldest. By Jane, this attention was received with the greatest pleasure,
but Elizabeth still saw superciliousness in their treatment of everybody, hardly excepting even her sister,
and could not like them; though their kindness to Jane, such as it was, had a value as arising in all
probability from the influence of their brother’s admiration. It was generally evident whenever they met,
that he did admire her and to her it was equally evident that Jane was yielding to the preference which
she had begun to entertain for him from the first, and was in a way to be very much in love; but she
considered with pleasure that it was not likely to be discovered by the world in general, since Jane united,
with great strength of feeling, a composure of temper and a uniform cheerfulness of manner which would guard
her from the suspicions of the impertinent. She mentioned this to her friend Miss Lucas.
Occupied in observing Mr Bingley attentions to her sister, Elizabeth was far from suspecting that
she was herself becoming an object of some interest in the eyes of his friend. Mr. Darcy had at first
scarcely allowed her to be pretty; he had looked at her without admiration at the ball;
and when they next met, he looked at her only to criticise.'''

#regex to find out names in the string
names = re.findall(r"\b([A-Z][a-z]+) ([A-Z][a-z]+)\b",prideAndPrejudiceParagraph);

#declared a dictionary to store surnames
surnames = {}
#iterate over names
for i in range(len(names)):
#store surname of the name in a variable
val = names[i][1]
#print the surname
print(val)
#if surname exists already in dictionary
#just increment its value
if val in surnames:
surnames[val] = surnames[val] + 1
#else, initialize its value to 1
else:
surnames[val] = 1
print(surnames)

For better understanding of code indentation and comments, here is the image of code snippet-

OUTPUT sample is as in the image below-

If this answer helps, please give an up vote. Feel free to ask any query in comments.

Add a comment
Know the answer?
Add Answer to:
Write a program using regular expressions to extract as many of the surnames as you can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • Write and submit one complete Java program to solve the following requirements. Your program will employ...

    Write and submit one complete Java program to solve the following requirements. Your program will employ packages (that is, source directories), and contain multiple source files. Because you are using packages, your code should be in a directory named “Greenhouse.” You should be able to compile your code using the command “javac Greenhouse\*.java” from a directory just below the Greenhouse directory. In the program for this assignment, class names have been specified. You mustuse the supplied class name for both...

  • Write and submit one complete Java program to solve the following requirements. Your program will employ...

    Write and submit one complete Java program to solve the following requirements. Your program will employ packages (that is, source directories), and contain multiple source files. Because you are using packages, your code should be in a directory named “Greenhouse.” You should be able to compile your code using the command “javac Greenhouse\*.java” from a directory just below the Greenhouse directory. In the program for this assignment, class names have been specified. You mustuse the supplied class name for both...

  • How do i write the pseudocode for this java code? First, write out pseudocode, and then create a program to help you by accomplishing the following tasks: :  Use command line interface to ask the use...

    How do i write the pseudocode for this java code? First, write out pseudocode, and then create a program to help you by accomplishing the following tasks: :  Use command line interface to ask the user to input the following. ○ How many apples are on hand ○ How many apples should be in stock ○ How many oranges are on hand ○ How many oranges should be in stock  Perform an operation to determine how many of...

  • In the original flashcard problem, a user can ask the program to show an entry picked...

    In the original flashcard problem, a user can ask the program to show an entry picked randomly from a glossary. When the user presses return, the program shows the definition corresponding to that entry. The user is then given the option of seeing another entry or quitting. A sample session might run as follows: Enter s to show a flashcard and q to quit: s Define: word1 Press return to see the definition definition1 Enter s to show a flashcard...

  • Python program This assignment requires you to write a single large program. I have broken it...

    Python program This assignment requires you to write a single large program. I have broken it into two parts below as a suggestion for how to approach writing the code. Please turn in one program file. Sentiment Analysis is a Big Data problem which seeks to determine the general attitude of a writer given some text they have written. For instance, we would like to have a program that could look at the text "The film was a breath of...

  • Implement the histogram function to complete the desired program. You must use dynamically allocated arrays for...

    Implement the histogram function to complete the desired program. You must use dynamically allocated arrays for this purpose. For your initial implementation, use ordered insertion to keep the words in order and ordered sequential search when looking for words. Note that the array utility functions from the lecture notes are available to you as art of the provided code. Although we are counting words in this program, the general pattern of counting occurrences of things is a common analysis step...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

  • I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe...

    I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe the missing portion. As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. // TicTacToe.cpp: Follow along with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT