<commit_before>/* Edwin The Assistant

   Copyright 2017 <NAME>

   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.

   Topic of a conversation

 */

'use strict';

const path = require('path');
const fs = require('fs');
const jsonfile = require('jsonfile');

const Statement = require('./Statement');
const Groom = require('./Groom');

/**
 * Topic of a conversation
 * @class Topic
 */
class Topic {
  /**
   * @constructs Topic
   * @param {string} topicId
   */
  constructor(topicId) {
    this.topicId = topicId;
    this.topicFile = path.join(
      __dirname,
      '..',
      'data',
      'topics',
      this.topicId + '.json'
    );
  }

  /**
   * Check if a topic exists
   *
   * @return {Boolean} Topic exists
   */
  exists() {
    return fs.existsSync(this.topicFile);
  }

  /**
   * Return topic annotations
   *
   * @return {Array} Array of annotations
   */
  getAnnotations() {
    if (this.exists()) {
      const annotations = jsonfile.readFileSync(this.topicFile).annotation;
      if (annotations !== undefined) {
        return annotations;
      }
    }
    return [];
  }

  /**
   * Return topic steps
   *
   * @return {Array} Array of steps
   */
  steps() {
    if (this.exists()) {
      return jsonfile.readFileSync(this.topicFile).steps;
    }
    return {};
  }

  /**
   * Set the field annotation from initial statement
   *
   * @param  {Object} state Current state
   */
  applyFieldAnnotationsForTopic(state) {
    const annotation = this.getAnnotations();
    const statement = new Statement(state.getStatement());

    for (let a = 0; a < annotation.length; a++) {
      const rule = annotation[a];

      // substitution can be in start words and such
      const swStatement = new Statement(rule.startWord);
      rule.startWord = swStatement.replaceBracketNotationWithStateFields(state);

      if (state.getField(rule.field) === undefined && rule.startWord !== '') {
        // Process Next Word Of Type
        if (rule.type === 'nextWordOfType') {
          if (state.getField(rule.field) === undefined) {
            state.setField(rule.field,
              statement.getNextWordOfTypeAfterWord(
                rule.startWord,
                rule.wordType,
                this.debug
              )
            );
          }
        }

        // Process everythingAfterWord
        if (rule.type === 'everythingAfterWord') {
          if (state.getField(rule.field) === undefined) {
            state.setField(rule.field,
              statement.getEverythingAfterWord(
                rule.startWord
              )
            );
          }<commit_msg>removed redudant field check from topic annotation<commit_after>/* Edwin The Assistant

   Copyright 2017 <NAME>

   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.

   Topic of a conversation

 */

'use strict';

const path = require('path');
const fs = require('fs');
const jsonfile = require('jsonfile');

const Statement = require('./Statement');
const Groom = require('./Groom');

/**
 * Topic of a conversation
 * @class Topic
 */
class Topic {
  /**
   * @constructs Topic
   * @param {string} topicId
   */
  constructor(topicId) {
    this.topicId = topicId;
    this.topicFile = path.join(
      __dirname,
      '..',
      'data',
      'topics',
      this.topicId + '.json'
    );
  }

  /**
   * Check if a topic exists
   *
   * @return {Boolean} Topic exists
   */
  exists() {
    return fs.existsSync(this.topicFile);
  }

  /**
   * Return topic annotations
   *
   * @return {Array} Array of annotations
   */
  getAnnotations() {
    if (this.exists()) {
      const annotations = jsonfile.readFileSync(this.topicFile).annotation;
      if (annotations !== undefined) {
        return annotations;
      }
    }
    return [];
  }

  /**
   * Return topic steps
   *
   * @return {Array} Array of steps
   */
  steps() {
    if (this.exists()) {
      return jsonfile.readFileSync(this.topicFile).steps;
    }
    return {};
  }

  /**
   * Set the field annotation from initial statement
   *
   * @param  {Object} state Current state
   */
  applyFieldAnnotationsForTopic(state) {
    const annotation = this.getAnnotations();
    const statement = new Statement(state.getStatement());

    for (let a = 0; a < annotation.length; a++) {
      const rule = annotation[a];

      // substitution can be in start words and such
      const swStatement = new Statement(rule.startWord);
      rule.startWord = swStatement.replaceBracketNotationWithStateFields(state);

      if (state.getField(rule.field) === undefined && rule.startWord !== '') {
        // Process Next Word Of Type
        if (rule.type === 'nextWordOfType') {
          state.setField(rule.field,
            statement.getNextWordOfTypeAfterWord(
              rule.startWord,
              rule.wordType,
              this.debug
            )
          );
        }

        // Process everythingAfterWord
        if (rule.type === 'everythingAfterWord') {
          state.setField(rule.field,
            statement.getEverythingAfterWord(
              rule.startWord
            )
          );
{'fraction_non_alphanumeric': 0.11145713817406983, 'fraction_numerical': 0.0029503360104900836, 'mean_word_length': 2.325340599455041, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1618607', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1674, 'n_words': 500}