<commit_before>'use strict';

/* global jsondiffpatch */

angular.module('arethusa.review').service('review', [
  'configurator',
  'state',
  'morph',
  '$rootScope',
  'navigator',
  'plugins',
  function (configurator, state, morph, $rootScope, navigator, plugins) {
    var self = this;
    var retriever;
    var doc;

    this.externalDependencies = [
      "../bower_components/jsondiffpatch/build/bundle.min.js"
    ];

    self.goldTokens = {};

    self.defaultConf = {
      "name" : "review",
      "link" : true,
      "contextMenu" : true,
      "contextMenuTemplate" : "templates/arethusa.review/context_menu.html"
    };

    function DiffCounts() {
      this.tokens = 0;
      this.attrs  = 0;
    }

    function configure() {
      configurator.getConfAndDelegate('review', self);
      configurator.getStickyConf('review', self, ['link', 'autoDiff']);
      self.comparators = [
        'morphology.lemma',
        'morphology.postag',
        'head.id',
        'relation.label'
      ];
      var retrievers = configurator.getRetrievers(self.conf.retrievers);
      retriever = retrievers.TreebankRetriever;
      self.diffActive = false;
    }

    configure();

    function addStyleInfo(tokens) {
      angular.forEach(tokens, function (token, id) {
        var form = token.morphology;
        if (form) {
          morph.postagToAttributes(form);
          token.style = morph.styleOf(form);
        }
      });
    }

    function broadcast() {
      self.diffActive = true;
      $rootScope.$broadcast('diffLoaded');
    }

    self.goToCurrentChunk = function() {
      self.pos = navigator.status.currentPos;
      self.goldTokens = doc[self.pos].tokens;
      addStyleInfo(self.goldTokens);
    };

    function loadDocument() {
      retriever.getData(function (res) {
        doc = res;
        postInit(true);
      });
    }


    function extract(obj) {
      return arethusaUtil.inject({}, obj, function(memo, id, token) {
        memo[id] = arethusaUtil.copySelection(token, self.comparators);
      });
    }

    function countDiffs() {
      var dC = self.diffCounts = new DiffCounts();
      angular.forEach(self.diff, function(d) {
        dC.tokens++;
        angular.forEach(d, function(attr) { dC.attrs++; });
      });
    }

    this.compare = function () {
      self.diff = jsondiffpatch.diff(
        extract(state.tokens),
        extract(self.goldTokens)
      );

      countDiffs();

      angular.forEach(self.diff, function (diff, id) {
        state.setState(id, 'diff', diff);
      });
      broadcast();
    };

    loadDocument();

    function postInit(initialLoad) {
      if (self.link || initialLoad) self.goToCurrentChunk();

      plugins.doAfter('depTree', function() {
        if (self.autoDiff) self.compare();
      });
    }

    this.init = function () {
      configure();

      if (!doc) return; // in case this gets called before we're ready
      postInit();
    };
  }
]);
<commit_msg>Make morph a lazy dependency of review<commit_after>'use strict';

/* global jsondiffpatch */

angular.module('arethusa.review').service('review', [
  'configurator',
  'state',
  '$rootScope',
  'navigator',
  'plugins',
  function (configurator, state, $rootScope, navigator, plugins) {
    var self = this;
    var retriever;
    var doc;

    this.externalDependencies = [
      "../bower_components/jsondiffpatch/build/bundle.min.js"
    ];

    self.goldTokens = {};

    self.defaultConf = {
      "name" : "review",
      "link" : true,
      "contextMenu" : true,
      "contextMenuTemplate" : "templates/arethusa.review/context_menu.html"
    };

    function DiffCounts() {
      this.tokens = 0;
      this.attrs  = 0;
    }

    var lazyMorph;
    function morph() {
      if (!lazyMorph) lazyMorph = plugins.get('morph');
      return lazyMorph;
    }

    function configure() {
      configurator.getConfAndDelegate('review', self);
      configurator.getStickyConf('review', self, ['link', 'autoDiff']);
      self.comparators = [
        'morphology.lemma',
        'morphology.postag',
        'head.id',
        'relation.label'
      ];
      var retrievers = configurator.getRetrievers(self.conf.retrievers);
      retriever = retrievers.TreebankRetriever;
      self.diffActive = false;
    }

    configure();

    function addStyleInfo(tokens) {
      angular.forEach(tokens, function (token, id) {
        var form = token.morphology;
        if (form) {
          morph().postagToAttributes(form);
          token.style = morph().styleOf(form);
        }
      });
    }

    function broadcast() {
      self.diffActive = true;
      $rootScope.$broadcast('diffLoaded');
    }

    self.goToCurrentChunk = function() {
      self.pos = navigator.status.currentPos;
      self.goldTokens = doc[self.pos].tokens;
      addStyleInfo(self.goldTokens);
    };

    function loadDocument() {
      retriever.getData(function (res) {
        doc = res;
        postInit(true);
      });
    }


    function extract(obj) {
      return arethusaUtil.inject({}, obj, function(memo, id, token) {
        memo[id] = arethusaUtil.copySelection(token, self.comparators);
      });
    }

    function countDiffs() {
      var dC = self.diffCounts = new DiffCounts();
      angular.forEach(self.diff, function(d) {
        dC.tokens++;
        angular.forEach(d, function(attr) { dC.attrs++; });
      });
    }

    this.compare = function () {
      self.diff = jsondiffpatch.diff(
        extract(state.tokens),
        extract(self.goldTokens)
      );

      countDiffs();

      angular.forEach(self.diff, function (diff, id) {
        state.setState(id, 'diff', diff);
      });
      broadcast();
    };

    loadDocument();

    function postInit(initialLoad) {
      if (self.link || initialLoad) self.goToCurrentChunk();

      plugins.doAfter('depTree', function() {
        if (self.autoDiff) self.compare();
      });
    }

    this.init = function () {
      configure();

      if (!doc) {
        loadDocument();
      } else {
        postInit();
      }
    };
  }
]);

{'fraction_non_alphanumeric': 0.14382207578253706, 'fraction_numerical': 0.0006589785831960461, 'mean_word_length': 2.3504415011037527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6865310', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1886, 'n_words': 333}