Acronym finder which also scan characters inside a word

82 Views Asked by At

I am trying to make an acronym generator which can generate acronym from characters not only from the first character of word but also character inside a word. I did some programming in Java (https://github.com/hkkongou/acronym/blob/main/Trie.java), it does find some acronyms. However, not all acronym are scanned. I then found a very good website which does the thing that I want. https://acronymify.com/ . But it does not provide any source code for me to learn. And I am stuck right now.

I used java version of https://www.geeksforgeeks.org/trie-insert-and-search/ as example and modify so that my program can output searched acronym. Given "National Aeronautics and Space Administration", I turn these words into lower case and remove spaces "nationalaeronauticsandspaceadministration", then use two loops and loop all the characters from left to right and see whether these characters have acronyms.

Below are the codes I used to search acronyms, I don't know how to make it able to find other acronyms when it successfully found 1 acronym. Relaunching the search function will always have the same result.

example of input: hierarCHical taxonomic classification for viral mEtagEnomic data via deep leaRning

output: ieroe, archical, caltanissetta (and other words)

expected output: cheer (and other words)

"cheer" is an example of words that haven't been scanned during the runtime <-- my problem

public class Trie extends JFrame {
    static final int ALPHABET_SIZE = 26;
    static class TrieNode {
        TrieNode[] children = new TrieNode[ALPHABET_SIZE];
        boolean isEndOfWord;
        TrieNode() {
            isEndOfWord = false;
            for (int i = 0; i < ALPHABET_SIZE; i++) {
                children[i] = null;
            }
        }
    };
    static TrieNode root;
    static void insert(String key) {
        int level;
        int length = key.length();
        int index;
        TrieNode pCrawl = root;
        for (level = 0; level < length; level++) {
            index = key.charAt(level) - 'a';
            if (pCrawl.children[index] == null) {
                pCrawl.children[index] = new TrieNode();
            }
            pCrawl = pCrawl.children[index];
        }
        pCrawl.isEndOfWord = true;
    }
    public static ArrayList<String> words = new ArrayList<String>();
    public static ArrayList<String> words2 = new ArrayList<String>();
    static boolean search(String key) {
        int level;
        int length = key.length();
        int index;
        TrieNode pCrawl = root;
        String sentence = "";
        StringBuilder updateString = new StringBuilder(key);
        for (level = 0; level < length; level++) {
            index = key.charAt(level) - 'a';
            if (pCrawl.children[index] == null) {
            } else {
                pCrawl = pCrawl.children[index];
                sentence = sentence + key.charAt(level);
                updateString.setCharAt(level, Character.toUpperCase(key.charAt(level)));
            }
        }
        if (pCrawl.isEndOfWord) {
            words.add(sentence);
            words2.add(updateString.toString());
        } else {
            sentence = "";
        }
        return (pCrawl.isEndOfWord);
    }
    static void multisearch(String key) {
        for (int i = 0; i < key.length(); i++) {
            search(key.substring(i, key.length()));
        }
    }
    public static void main(String args[]) throws MalformedURLException, IOException {
        Scanner myObj = new Scanner(System.in);
        String data = myObj.nextLine();  // Read user input
        run(data);
        System.out.println(TempA);
    }
    public static String TempA = "";
    public static String TempB = "";
    public static void run(String input) throws MalformedURLException, IOException {
        words.clear();
        words2.clear();
        ArrayList<String> ar = new ArrayList<String>();
        URL url = new URL("https://raw.githubusercontent.com/dwyl/english-words/master/words.txt");
        Scanner sc = new Scanner(url.openStream());
        StringBuffer sb = new StringBuffer();
        String temp = "";
        while (sc.hasNext()) {
            temp = sc.next();
            temp = temp.toLowerCase();
            temp = temp.replaceAll("[^A-Za-z]", "");
            if (temp.length() > 1) {
                ar.add(temp);
            }
        }
        String[] keys = new String[ar.size()];
        keys = ar.toArray(keys);
        String output[] = {"Not present in trie", "Present in trie"};
        root = new TrieNode();
        for (int i = 0; i < keys.length; i++)
            insert(keys[i]);
        input = input.toLowerCase().replace(" ", "");
        System.out.println(input);
        multisearch(input);
        TempA = TempB = "";
        for (int i = 0; i < words2.size(); i++) {
            TempA = TempA + "\n" + words.get(i);
            TempB = TempB + "\n" + input.substring(0, input.length() - words2.get(i).length()) + words2.get(i);
        }
    }
}
0

There are 0 best solutions below