Question

Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

Overview:

Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms.

Input:

Your code should work for any text either inputted directly or read in from a file.

However, for testing - input file has been provided:

  • The Gettysburg Address (by President Abraham Lincoln, 1863)

You should minimally search for these three patterns in each text: FREE, BRAVE, NATION.

You should convert the entire string to upper case and conduct your searches accordingly.

Methodology and Output:

  1. Implement the Brute Force Algorithm

  2. Implement the Knuth-Morris-Pratt Algorithm

  3. For each algorithm and text/pattern combination

    a)Track and tabulate the number of character-comparison operations   b)Track and tabulate the clock time for each algorithm

Input file for test:

The Gettysburg Address.txt

Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting-place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate — we cannot consecrate — we cannot hallow — this ground. The brave men, living and dead, who struggled here have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us — that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion — that we here highly resolve that these dead shall not have died in vain — that this nation shall have a new birth of freedom and that government of the people, by the people, for the people, shall not perish from the earth.

Code in JAVA thanks

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

Working code implemented in Java and appropriate comments provided for better understanding:

Here I am attaching code for these files:

  • StringSearch.java
  • Print.java
  • KMP.java

Source code for StringSearch.java:

import java.util.*;
import java.io.*;

public class StringSearch{
static Map<Integer, String> inputTextMap;
static Map<Integer, String> searchTextMap;
static String inputText = "", searchText = "";
static long time = 0;
static int comp=0, searchIndex=1, textIndex =1;
public static void main(String[] args)throws FileNotFoundException{
System.out.print("===============\n");
System.out.print("BruteForce\n");
System.out.print("===============");
inputTextMap = new HashMap();
searchTextMap = new HashMap();
textIndex = 1;
searchIndex = 1;
inputTextMap.put(1, "Gettysburg Address");
inputTextMap.put(2, "Star Spangled Banner");
inputText = inputTextMap.get(searchIndex);

String s = readFile(textIndex);

searchTextMap.put(1,"FREE");
searchTextMap.put(2,"BRAVE");
searchTextMap.put(3,"NATION");
for(int i=1; i<=3; i++){
searchText = searchTextMap.get(searchIndex);
bruteForce(s, searchTextMap.get(searchIndex++));
}
  
}
public static String readFile(int n)throws FileNotFoundException{
String read = "input"+Integer.toString(n)+".txt";
File file = new File(read);
Scanner sc = new Scanner(file);
StringBuilder sb = new StringBuilder();

while(sc.hasNext()){
sb.append(sc.nextLine());
}
return sb.toString().toUpperCase();
}

public static void bruteForce(String text, String find){
long start = System.currentTimeMillis();
time = start;
for(int i=0; i<=text.length()-find.length(); i++){
int j;
for(j=0; j<find.length(); j++){
comp++;
if(text.charAt(i+j) != find.charAt(j)){
break;
}
}
  
if(j == find.length()){
long end = System.currentTimeMillis();
time = end-time;
Print p = new Print(i,comp,time,searchText,inputText);
p.print(p);
return;
}
}
}
}

Source code for Print.java:

public class Print {
int index, compare;
long time;
String text, search;

public Print(){
this.index = 0;
this.compare = 0;
this.time = 0;
this.text = "";
this.search = "";
}

public Print(int index, int compare, long time, String text, String search){
this.index = index;
this.compare = compare;
this.time = time;
this.text = text;
this.search = search;
}
public static void print(Print p) {
System.out.println();
System.out.printf("%15s %25s %25s %25s %25s %1s", "First Index", "Comparisons", "Time","Input Text","Search word", "\n");
System.out.printf("%15d %22d %25dms %25s %25s", p.index, p.compare, p.time, p.text, p.search);
System.out.println();
}
}

Source code for KMP.java:

import java.util.*;
import java.io.*;

public class KMP {
static Map<Integer, String> inputTextMap;
static Map<Integer, String> searchTextMap;
static String inputText = "", searchText = "";
static long time = 0;
static int comp=0, searchIndex=1, textIndex =1;
public static void main(String[] args)throws FileNotFoundException{
System.out.print("===============\n");
System.out.print("KMP\n");
System.out.print("===============");
inputTextMap = new HashMap();
searchTextMap = new HashMap();
textIndex = 1;
searchIndex = 1;
inputTextMap.put(1, "Gettysburg Address");
inputTextMap.put(2, "Star Spangled Banner");
inputText = inputTextMap.get(textIndex);

String s = readFile(textIndex);

searchTextMap.put(1,"FREE");
searchTextMap.put(2,"BRAVE");
searchTextMap.put(3,"NATION");
for(int i=1; i<=3; i++){
searchText = searchTextMap.get(searchIndex);
kmp(s, searchTextMap.get(searchIndex++));
}
  
}
public static String readFile(int n)throws FileNotFoundException{
String read = "input"+Integer.toString(n)+".txt";
File file = new File(read);
Scanner sc = new Scanner(file);
StringBuilder sb = new StringBuilder();

while(sc.hasNext()){
sb.append(sc.nextLine());
}
return sb.toString().toUpperCase();
}

public static void kmp(String search, String pattern){
long start = System.currentTimeMillis();
time = start;
int[] lpsarr = new int[pattern.length()];
int j = 0;

lps(pattern, pattern.length(), lpsarr);
int i = 0;
while(i < search.length()){
comp++;
if(pattern.charAt(j) == search.charAt(i)){
i++;
j++;
}
if(j == pattern.length()){
long end = System.currentTimeMillis();
time = end-start;
Print p = new Print(i,comp,time,searchText,inputText);
p.print(p);
return;
}
else if(i < search.length() && pattern.charAt(j) != search.charAt(i)){
if(j != 0){
comp++;
j = lpsarr[j-1];
}else{
i++;
}
}
}

}

public static void lps(String s, int length, int[] lpsarr){
int len = 0;
int i=1;
lpsarr[0] = 0;

while(i < length){
if(s.charAt(i) == s.charAt(len)){
len++;
lpsarr[i++] = len;
}else{
if(len != 0){
len = lpsarr[len-1];
}else{
lpsarr[i++] = len;
}
}
}
}
}

Sample Output Screenshots:

Hope it helps, if you like the answer give it a thumbs up. Thank you.

Add a comment
Know the answer?
Add Answer to:
Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...
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
  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • Four score and seven years ago our fathers brought forth on this continent, a new nation,...

    Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for...

  • SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective...

    SCREENSHOTS OF CODE ONLY!! PLEASE DON'T POST TEXT!! C++ CODE! PLEASE DON'T REPOST OLD POSTS! Objective To gain experience with the operations involving binary search trees. This data structure as linked list uses dynamic memory allocation to grow as the size of the data set grows. Unlike linked lists, a binary search tree is very fast to insert, delete and search. Project Description When an author produce an index for his or her book, the first step in this process...

  • Delivered August 28, 1963 by Dr. Martin Luther King Jr. I am happy to join with...

    Delivered August 28, 1963 by Dr. Martin Luther King Jr. I am happy to join with you today in what will go down in history as the greatest demonstration for freedom in the history of our nation. Five score years ago, a great American, in whose symbolic shadow we stand today, signed the Emancipation Proclamation. This momentous decree came as a great beacon light of hope to millions of Negro slaves who had been seared in the flames of withering...

  • Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Plea...

    Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Please do not use 'print' or 'input' statements. Context of the assignment is: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. Please see attachments below: We need to consider the following cases: Case 1 - It is possible that the...

  • 1 Overview The goal of this assignment is to help you understand caches better. You are...

    1 Overview The goal of this assignment is to help you understand caches better. You are required to write a cache simulator using the C programming language. The programs have to run on iLab machines. We are providing real program memory traces as input to your cache simulator. The format and structure of the memory traces are described below. We will not give you improperly formatted files. You can assume all your input files will be in proper format as...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • I would like some assistance correcting an issue I am having with this assignment. Once a...

    I would like some assistance correcting an issue I am having with this assignment. Once a finite state automaton (FSA) is designed, its transition diagram can be translated in a straightforward manner into program code. However, this translation process is considerably tedious if the FSA is large and troublesome if the design is modified. The reason is that the transition information and mechanism are combined in the translation. To do it differently, we can design a general data structure such...

  • Read “Instituionalizing our Demise: America vs Multiculturalism” by Roger Kimball on pg 268 and “Reinventing America”...

    Read “Instituionalizing our Demise: America vs Multiculturalism” by Roger Kimball on pg 268 and “Reinventing America” Call for a new national indentity” by Elizabeth Martinez on pg 275. Create a double entry notebook for each reading selection It should be atleast five observation and responses. wric 268 PART 2 essay pro. exactly how and why their authors disagree. Instead of with parties in conflict as mediators do, you will nt of view designed to appeal to both sides, mediatn posing...

  • FISCAL POLICY IN THEORY: March, 2020: we are on the verge of Congress and the President...

    FISCAL POLICY IN THEORY: March, 2020: we are on the verge of Congress and the President passing legislation that will empower the federal government to spend an unprecedented amount of EXTRA money not seen since World War 2 ---- in order to address the pandemic but also to help cushion the blow financially of perhaps ten or twenty million Americans --- or more --- losing their jobs, and thus suffering a drop in income. The scale of the 2020 recession...

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
ADVERTISEMENT