Filip_Markoski

[ADSx] - Spell Checking

Jan 11th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import java.util.stream.Collectors;
  8.  
  9. class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
  10.     K key;
  11.     E value;
  12.  
  13.     public MapEntry(K key, E val) {
  14.         this.key = key;
  15.         this.value = val;
  16.     }
  17.  
  18.     public int compareTo(K that) {
  19.         @SuppressWarnings("unchecked")
  20.         MapEntry<K, E> other = (MapEntry<K, E>) that;
  21.         return this.key.compareTo(other.key);
  22.     }
  23.  
  24.     public String toString() {
  25.         return "(" + key + "," + value + ")";
  26.     }
  27. }
  28.  
  29.  
  30. class OBHT<K extends Comparable<K>, E> {
  31.  
  32.     private MapEntry<K, E>[] buckets;
  33.     static final int NONE = -1; // ... distinct from any bucket index.
  34.     @SuppressWarnings({"rawtypes", "unchecked"})
  35.     private static final MapEntry former = new MapEntry(null, null);
  36.     private int occupancy = 0;
  37.  
  38.     @SuppressWarnings("unchecked")
  39.     public OBHT(int m) {
  40.         buckets = (MapEntry<K, E>[]) new MapEntry[m];
  41.     }
  42.  
  43.     private int hash(K key) {
  44.         return Math.abs(key.hashCode()) % buckets.length;
  45.     }
  46.  
  47.     public MapEntry<K, E> getBucket(int i) {
  48.         return buckets[i];
  49.     }
  50.  
  51.     public int search(K targetKey) {
  52.         int b = hash(targetKey);
  53.         int n_search = 0;
  54.         for (; ; ) {
  55.             MapEntry<K, E> oldEntry = buckets[b];
  56.             if (oldEntry == null)
  57.                 return NONE;
  58.             else if (targetKey.equals(oldEntry.key))
  59.                 return b;
  60.             else {
  61.                 b = (b + 1) % buckets.length;
  62.                 n_search++;
  63.                 if (n_search == buckets.length)
  64.                     return NONE;
  65.             }
  66.         }
  67.     }
  68.  
  69.     public void insert(K key, E val) {
  70.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  71.         int b = hash(key);
  72.         int n_search = 0;
  73.  
  74.         for (; ; ) {
  75.             MapEntry<K, E> oldEntry = buckets[b];
  76.             if (oldEntry == null) {
  77.                 if (++occupancy == buckets.length) {
  78.                     System.out.println("Hash tabelata e polna!!!");
  79.                 }
  80.                 buckets[b] = newEntry;
  81.                 return;
  82.             } else if (oldEntry == former
  83.                     || key.equals(oldEntry.key)) {
  84.                 buckets[b] = newEntry;
  85.                 return;
  86.             } else {
  87.                 b = (b + 1) % buckets.length;
  88.                 n_search++;
  89.                 if (n_search == buckets.length)
  90.                     return;
  91.  
  92.             }
  93.         }
  94.     }
  95.  
  96.     @SuppressWarnings("unchecked")
  97.     public void delete(K key) {
  98.         int b = hash(key);
  99.         int n_search = 0;
  100.         for (; ; ) {
  101.             MapEntry<K, E> oldEntry = buckets[b];
  102.  
  103.             if (oldEntry == null)
  104.                 return;
  105.             else if (key.equals(oldEntry.key)) {
  106.                 buckets[b] = former;
  107.                 return;
  108.             } else {
  109.                 b = (b + 1) % buckets.length;
  110.                 n_search++;
  111.                 if (n_search == buckets.length)
  112.                     return;
  113.  
  114.             }
  115.         }
  116.     }
  117.  
  118.     public String toString() {
  119.         String temp = "";
  120.         for (int i = 0; i < buckets.length; i++) {
  121.             temp += i + ":";
  122.             if (buckets[i] == null)
  123.                 temp += "\n";
  124.             else if (buckets[i] == former)
  125.                 temp += "former\n";
  126.             else
  127.                 temp += buckets[i] + "\n";
  128.         }
  129.         return temp;
  130.     }
  131. }
  132.  
  133.  
  134. class Zbor implements Comparable<Zbor> {
  135.     String zbor;
  136.  
  137.     public Zbor(String zbor) {
  138.         this.zbor = zbor;
  139.     }
  140.  
  141.     @Override
  142.     public boolean equals(Object obj) {
  143.         Zbor pom = (Zbor) obj;
  144.         return this.zbor.equals(pom.zbor);
  145.     }
  146.  
  147.     @Override
  148.     public int hashCode() {
  149.         /*
  150.         *
  151.         * Vie ja kreirate hesh funkcijata
  152.         *
  153.         */
  154.         return zbor.hashCode();
  155.     }
  156.  
  157.     @Override
  158.     public String toString() {
  159.         return zbor;
  160.     }
  161.  
  162.     @Override
  163.     public int compareTo(Zbor arg0) {
  164.         return zbor.compareTo(arg0.zbor);
  165.     }
  166. }
  167.  
  168. public class Speluvanje {
  169.     public static void main(String[] args) throws IOException {
  170.         OBHT<Zbor, String> obht;
  171.         Scanner scanner = new Scanner(System.in);
  172.         int N = Integer.parseInt(scanner.nextLine());
  173.  
  174.         //---Vie odluchete za goleminata na hesh tabelata----
  175.         obht = new OBHT<>(N + 1);
  176.  
  177.         for (int i = 0; i < N; i++) {
  178.             String word = scanner.nextLine().toLowerCase();
  179.             String current = trim(word);
  180.             obht.insert(new Zbor(current), current);
  181.         }
  182.  
  183.         //System.out.println(obht.toString());
  184.  
  185.         String text = scanner.nextLine().toLowerCase();
  186.         String split[] = text.split("\\s+");
  187.  
  188.         /*Matcher matcher= Pattern.compile("\\w+").matcher(text);
  189.         while (matcher.find()){
  190.  
  191.         }*/
  192.  
  193.         List<String> wrongWords = new ArrayList<>();
  194.  
  195.         //System.out.println(Arrays.toString(split));
  196.  
  197.         for (int i = 0; i < split.length; i++) {
  198.             String current = trim(split[i]);
  199.             int searchIndex = obht.search(new Zbor(current));
  200.             if (searchIndex == -1) {
  201.                 //System.out.println(current + " S:" + split[i]);
  202.                 if (current.length() != 0) {
  203.                     wrongWords.add(current);
  204.                 }
  205.             }
  206.         }
  207.         //System.out.println(wrongWords);
  208.         //System.out.println(wrongWords.size());
  209.         if (wrongWords.size() == 0) System.out.println("Bravo");
  210.         else System.out.println(wrongWords.stream()
  211.                 .collect(Collectors.joining("\n")));
  212.  
  213.     }
  214.  
  215.     public static String trim(String word) {
  216.         Character last = word.charAt(word.length() - 1);
  217.         if (!Character.isAlphabetic(last))
  218.             return word.substring(0, word.length() - 1);
  219.         return word;
  220.     }
  221. }
  222.  
  223. /**
  224.  * Write a program to check the spelling of a text. You are given a dictionary of english words, then a text that should be spell-checked. As a result, all incorrectly spelled words, or words not in the dictionary should be printed.
  225.  * <p>
  226.  * Input: First you input the number N, then N lines of english words, then the text to be checked.
  227.  * <p>
  228.  * Output: Print a list of incorrectly spelled words (each on its own line). If all words are correct, print: Bravo.
  229.  * <p>
  230.  * Note: Avoid punctuation marks such as, dot (.), comma (,), exclamation point (!) and question makr (?). Words are case insensitive, i.e. words with first capital letter in the text should be considered correct even if they don't start with capital in the dictionary. Work with open bucket hash table (OBHT). You are left to decide the number of buckets in the table.
  231.  */
Advertisement
Add Comment
Please, Sign In to add comment