Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package spellchecker;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- class SpellSuggest {
- public static final HashMap<String, Integer> TagalogDatabaseWords = new HashMap<String, Integer>();
- /**
- * This initializes the content of the Tagalog Words Database.
- *
- * @param file the dictionary text file
- * @throws IOException
- */
- public SpellSuggest(String file) throws IOException {
- try {
- BufferedReader in = new BufferedReader(new FileReader(file));
- Pattern p = Pattern.compile("\\w+");
- for (String temp = ""; temp != null; temp = in.readLine()) {
- Matcher m = p.matcher(temp.toLowerCase());
- while (m.find()) {
- TagalogDatabaseWords.put((temp = m.group()), TagalogDatabaseWords.containsKey(temp) ? TagalogDatabaseWords.get(temp) + 1 : 1);
- }
- }
- in.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- private final ArrayList<String> edits(String word) {
- ArrayList<String> result = new ArrayList<String>();
- for (int i = 0; i < word.length(); ++i) {
- result.add(word.substring(0, i) + word.substring(i + 1));
- }
- for (int i = 0; i < word.length() - 1; ++i) {
- result.add(word.substring(0, i) + word.substring(i + 1, i + 2) + word.substring(i, i + 1) + word.substring(i + 2));
- }
- for (int i = 0; i < word.length(); ++i) {
- for (char c = 'a'; c <= 'z'; ++c) {
- result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i + 1));
- }
- }
- for (int i = 0; i < word.length(); ++i) {
- for (char c = 'a'; c <= 'z'; ++c) {
- result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
- }
- }
- return result;
- }
- public final String correct(String word) {
- String test = "lmao";
- String[] words = new String[3];
- int[] val = new int[3];
- if (TagalogDatabaseWords.containsKey(word)) {
- words[0] = word;
- return test;
- }
- ArrayList<String> list_edits = edits(word);
- HashMap<Integer, String> candidates = new HashMap<Integer, String>();
- for (String s : list_edits) {
- if (TagalogDatabaseWords.containsKey(s)) {
- if (words[0] == null) {
- words[0] = s;
- val[0] = TagalogDatabaseWords.get(s);
- } else if (words[1] == null) {
- if (TagalogDatabaseWords.get(s) >= val[0]) {
- words[1] = words[0];
- val[1] = val[0];
- words[0] = s;
- val[0] = TagalogDatabaseWords.get(s);
- } else {
- words[1] = s;
- val[1] = TagalogDatabaseWords.get(s);
- }
- } else if (words[2] == null) {
- if (TagalogDatabaseWords.get(s) >= val[0]) {
- words[2] = words[1];
- val[2] = val[1];
- words[1] = words[0];
- val[1] = val[0];
- words[0] = s;
- val[0] = TagalogDatabaseWords.get(s);
- } else if (TagalogDatabaseWords.get(s) >= val[1]) {
- words[2] = words[1];
- val[2] = val[1];
- words[1] = s;
- val[1] = TagalogDatabaseWords.get(s);
- } else {
- words[2] = s;
- val[2] = TagalogDatabaseWords.get(s);
- }
- } else if (TagalogDatabaseWords.get(s) >= val[0]) {
- words[2] = words[1];
- val[2] = val[1];
- words[1] = words[0];
- val[1] = val[0];
- words[0] = s;
- val[0] = TagalogDatabaseWords.get(s);
- } else if (TagalogDatabaseWords.get(s) >= val[1]) {
- words[2] = words[1];
- val[2] = val[1];
- words[1] = s;
- val[1] = TagalogDatabaseWords.get(s);
- } else if (TagalogDatabaseWords.get(s) >= val[2]) {
- words[2] = s;
- val[2] = TagalogDatabaseWords.get(s);
- }
- }
- }
- int ctr = 0;
- for (int i = 0; i < 3; i++) {
- System.out.println(words[i] + " : " + val[i]);
- }
- if (words[2] != null) {
- return test;
- }
- for (String s : list_edits) {
- for (String w : edits(s)) {
- if (TagalogDatabaseWords.containsKey(w)) {
- //candidates.put(TagalogDatabaseWords.get(w), w);
- if(words[0] == null){
- words[0] = w;
- val[0] = TagalogDatabaseWords.get(w);
- }else if(words[1] == null){
- }else if(words[2] == null){
- }
- }
- }
- }
- //return candidates.size() > 0 ? candidates.get(Collections.max(candidates.keySet())) : "Sorry but no possible soluction found";
- test = "kek";
- return test;
- }
- public static void main(String[] args) throws IOException {
- if (args.length > 0) {
- System.out.println((new SpellSuggest("src/tl.txt")).correct(args[0]));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement