Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I know it looks like a lot but its a lot of helpful tips and tricks for the program itself YOUR TASK You will be

I know it looks like a lot but its a lot of helpful tips and tricks for the program itself

YOUR TASK

You will be writing a program that emulates the unix finger command in PERL, with a twist.

Write a program that provides the functionality of the command: finger possiblematch

but allows possiblematch to be misspelled. This may be accomplished by using the Soundex method to identify names that sound alike.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

THE SOUNDEX ALGORITHM The "Soundex" method of transforming names to a code that groups names/words that sound alike was originally developed by Margaret K. Odell and Robert C. Russel. Donald Knuth describes it on page 392 of The Art of Computer Programming, Volume 3 1. Retain the first letter of the name for use in step 5 2. Assign the following numbers to the letters in the word (case insensitive): LETTERS TO CONVERT BFPVbfp v CGJKQSXZcgjkqsxz DTdt CODE TO USE 2 4 MN m n 3. If two or more letters with the same code were adjacent in the original name (before step 2), including the first and any subsequent letters (see "Lloyd" example below), omit all but the first. 4. Drop all occurrences of A, E, H, I, O, UW, and Y in other positions. Also drop any non-alphabetic characters such as the dash () and apostrophe () 5. Convert to the form "letter, digit, digit, digit, digit" by adding trailing zeros (if there are less than four digits), or by dropping rightmost digits (if there are more than four) where the "letter" comes from step 1. Your "letter" should be capitalized. For example, the names Euler, Gauss, Hilbert, Knuth, Lloyd, and Lukasieicz have the respective codes E4600 G2000, H4163, K5300, L3000. L2220. Notice especially how Lloyd is converted to L3000 and how Euler converted to E4600 and not E6000 Of course this system will bring together names that are somewhat different, as well as names that are similar, the same six codes would be obtained for Ellery, Ghosh, Hulbred, Kant, Ladd, and Lissajous On the other hand, a few related names like Rogers and Rodgers, or Sinclair and St. Clair, or Tchebysheff and Chebyshev, remain separate But by and large the Soundex code greatly increases the chance of finding a name in one of its disguises. For further information, cf. C.P. Bourne and D.F. Ford, JACM 8 (1961), 538-552; Leon Davidson, CACM 5 (1962), 169-171; Federal Population Censuses 1790-1890 (Washington, D.C.: National Archives, 1971), 90]. FIRST THINGS FIRST Implement the Soundex algorithm as a function. This program will simply help you focus on developing the Soundex algorithm. You'll need to pull this out and put it together with all the other pieces in the end. You can obviously write and test your own from scratch if you wish, but if you'd like some code that that tests your implementation, use the program on this page and implement the Soundex algorithm in the soundex function. Here's the test program for the first part: #! /usr/bin/env perl use Modern: : Perl; # the key-value pairs are the name and the correct Soundex code # this is a hash, which we'll talk about soon my %sndx- ( "Euler" -> "E4600" ,"Gauss"-"G2000" Hilbert" >"H4163","Knuth""K5300" Lloyd" "L3000 "Lukasiecz" -> "L2220", print "Processing known names: nn" my @names -sort {$ Sb} keys %sndx; foreach my $nm (names) my $findsdx- &soundex ($nm); # calls function w/param of $nm print "The soundex for $nm should be $sndx(Snm and is $findsdxIn" if findSdx ne $sndxI$nm)) print "tHowever, that is incorrect!nin" ### The goal of the soundex subroutine below is to take in a string ### which will be a last name and process it via soundex magic, ### resulting in $sdxvalue at the very end containing the Soundex ### code for the input. So, if "Euler" (no quotes) gets passed in ### to $temp, $sdxvalue should contain "E4600" when all work is done sub soundex ( my $temp shift; tempuc $tempi my $sdxvalue- " $temp not proces sed "; # default response #### Do the work here on Stemp to create the appropriate $sdxvalue # uppercase the name passed in to normalize it #### Obviously assign $temp to $sdxvalue before you hit the return statement return $sdValue; So, the first thing you should do is write the Soundex conversion portion of the code -make sure your soundex() function is working correctly before moving on to the rest of the program When you're done with this first program, you'll have a working soundex finished program. function that you'll eventually use in your SECOND THINGS SECOND Using your knowledge of split and your new knowledge of how to write functions, write the function sub getNames The parameter sent to this subroutine is a line from the file /etc/passwd, such as: rfulkerson::14608 140608 Robert A. Fulkerson, PKI 174B, N/A, N/A: /home/rfulkerson: /bin/bash mgpayne: *:14609:14609:Matt Payne,,,:/home/mgpayne:/bin/bash news:x: 9:9:news:/var/spoolews: /bin/bash postfix:x 110:118::/var/spool/postfix:/bin/false The first example, for the account rfulkerson, has more information in the 5th field than the second example. The fifth field contains sub-fields delimited first by commas and then by spaces. The name will always be found in the first comma-separated portion of field 5. In the first two examples above, this is true: Robert A. Fulkerson and Matt Payne are both present in the first comma-separated portion of the fifth field. For the news account, there just don't happen to be any commas, which doesn't make a difference: news is still the first "comma-separated field in the fifth field" of the/etc/passwd file entry. For the postfix account, notice that the fifth field is empty. This does happen and you should account for it. sub getNames returns the userid, the first name and the last name (in that order). You can store these values in an array (again, in that order) and simply return the array Again, as with the soundex ) function, test this function in a separate program first. You can find example code below. You will need to implement the function mentioned before it's complete and will run correctly. /usr/bin/env perl use Modern:: Perl; open (IN,"/etc/passwd") # this opens the file for processing my @wholeFile- 1N>; close IN: foreach my $line (wholeFile) # this reads in the file, line by line, into # individual elements of array @wholeFile # this closes the file # process all lines of the file print $line: my (Suserid, sfirstname, Slastname) # print the line so we can see it all # send $line to function to grab userid, first name and last name (userid, Sfirstname, lastname)&getNames ($line) print "tlogin print "tFirst name: Sfirstname " print "tlast name $lastnameInin" : $useridin" sub getNames my Sparametershifti my results"fakeuserid", "Eake first name,"ake last name" # put real code here to process $parameter and get the real results return gresults # get the argument sent to the function THIRD THINGS THIRD Practice using the system) function by running this program Use perldoc -f system to read more if you're interested. #1/usr/bin/env perl use Modern:: Perl; # Use the system() function # actual command to execute goes in $arguments [0] # all parameters go into Sarguments [1] and up my Suserid "list" my args"finger",Suseridi system (args) In your soundex finger program you'll want to use system to call the real finger program for each account where soundex (item_from_passwd file) equals &soundex (command_line_ data) FOURTH THINGS LAST Some general notes and comments about your program Your program should attempt to match first names, last names and logins .Your soundex algorithm should generate a capital letter followed by four digits. Invoke the program like this (assume your program is called sdxfinger.pl): sdxfinger.pl possiblematch If no command-line argument is given, print a meaningful usage statement telling the user how to invoke the program (i.e., tell them to supply a name to your soundex finger program) and exit the program. Use $0 when generating your usage statement. .You should be able to run -/sdxfinger.pl more or ./sdxfinger.pl mohr and get the same results as if you typed finger moore. Try some others, such as: /sdxfinger.pl fulkerson /sdxfinger.pl faulkersuhn /sdxfinger.pl fpfffffauhlkkersssin These should all produce the same output as if you had typed finger fulkerson with the added bonus of also including accounts with similar sounding last names. Your output should end up looking identical to what calling the normal finger function looks like when called with one command-ine argument with one exception: print out the Soundex code you have calculated for the command line argument before any matches you have made .Each matched account should only be listed once .You may not use the Text: :Soundex library to solve this program. Goals Write a moderately difficult Perl program, trying to implement a most-likely-known algorithm in a Perl way instead of a C/C++Java way. Points to Think About Writing from a Perl frame of mind will simplify this program quite a bit Writing the code in small chunks and testing those chunks thoroughly will make development go smoother. It's easier to debug smaller pieces of code and then assemble them into one larger program than to try and tackle all of the pieces at the same time You may run a sample program by typing ~ rfulkerson/samples/sax finger on Loki. Grading Points Your program must have a complete, signed Honor Pledge in order to be graded. You must write the soundex subroutine and the getName s subroutine For this program, a portion of your grade will be based on documentation of your subroutines. Explain any code that is not obvious (which should be most of the conversion code in the soundex function). Your soundex code must be a capital letter followed by four digits. Your program must not produce any errors. If it does, points will be taken off, one point per error to a maximum of 5 points. You may not use the Text::Soundex library in your solution. command line argument, before you print all matching accounts. argument is given, and your program should end after the usage statement is displayed. .You must output the command line argument you were processing, as well as the Soundex code for that Your program must have a meaningful and accurate usage statement that is displayed if no command line If no accounts match the command line argument, your program should print "No such user". .You should only print matching accounts once. Ru ma usereloki:-$ The name you were looking for,skellington, converted to S4523 Einger: skellington: no such user am Ru ma user1oki:-$./sdxfinger.pl elmo The name you were looking for, elmo, converted to E4500 Login: efluehr Directory: /home/efluehr Never logged in Mail last read Thu Nov 1 06:49 2012 (CDT) No Plan Name: Ellen Fluehr Shell /bin/bash Login: ehopkins Directory: /home/ehopkins Never logged in No mail No Plan Name: Elna Hopkins Shell /bin/bash Name: Ellen Sampson Shell /bin/bash Login: esampson Directory: /home/esampson Never logged in No mai1 No Plan Login: eterry Directory: /home/eterry Never logged in Mail last read Thu Now 1 06:49 2012 (CDT) No Plan Name: Elaina Terry Shell /bin/bash THE SOUNDEX ALGORITHM The "Soundex" method of transforming names to a code that groups names/words that sound alike was originally developed by Margaret K. Odell and Robert C. Russel. Donald Knuth describes it on page 392 of The Art of Computer Programming, Volume 3 1. Retain the first letter of the name for use in step 5 2. Assign the following numbers to the letters in the word (case insensitive): LETTERS TO CONVERT BFPVbfp v CGJKQSXZcgjkqsxz DTdt CODE TO USE 2 4 MN m n 3. If two or more letters with the same code were adjacent in the original name (before step 2), including the first and any subsequent letters (see "Lloyd" example below), omit all but the first. 4. Drop all occurrences of A, E, H, I, O, UW, and Y in other positions. Also drop any non-alphabetic characters such as the dash () and apostrophe () 5. Convert to the form "letter, digit, digit, digit, digit" by adding trailing zeros (if there are less than four digits), or by dropping rightmost digits (if there are more than four) where the "letter" comes from step 1. Your "letter" should be capitalized. For example, the names Euler, Gauss, Hilbert, Knuth, Lloyd, and Lukasieicz have the respective codes E4600 G2000, H4163, K5300, L3000. L2220. Notice especially how Lloyd is converted to L3000 and how Euler converted to E4600 and not E6000 Of course this system will bring together names that are somewhat different, as well as names that are similar, the same six codes would be obtained for Ellery, Ghosh, Hulbred, Kant, Ladd, and Lissajous On the other hand, a few related names like Rogers and Rodgers, or Sinclair and St. Clair, or Tchebysheff and Chebyshev, remain separate But by and large the Soundex code greatly increases the chance of finding a name in one of its disguises. For further information, cf. C.P. Bourne and D.F. Ford, JACM 8 (1961), 538-552; Leon Davidson, CACM 5 (1962), 169-171; Federal Population Censuses 1790-1890 (Washington, D.C.: National Archives, 1971), 90]. FIRST THINGS FIRST Implement the Soundex algorithm as a function. This program will simply help you focus on developing the Soundex algorithm. You'll need to pull this out and put it together with all the other pieces in the end. You can obviously write and test your own from scratch if you wish, but if you'd like some code that that tests your implementation, use the program on this page and implement the Soundex algorithm in the soundex function. Here's the test program for the first part: #! /usr/bin/env perl use Modern: : Perl; # the key-value pairs are the name and the correct Soundex code # this is a hash, which we'll talk about soon my %sndx- ( "Euler" -> "E4600" ,"Gauss"-"G2000" Hilbert" >"H4163","Knuth""K5300" Lloyd" "L3000 "Lukasiecz" -> "L2220", print "Processing known names: nn" my @names -sort {$ Sb} keys %sndx; foreach my $nm (names) my $findsdx- &soundex ($nm); # calls function w/param of $nm print "The soundex for $nm should be $sndx(Snm and is $findsdxIn" if findSdx ne $sndxI$nm)) print "tHowever, that is incorrect!nin" ### The goal of the soundex subroutine below is to take in a string ### which will be a last name and process it via soundex magic, ### resulting in $sdxvalue at the very end containing the Soundex ### code for the input. So, if "Euler" (no quotes) gets passed in ### to $temp, $sdxvalue should contain "E4600" when all work is done sub soundex ( my $temp shift; tempuc $tempi my $sdxvalue- " $temp not proces sed "; # default response #### Do the work here on Stemp to create the appropriate $sdxvalue # uppercase the name passed in to normalize it #### Obviously assign $temp to $sdxvalue before you hit the return statement return $sdValue; So, the first thing you should do is write the Soundex conversion portion of the code -make sure your soundex() function is working correctly before moving on to the rest of the program When you're done with this first program, you'll have a working soundex finished program. function that you'll eventually use in your SECOND THINGS SECOND Using your knowledge of split and your new knowledge of how to write functions, write the function sub getNames The parameter sent to this subroutine is a line from the file /etc/passwd, such as: rfulkerson::14608 140608 Robert A. Fulkerson, PKI 174B, N/A, N/A: /home/rfulkerson: /bin/bash mgpayne: *:14609:14609:Matt Payne,,,:/home/mgpayne:/bin/bash news:x: 9:9:news:/var/spoolews: /bin/bash postfix:x 110:118::/var/spool/postfix:/bin/false The first example, for the account rfulkerson, has more information in the 5th field than the second example. The fifth field contains sub-fields delimited first by commas and then by spaces. The name will always be found in the first comma-separated portion of field 5. In the first two examples above, this is true: Robert A. Fulkerson and Matt Payne are both present in the first comma-separated portion of the fifth field. For the news account, there just don't happen to be any commas, which doesn't make a difference: news is still the first "comma-separated field in the fifth field" of the/etc/passwd file entry. For the postfix account, notice that the fifth field is empty. This does happen and you should account for it. sub getNames returns the userid, the first name and the last name (in that order). You can store these values in an array (again, in that order) and simply return the array Again, as with the soundex ) function, test this function in a separate program first. You can find example code below. You will need to implement the function mentioned before it's complete and will run correctly. /usr/bin/env perl use Modern:: Perl; open (IN,"/etc/passwd") # this opens the file for processing my @wholeFile- 1N>; close IN: foreach my $line (wholeFile) # this reads in the file, line by line, into # individual elements of array @wholeFile # this closes the file # process all lines of the file print $line: my (Suserid, sfirstname, Slastname) # print the line so we can see it all # send $line to function to grab userid, first name and last name (userid, Sfirstname, lastname)&getNames ($line) print "tlogin print "tFirst name: Sfirstname " print "tlast name $lastnameInin" : $useridin" sub getNames my Sparametershifti my results"fakeuserid", "Eake first name,"ake last name" # put real code here to process $parameter and get the real results return gresults # get the argument sent to the function THIRD THINGS THIRD Practice using the system) function by running this program Use perldoc -f system to read more if you're interested. #1/usr/bin/env perl use Modern:: Perl; # Use the system() function # actual command to execute goes in $arguments [0] # all parameters go into Sarguments [1] and up my Suserid "list" my args"finger",Suseridi system (args) In your soundex finger program you'll want to use system to call the real finger program for each account where soundex (item_from_passwd file) equals &soundex (command_line_ data) FOURTH THINGS LAST Some general notes and comments about your program Your program should attempt to match first names, last names and logins .Your soundex algorithm should generate a capital letter followed by four digits. Invoke the program like this (assume your program is called sdxfinger.pl): sdxfinger.pl possiblematch If no command-line argument is given, print a meaningful usage statement telling the user how to invoke the program (i.e., tell them to supply a name to your soundex finger program) and exit the program. Use $0 when generating your usage statement. .You should be able to run -/sdxfinger.pl more or ./sdxfinger.pl mohr and get the same results as if you typed finger moore. Try some others, such as: /sdxfinger.pl fulkerson /sdxfinger.pl faulkersuhn /sdxfinger.pl fpfffffauhlkkersssin These should all produce the same output as if you had typed finger fulkerson with the added bonus of also including accounts with similar sounding last names. Your output should end up looking identical to what calling the normal finger function looks like when called with one command-ine argument with one exception: print out the Soundex code you have calculated for the command line argument before any matches you have made .Each matched account should only be listed once .You may not use the Text: :Soundex library to solve this program. Goals Write a moderately difficult Perl program, trying to implement a most-likely-known algorithm in a Perl way instead of a C/C++Java way. Points to Think About Writing from a Perl frame of mind will simplify this program quite a bit Writing the code in small chunks and testing those chunks thoroughly will make development go smoother. It's easier to debug smaller pieces of code and then assemble them into one larger program than to try and tackle all of the pieces at the same time You may run a sample program by typing ~ rfulkerson/samples/sax finger on Loki. Grading Points Your program must have a complete, signed Honor Pledge in order to be graded. You must write the soundex subroutine and the getName s subroutine For this program, a portion of your grade will be based on documentation of your subroutines. Explain any code that is not obvious (which should be most of the conversion code in the soundex function). Your soundex code must be a capital letter followed by four digits. Your program must not produce any errors. If it does, points will be taken off, one point per error to a maximum of 5 points. You may not use the Text::Soundex library in your solution. command line argument, before you print all matching accounts. argument is given, and your program should end after the usage statement is displayed. .You must output the command line argument you were processing, as well as the Soundex code for that Your program must have a meaningful and accurate usage statement that is displayed if no command line If no accounts match the command line argument, your program should print "No such user". .You should only print matching accounts once. Ru ma usereloki:-$ The name you were looking for,skellington, converted to S4523 Einger: skellington: no such user am Ru ma user1oki:-$./sdxfinger.pl elmo The name you were looking for, elmo, converted to E4500 Login: efluehr Directory: /home/efluehr Never logged in Mail last read Thu Nov 1 06:49 2012 (CDT) No Plan Name: Ellen Fluehr Shell /bin/bash Login: ehopkins Directory: /home/ehopkins Never logged in No mail No Plan Name: Elna Hopkins Shell /bin/bash Name: Ellen Sampson Shell /bin/bash Login: esampson Directory: /home/esampson Never logged in No mai1 No Plan Login: eterry Directory: /home/eterry Never logged in Mail last read Thu Now 1 06:49 2012 (CDT) No Plan Name: Elaina Terry Shell /bin/bash

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

width = 0 car = build _ car ( width ) print ( car , end = " " )

Answered: 1 week ago