Text completion
Write a program that takes in a user inputted word and prints out possible words that are valid in the English dictionary. Please state any dictionaries or files/binaries/dependencies used in your program. Do show the similarity of the inputted word and outcome as a percentage. Any algorithm can be used to accomplish this task.
- Task
- Resources
Github Repo
Raw Text, Save as .txt file
Hamming Distance
Jaro-Winkler Distance
SoundEx Algorithm
SoundEx Algorithm Wiki
Dice's Coefficient
Dice Coefficient Wiki
- Possible Output
Input word: complition compaction : 80.00% similar. completion : 90.00% similar. completions : 81.82% similar. complexion : 80.00% similar.
- Extension
- How can you make the accuracy of your program higher?
Java
Github Repo Uses dependencies given. <lang Java> import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Scanner;
//uses https://github.com/dwyl/english-words
public class textCompletionConcept {
public static int correct = 0; public static ArrayList<String> listed = new ArrayList<>(); public static void main(String[]args) throws IOException, URISyntaxException { Scanner input = new Scanner(System.in); System.out.println("Input word: "); String errorRode = input.next(); File file = new File(new File(textCompletionConcept.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getPath() + File.separator + "words.txt"); Scanner reader = new Scanner(file); while(reader.hasNext()){ double percent; String compareToThis = reader.nextLine(); char[] s1 = errorRode.toCharArray(); char[] s2 = compareToThis.toCharArray(); int maxlen = Math.min(s1.length, s2.length); for (int index = 0; index < maxlen; index++) { String x = String.valueOf(s1[index]); String y = String.valueOf(s2[index]); if (x.equals(y)) { correct++; } } double length = Math.max(s1.length, s2.length); percent = correct / length; percent *= 100; boolean perfect = false; if (percent >= 80 && compareToThis.charAt(0) == errorRode.charAt(0)) { if(String.valueOf(percent).equals("100.00")){ perfect = true; } String addtoit = compareToThis + " : " + String.format("%.2f", percent) + "% similar."; listed.add(addtoit); } if(compareToThis.contains(errorRode) && !perfect && errorRode.length() * 2 > compareToThis.length()){ String addtoit = compareToThis + " : 80.00% similar."; listed.add(addtoit); } correct = 0; }
for(String x : listed){ if(x.contains("100.00% similar.")){ System.out.println(x); listed.clear(); break; } }
for(String x : listed){ System.out.println(x); } }
} </lang>
- Output
Input word: complition compaction : 80.00% similar. completion : 90.00% similar. completions : 81.82% similar. complexion : 80.00% similar. Process finished with exit code 0
Raku
(formerly Perl 6)
<lang perl6>sub MAIN ( Str $user_word = 'complition', Str $filename = 'words.txt' ) {
my @s1 = $user_word.comb; my @listed = gather for $filename.IO.lines -> $line { my @s2 = $line.comb;
my $correct = 100 * sum( @s1 Zeq @s2) / max(+@s1, +@s2);
my $score = ( $correct >= 100 and @s1[0] eq @s2[0] ) ?? 100 !! ( $correct >= 80 and @s1[0] eq @s2[0] ) ?? $correct !! ( $line.contains($user_word) and @s1 * 2 > @s2 ) ?? 80 !! 0; take [$score, $line] if $score; }
@listed = @listed[$_] with @listed.first: :k, { .[0] == 100 };
say "{.[0].fmt('%.2f')}% {.[1]}" for @listed;
}</lang>
- Output:
80.00% compaction 90.00% completion 81.82% completions 80.00% complexion