Beispiel #1
0
  public ParseEssay() {
    System.setProperty("wordnet.database.dir", "../war/dict");
    synonyms = new ArrayList<String>();
    database = WordNetDatabase.getFileInstance();
    baos = new ByteArrayOutputStream();
    lp = LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");

    // ??
  }
Beispiel #2
0
  /**
   * Check if the keyword is in dictionary
   *
   * @param word - the keyword
   * @return <tt>true</tt> - if it is in dictionary, <tt>false</tt> - if not in dictionary
   */
  public static boolean isAWord(String word) {

    WordNetDatabase database = WordNetDatabase.getFileInstance();
    Synset[] synsetNoun = database.getSynsets(word, SynsetType.NOUN);
    Synset[] synsetVerb = database.getSynsets(word, SynsetType.VERB);
    //		System.err.println(synsetNoun.length + synsetVerb.length);

    if (synsetNoun.length + synsetVerb.length > 0) return true;
    else return false;
  }
Beispiel #3
0
  public Wordnet() {
    super();

    // String wordnet_location = getPathDict();
    // System.setProperty("wordnet.database.dir", wordnet_location);
    System.setProperty(
        "wordnet.database.dir", "/Users/angel/Desktop/workspace-miso/metaRDF.core/dict");

    database = WordNetDatabase.getFileInstance();
  }
 public Wordnet() {
   wordnet = WordNetDatabase.getFileInstance();
 }
public class QueryExpansion {

  private Tagger tagger = new Tagger();
  private static Logger logger = Logger.getLogger(QueryExpansion.class);
  private WordNetDatabase database = WordNetDatabase.getFileInstance();

  public QueryExpansion() throws IOException {
    tagger.loadModel("/cmu/arktweetnlp/model.20120919");
    System.setProperty("wordnet.database.dir", GlobalProperty.getInstance().getWordNetPath());
    // System.setProperty("wordnet.database.dir",
    // "/Users/qiaoyu/Documents/E6998_Semantic_Tech_In_IBM_Watson/twitter-semantic-search/search-pipeline/lib/dict");
    logger.info("Done initializing QueryExpansion");
  }

  public String expandQuery(String originalQuery) {
    StringBuilder result = new StringBuilder();

    List<TaggedToken> taggedTokens = tagger.tokenizeAndTag(originalQuery);

    for (TaggedToken token : taggedTokens) {
      if (token.tag.matches("N|O|^|S|Z|L|M|Y|X|!")) {
        result.append(" ");
        result.append(token.token);
        Synset[] synsets = database.getSynsets(token.token, SynsetType.NOUN);
        if (synsets.length > 0) {
          String[] temp = synsets[0].getWordForms();
          for (int j = 0; j < Math.min(temp.length, 3); ++j) {
            result.append(" ");
            result.append(temp[j]);
          }
        }
      } else if (token.tag.matches("V|T")) {
        result.append(" ");
        result.append(token.token);
        Synset[] synsets = database.getSynsets(token.token, SynsetType.VERB);
        if (synsets.length > 0) {
          String[] temp = synsets[0].getWordForms();
          for (int j = 0; j < Math.min(temp.length, 3); ++j) {
            result.append(" ");
            result.append(temp[j]);
          }
        }
      } else if (token.tag.matches("A")) {
        result.append(" ");
        result.append(token.token);
        Synset[] synsets = database.getSynsets(token.token, SynsetType.ADJECTIVE);
        if (synsets.length > 0) {
          String[] temp = synsets[0].getWordForms();
          for (int j = 0; j < Math.min(temp.length, 3); ++j) {
            result.append(" ");
            result.append(temp[j]);
          }
        }
      } else if (token.tag.matches("R")) {
        result.append(" ");
        result.append(token.token);
        Synset[] synsets = database.getSynsets(token.token, SynsetType.ADVERB);
        if (synsets.length > 0) {
          String[] temp = synsets[0].getWordForms();
          for (int j = 0; j < Math.min(temp.length, 3); ++j) {
            result.append(" ");
            result.append(temp[j]);
          }
        }
      }
    }
    logger.info(
        "Before expansion, query = "
            + originalQuery
            + ", after expansion, query = "
            + result.toString().trim());
    return result.toString().trim();
  }

  public static void main(String[] args) throws IOException {
    QueryExpansion qe = new QueryExpansion();
    System.out.println(
        qe.expandQuery(
            "The Political Power of Social Media | Foreign Affairs: http://fam.ag/i5A7Av"));
  }
}
  public Result evaluateModel(
      ModelFile solution,
      ModelFile studentModel,
      Result mistakes,
      ArrayList<EvaluationCriteria> markers,
      double totalMarks)
      throws Exception {
    String wordNetpath = this.getClass().getClassLoader().getResource("").getPath() + "/WordNetDic";
    System.setProperty("wordnet.database.dir", wordNetpath);
    SystemSequenceDiagramReader reader = new SystemSequenceDiagramReader();
    SSD solutionDetails = reader.getRefModelDetails(solution);

    EvaluationResult evaluationResult = new EvaluationResult();

    Double studentMarks = totalMarks;

    try {
      /*
       * Evaluate student's model by comparing with reference model and
       * mistakes that are essential.
       */
      for (EvaluationCriteria marker : markers) {
        if (marker.getType().equals("LifeLine")) {

          for (String lifeline : solutionDetails.getLifelines()) {
            /*
             * If lifeline/class name in student's model does not
             * match exactly with the reference model's
             * lifelines/class name, then look for its synonyms.
             */

            if (!lifeline.toLowerCase().contains(marker.getElementName().toLowerCase())
                || !lifeline.toLowerCase().equals(marker.getElementName().toLowerCase())) {
              boolean variationFound = false;
              WordNetDatabase database = WordNetDatabase.getFileInstance();
              Synset[] synsets = database.getSynsets(lifeline.toLowerCase());
              for (int k = 0; k < synsets.length; k++) {
                String[] wordForms = synsets[k].getWordForms();
                for (int j = 0; j < wordForms.length; j++) {
                  if (wordForms[j].toLowerCase().equals(marker.getElementName().toLowerCase())) {
                    variationFound = true;
                    break;
                  }
                }
              }
              if (!variationFound) {
                studentMarks = deductMarks(studentMarks, marker.isEssential(), marker.getMarks());
                break;
              }
            } else {
              solutionDetails.getLifelines().remove(lifeline);
              break;
            }
          }
        }
      }
      /*
       * Comparing operations and their sequence of student model with
       * evaluators' model.
       */
      for (EvaluationCriteria marker : markers) {
        if (marker.getType().equals("Operation")) {
          for (String operation : solutionDetails.getOperations()) {
            boolean found = false;
            // looking for exact match
            if (!found && !operation.toLowerCase().equals(marker.getElementName().toLowerCase())) {
              found = true;
            }
            /*
             * Find occurrence of reference operation in student's
             * operation
             */
            else if (!found && marker.getElementName().contains("(")) {
              String[] values = marker.getElementName().split("\\(");
              Pattern pattern = Pattern.compile(values[0]);
              Matcher matcher = pattern.matcher(operation);
              while (matcher.find()) {
                found = true;
                break;
              }
            }
            if (!found)
              studentMarks = deductMarks(studentMarks, marker.isEssential(), marker.getMarks());
            else {
              solutionDetails.getOperations().remove(operation);
              break;
            }
          }
        }
      }
      /*
       * Evaluate student's model by checking syntax as well as semantic
       * mistakes and deduct marks of those mistakes that are
       * essential(Specified by evaluator).
       */
      HashMap<String, ArrayList<String>> mistakesResult = new HashMap<String, ArrayList<String>>();

      for (EvaluationCriteria marker : markers) {

        if (marker.getType().equals("Mistake")) {
          for (Errors mistake : mistakes.getErrors()) {

            if (mistake.getErrorDiscrption().contains(marker.getElementName())) {
              if (marker.isEssential() == true) {
                studentMarks = deductMarks(studentMarks, marker.isEssential(), marker.getMarks());

                String error = mistake.getErrorName();

                // for errors
                if (mistake.getType().equals("Error")) {

                  if (mistakesResult.containsKey(error)) {
                    mistakesResult
                        .get(error)
                        .add(mistake.getElementName() + "%" + mistake.getErrorDiscrption());
                  } else {
                    ArrayList<String> list = new ArrayList<>();
                    list.add(mistake.getElementName() + "%" + mistake.getErrorDiscrption());
                    mistakesResult.put(error, list);
                  }
                }

                // for warnings
                if (mistake.getType().equals("Warning")) {

                  if (mistakesResult.containsKey(error)) {
                    mistakesResult
                        .get(error)
                        .add(mistake.getElementName() + "%" + mistake.getErrorDiscrption());
                  } else {
                    ArrayList<String> list = new ArrayList<>();
                    list.add(mistake.getElementName() + "%" + mistake.getErrorDiscrption());
                    mistakesResult.put(error, list);
                  }
                }
              }
            }
          }
        }
      }
      Set<String> keySet = mistakesResult.keySet();
      for (String error : keySet) {

        int count = 0;
        EvaluationResultError resultError = new EvaluationResultError();
        for (String string : mistakesResult.get(error)) {
          String[] token = string.split("%");
          EvaluationResultErrorsDetail detail = new EvaluationResultErrorsDetail();
          detail.setElementName(token[0]);
          detail.setErrorDiscption(token[1]);
          resultError.addDetail(detail);
          count++;
        }

        resultError.setErrorName(error);
        resultError.setErrorCount(count);
        evaluationResult.addErrors(resultError);
      }

      /*
       * Store marks of each student in ArrayList
       */

      evaluationResult.setTotalMarks(totalMarks);
      evaluationResult.setStudentMarks(studentMarks);

    } catch (Exception e) {
      e.printStackTrace();
    }
    Result result = new Result();
    result.setEvaluationResult(evaluationResult);
    return result;
  }