All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cogroo.tools.checker.rules.verbs.Verbs Maven / Gradle / Ivy

There is a newer version: 4.3.1
Show newest version
/**
 * Copyright (C) 2012 cogroo 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cogroo.tools.checker.rules.verbs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.cogroo.analyzer.InitializationException;

/**
 * The Verbs class reads a verb's list as input and turns it into a
 * List
 * 
 */
public class Verbs {

  /**
   * Structure that stores from an input list verbs, its acceptable prepositions
   * and in which case to use them
   */
  private final Map verbsMap;

  private static final Pattern PREP_LINE = Pattern.compile("^\\w+:.*");

  public Verbs() {
    verbsMap = Collections.unmodifiableMap(parseConfiguration());
  }

  public Map parseConfiguration() {
    InputStream input = Verbs.class.getClassLoader().getResourceAsStream(
        "rules/government/verbs.txt");

    Map map = new HashMap();

    try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(input,
          "UTF-8"));

      List preps = null;
      String verb = null;

      while (reader.ready()) {
        String line = reader.readLine();

        if (line.length() > 0) {
        	
        	/* This line is a comment */
        	if (line.charAt(0) == '%')
        		continue;
        	
          if (line.charAt(0) == '#') {

            if (preps != null) {
              map.put(verb, new VerbPlusPreps(preps));
              verb = null;
              preps = null;
            }

            preps = new ArrayList();
            verb = line.substring(1).trim();

          } else if (PREP_LINE.matcher(line).matches()) {
            Prep prep = new Prep();
            String[] words = line.split(":\\s?", 3);
            if (words != null) {
              prep.setPreposition(words[0]);
              if (words.length > 1) {
                prep.setMeaning(words[1]);
                if (words.length > 2)
                  prep.setObjects(words[2]);
              }
            }
            preps.add(prep);
          }
        }
      }
      return map;

    } catch (UnsupportedEncodingException e) {
      // Shouldn't happen because every system contains the utf-8 encode.
      throw new InitializationException(
          "Enconding problem while reading the verbs.txt file", e);
    } catch (IOException e) {
      throw new InitializationException("Could not read the verbs.txt file", e);
    }
  }

  public VerbPlusPreps getVerb(String verb) {
    return this.verbsMap.get(verb);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy