Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.nio.file.Files;
  3. import java.nio.file.Paths;
  4. import java.nio.file.StandardOpenOption;
  5. import java.util.HashMap;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import java.util.Map.Entry;
  9. import java.util.TreeSet;
  10. import java.util.concurrent.ExecutionException;
  11. import java.util.concurrent.atomic.AtomicBoolean;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14.  
  15. /**
  16.  * @author Two
  17.  */
  18. public class Grammar {
  19.  
  20.   static final Pattern replacePattern = Pattern.compile("\\p{IsAlphabetic}+\\->\\p{IsAlphabetic}+");
  21.  
  22.   public static Grammar fromInputLines(final String mapping, final String target) {
  23.     final HashMap<String, String> replacements = new HashMap<>();
  24.     final Matcher matcher = replacePattern.matcher(mapping);
  25.     while (matcher.find()) {
  26.       final String replacement = matcher.group();
  27.       final int delimiter = replacement.indexOf("->");
  28.       final String from = replacement.substring(0, delimiter);
  29.       final String to = replacement.substring(delimiter + 2);
  30.       replacements.put(to, from);
  31.     }
  32.     return new Grammar(replacements, target);
  33.   }
  34.   protected final HashMap<String, String> reversedRules;
  35.   protected final String root;
  36.  
  37.   public Grammar(final HashMap<String, String> reversedRules, final String root) {
  38.     this.reversedRules = reversedRules;
  39.     this.root = root;
  40.   }
  41.  
  42.   public boolean verifyWord(final String word) {
  43.     TreeSet<String> wordConstructs = new TreeSet<>();
  44.     wordConstructs.add(word);
  45.  
  46.     final AtomicBoolean found = new AtomicBoolean(false);
  47.     while ((found.get() == false) && (wordConstructs.isEmpty() == false)) {
  48.       wordConstructs = wordConstructs.parallelStream()
  49.               .collect(TreeSet::new,
  50.                       (list, wordConstruct) -> {
  51.                         if (root.equals(wordConstruct)) {
  52.                           found.set(true);
  53.                         } else {
  54.                           for (final Entry<String, String> entry : reversedRules.entrySet()) {
  55.                             final String variable = entry.getKey();
  56.                             int pos = wordConstruct.indexOf(variable);
  57.                             while (pos >= 0) {
  58.                               list.add(new StringBuilder().append(wordConstruct).replace(pos, pos + variable.length(), entry.getValue()).toString());
  59.                               pos = wordConstruct.indexOf(variable, pos + 1);
  60.                             }
  61.                           }
  62.                         }
  63.                       },
  64.                       TreeSet::addAll);
  65.     }
  66.     return found.get();
  67.   }
  68.  
  69.   public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
  70.     if (args.length < 1) {
  71.       System.err.println("Usage: Grammar <inputFile>");
  72.     } else {
  73.       final List<String> resultLines = new LinkedList<>();
  74.       final List<String> lines = Files.readAllLines(Paths.get(args[0]));
  75.       int offset = 0;
  76.       for (int i = lines.size() / 5; i > 0; --i) {
  77.         final Grammar grammar = fromInputLines(lines.get(offset + 2), lines.get(offset + 3));
  78.         final StringBuilder resultBuilder = new StringBuilder();
  79.         for (final String word : lines.get(offset + 4).split(",")) {
  80.           resultBuilder.append(word).append(":").append(grammar.verifyWord(word)).append("; ");
  81.         }
  82.         resultLines.add(resultBuilder.toString());
  83.         offset += 5;
  84.       }
  85.       Files.write(Paths.get("output.txt"), resultLines, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
  86.     }
  87.   }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement