From ab4974855a32bee0a9b9a941399a896119e755d4 Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 2 Aug 2023 11:27:21 -0700 Subject: [PATCH] add somemore stuff. --- .classpath | 5 + .gitignore | 1 + .project | 23 ++++ .settings/org.eclipse.buildship.core.prefs | 13 +++ build.gradle | 2 +- dict1.txt | 60 ++++++++++ .../project/ProjectApplication.java | 103 ++++++++++++++++-- 7 files changed, 199 insertions(+), 8 deletions(-) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.buildship.core.prefs create mode 100644 dict1.txt diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..a362346 --- /dev/null +++ b/.classpath @@ -0,0 +1,5 @@ + + + + + diff --git a/.gitignore b/.gitignore index a2ec7d0..2cbe2e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .gradle /build/ +/bin/ # Ignore Gradle GUI config gradle-app.setting diff --git a/.project b/.project new file mode 100644 index 0000000..48a9803 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + project + Project java-projects created by Buildship. + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/.settings/org.eclipse.buildship.core.prefs b/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..2b6d83b --- /dev/null +++ b/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,13 @@ +arguments= +auto.sync=false +build.scans.enabled=false +connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) +connection.project.dir= +eclipse.preferences.version=1 +gradle.user.home= +java.home=/usr/lib/jvm/java-11-openjdk-amd64 +jvm.arguments= +offline.mode=false +override.workspace.settings=true +show.console.view=true +show.executions.view=true diff --git a/build.gradle b/build.gradle index 1a64b83..56bb70b 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ dependencies { } testCompile 'junit:junit:4.12' compile 'junit:junit:4.12' - implementation 'org.springframework.boot:spring-boot-devtools:2.2.6.RELEASE' + compile('org.springframework.boot:spring-boot-devtools:2.2.6.RELEASE') sourceSets { main { diff --git a/dict1.txt b/dict1.txt new file mode 100644 index 0000000..5637020 --- /dev/null +++ b/dict1.txt @@ -0,0 +1,60 @@ +abash +aura +bar +barb +bee +beg +blush +bog +bogus +bough +bow +brew +briar +brow +brush +bug +bugs +bus +but +egg +ego +erg +ghost +go +goes +gorge +gosh +grew +grow +grub +gush +he +her +here +hew +hog +hose +how +hub +hug +huh +hush +owe +rub +sew +she +shrub +shrug +sir +sub +surge +swore +web +wee +were +whore +whose +woe +wore +worse \ No newline at end of file diff --git a/src/main/java/com/matthewjensen/project/ProjectApplication.java b/src/main/java/com/matthewjensen/project/ProjectApplication.java index 610b17e..2380cea 100644 --- a/src/main/java/com/matthewjensen/project/ProjectApplication.java +++ b/src/main/java/com/matthewjensen/project/ProjectApplication.java @@ -1,5 +1,7 @@ package com.matthewjensen.project; +import com.matthewjensen.project.modules.Anagrams; +import com.matthewjensen.project.modules.LetterInventory; import com.matthewjensen.project.modules.AnagramMain; import java.io.FileNotFoundException; @@ -7,6 +9,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; +import java.util.*; +import java.io.*; +import org.slf4j.*; @SpringBootApplication public class ProjectApplication { @@ -18,16 +23,100 @@ public class ProjectApplication { } @RestController class TwentyQuestions { + private static final Logger logger = LoggerFactory.getLogger(TwentyQuestions.class); + @RequestMapping("/") + public void home() { + if (1 == 1) { + logger.info("Hello mobile user!"); + } else if (3 == 3) { + logger.info("Hello tablet user!"); + } else { + logger.info("Hello desktop user!"); + } + } + // dictionary file to use for input (change to dict2, dict3) + private static final String DICTIONARY_FILE = "dict1.txt"; + + // set to true to test runtime and # of letter inventories created + private static final boolean TIMING = true; + private static final boolean DEBUG = true; + @RequestMapping("/20questions") - String index() { - //AnagramMain anagram = new AnagramMain(); + public String index() throws FileNotFoundException { + System.out.println("Welcome to the CS 145 anagram solver."); + System.out.println("Using dictionary file " + DICTIONARY_FILE + "."); + + // read dictionary into a set + Scanner input = new Scanner(new File(DICTIONARY_FILE)); + Set dictionary = new TreeSet(); + while (input.hasNextLine()) { + dictionary.add(input.nextLine()); + } + dictionary = Collections.unmodifiableSet(dictionary); // read-only + + // create Anagrams object for, well, solving anagrams + Anagrams solver = new Anagrams(dictionary); + + // get first phrase to solve + Scanner console = new Scanner(System.in); + String phrase = getPhrase(console); - //String[] args = new String[4]; - //try { - // anagram.main(args); - //} catch (FileNotFoundException e){ - //} + int i = 0; + // loop to get/solve each phrase + while (phrase.length() > 0 || i > 1) { + i++; + System.out.println("All words found in \"" + phrase + "\":"); + Set allWords = solver.getWords(phrase); + System.out.println(allWords); + System.out.println(); + + System.out.print("Max words to include (Enter for no max)? "); + String line; + if (DEBUG) { + line = "4"; + } else { + line = console.nextLine().trim(); + } + + long startTime = System.currentTimeMillis(); + if (line.length() > 0) { + // use a max + int max = new Scanner(line).nextInt(); + solver.print(phrase, max); // print all anagrams of phrase + } else { + // no max + solver.print(phrase); // print all anagrams of phrase + } + long endTime = System.currentTimeMillis(); + System.out.println(); + + // 12247 ms elapsed, 2594392 unique LetterInventory object(s) created + if (TIMING) { + long elapsed = endTime - startTime; + int inventories = LetterInventory.getInstanceCount(); + System.out.println(elapsed + " ms elapsed, " + inventories + + " unique LetterInventory object(s) created"); + LetterInventory.resetInstanceCount(); + } + + // get next phrase to solve + phrase = getPhrase(console); + } return "Hello There"; } + + // Helper to prompt for a phrase to generate anagrams. + public static String getPhrase(Scanner console) { + return "hello"; + /* + System.out.println(); + System.out.print("Phrase to scramble (Enter to quit)? "); + String phrase = console.nextLine().trim(); + if (DEBUG) { + phrase = "barbara bush"; + } + return phrase; + */ + } }