Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created September 27, 2021 18:13
Show Gist options
  • Save jananpatel2002/70a2a397062ea6481c299515ad6639bb to your computer and use it in GitHub Desktop.
Save jananpatel2002/70a2a397062ea6481c299515ad6639bb to your computer and use it in GitHub Desktop.
/*
* Name: Janan Patel
* Date: 9/27/2021
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW4
* Email: jkpatel2001@student.stcc.edu
* Algorithm Description:
* Searching a Database of surnames (lastnames) using Soundex using own made algorithm
* Starter newsoundex package by Prof. A.C. Silvestri
*/
package soundex;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Soundex {
private static char[] discardAllNonLetters(char[] chars) {
int count = 0;
for (int i = 0; i < chars.length; i++) {
if (Character.isLetter(chars[i])) {
chars[count] = chars[i];
count++;
}
}
char[] temp = new char[count];
System.arraycopy(chars, 0, temp, 0, count);
return temp;
}
private static char[] encodeEachLetterAsDigit(char[] chars) {
for (int i = 0; i < chars.length; i++) {
if ("AEIOUHWY".indexOf(chars[i]) == -1) {
} else
chars[i] = '0';
if ("BFPV".indexOf(chars[i]) == -1) {
} else
chars[i] = '1';
if ("CGJKQSXZ".indexOf(chars[i]) == -1) {
} else
chars[i] = '2';
if ("DT".indexOf(chars[i]) == -1) {
} else
chars[i] = '3';
if ("L".indexOf(chars[i]) == -1) {
} else
chars[i] = '4';
if ("MN".indexOf(chars[i]) == -1) {
} else
chars[i] = '5';
if ("R".indexOf(chars[i]) == -1) {
} else
chars[i] = '6';
}
return chars;
}
private static char[] removeDuplicateRepeatingDigits(char[] chars) {
int count = 1;
for (int i = 1; i < chars.length; i++) {
if (chars[i] != chars[count - 1]) {
chars[count] = chars[i];
count++;
}
}
char[] temp = new char[count];
System.arraycopy(chars, 0, temp, 0, count);
return temp;
}
private static char[] removeAllZeros(char[] chars) {
int count = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] != '0') {
chars[count] = chars[i];
count++;
}
}
char[] temp = new char[count];
System.arraycopy(chars, 0, temp, 0, count);
return chars;
}
private static char[] makeCodeExactlyLengthN(char[] chars, int n) {
char temp[] = new char[n];
if (chars.length > n) {
for (int i = 0; i < n; i++) {
temp[i] = chars[i];
}
return temp;
} else
if (chars.length < n) {
for (int i = 0; i < chars.length; i++) {
temp[i] = chars[i];
}
for (int i = chars.length; i < n; i++) {
temp[i] = '0';
}
} else {
temp = chars;
}
return temp;
}
public static String soundex(String str) {
str = str.toUpperCase();
char chars[] = str.toCharArray();
// Step 1: Discard all non-letter characters
chars = discardAllNonLetters(chars);
if (chars.length == 0)
return "0000";
// Step 2: Encode each letter as a digit
chars = encodeEachLetterAsDigit(chars);
// Step 3: Coalesce adjacent duplicate digits from code (e.g. 222025 becomes
// 2025)
chars = removeDuplicateRepeatingDigits(chars);
// Step 4: Replace the first digit of code with the first letter of the original
// name
chars[0] = str.charAt(0);
// Step 5: Remove all zeros from code
chars = removeAllZeros(chars);
// Step 6: Make the code exactly length 4 by padding with zeros or truncating
// the excess
chars = makeCodeExactlyLengthN(chars, 4);
String code = new String(chars);
return code;
}
}
/*
* Name: Janan Patel
* Date: 9/20/2021
* Course Number: CSC-220
* Course Name: Data Structures and Algorithms
* Problem Number: HW3
* Email: jkpatel2001@student.stcc.edu
* Algorithm Description:
* Searching a Database of surnames (lastnames) using Soundex
* Starter newsoundex by Prof. A.C. Silvestri
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import soundex.Soundex;
public class SurnameSoundexLookup {
private final static String TITLE = "Surname Soundex Lookup System V1.0";
private final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
private final static String SURNAME_TEXTFILE = "us.txt";
private static String surNames[];
private static String soundex[];
// **********************************************
// Put as many methods you need here
private static int findNumberOfNames(String filename) throws FileNotFoundException {
File file = new File(filename);
var input = new Scanner(file);
var count = 0;
while (input.hasNextLine()) {
input.nextLine();
count++;
}
input.close();
return count;
}
private static void initSoundexDataStructures(String inFileName) throws FileNotFoundException {
var numberOfNames = findNumberOfNames(SURNAME_TEXTFILE);
SurnameSoundexLookup.surNames = new String[numberOfNames];
SurnameSoundexLookup.soundex = new String[numberOfNames];
File inFile = new File(inFileName);
var input = new Scanner(inFile);
var count = 0;
while (input.hasNextLine()) {
var surName = input.nextLine();
surNames[count] = surName;
var soundex = Soundex.soundex(surName);
SurnameSoundexLookup.soundex[count] = soundex;
count++;
}
input.close();
}
private static String[] possibleMatches(String surName) throws FileNotFoundException {
String uppercase = surName.substring(0, 1).toUpperCase() + surName.substring(1);
System.out.println(uppercase);
var newsoundex = Soundex.soundex(uppercase);
int arr_counter = 0;
for (int j = 0; j < surNames.length; j++) {
if (soundex[j].equals(newsoundex)) {
arr_counter++;
}
}
int x = 0;
String possible_arr[] = new String[arr_counter];
for (int j = 0; j < surNames.length; j++) {
if (soundex[j].equals(newsoundex)) {
possible_arr[x] = surNames[j];
x++;
}
}
return possible_arr;
}
private static void outputMatches(String surName, String[] possibleMatches) throws FileNotFoundException {
String uppercase = surName.substring(0, 1).toUpperCase() + surName.substring(1);
System.out.println("\nThe possible matches are the following below");
System.out.println("============================================");
for (int y = 0; y < possibleMatches.length; y++) {
System.out.println(possibleMatches[y]);
}
for (int x = 0; x < possibleMatches.length; x++) {
if (uppercase.equals(possibleMatches[x])) {
System.out.println();
System.out.println("Your input was also in the Database exactly how you typed it: " + uppercase
+ "\n ^ Check above for extra names that sound close to " + uppercase + " ^"
);
}
}
}
// **********************************************
// Start your logic coding in the process method
private static void process(Scanner input, String args[]) throws Exception {
System.out.print("Enter Surname: ");
var surName = input.nextLine();
var possibleMatches = possibleMatches(surName);
outputMatches(surName, possibleMatches);
}
private static void preProcess(Scanner input, String[] args) throws Exception {
initSoundexDataStructures(SURNAME_TEXTFILE);
}
// **********************************************
// Do not change the doThisAgain method
private static boolean doThisAgain(Scanner input, String prompt) {
System.out.print(prompt);
String doOver = input.nextLine();
return doOver.trim().equalsIgnoreCase("Y");
}
// **********************************************
// Do not change the main method (Well maybe a little. :-))
public static void main(String args[]) throws Exception {
System.out.println("Welcome to " + TITLE);
Scanner input = new Scanner(System.in);
preProcess(input, args);
do {
process(input, args);
} while (doThisAgain(input, CONTINUE_PROMPT));
input.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment