<commit_before>

// Copyright (c) 2017 Google Inc. All rights reserved.
// This code may only be used under the BSD style license found at
// http://polymer.github.io/LICENSE.txt
// Code distributed by Google as part of this project is also
// subject to an additional IP rights grant found at
// http://polymer.github.io/PATENTS.txt

let {Strategy} = require('../../strategizer/strategizer.js');
let Recipe = require('../recipe/recipe.js');
let RecipeWalker = require('../recipe/walker.js');

module.exports = class SearchTokensToParticles extends Strategy {
  constructor(arc) {
    super();
    // TODO: Recipes. Views?
    this._byToken = {};
    for (let particle of arc.context.particles) {
      let name = particle.name.toLowerCase();
      this._byToken[name] = this._byToken[name] || [];
      this._byToken[name].push(particle);
    }
  }
  async generate(strategizer) {
    let findParticles = token => this._byToken[token] || [];
    var results = Recipe.over(strategizer.generated, new class extends RecipeWalker {
      onRecipe(recipe) {
        if (/*!recipe.isResolved() ||*/ !recipe.search || !recipe.search.unresolvedTokens.length) {
          return;
        }

        let specByToken = new Map();
        for (let token of recipe.search.unresolvedTokens) {
          for (let spec of findParticles(token)) {
            // TODO: Skip particles that are already in the active recipe?
            specByToken.set(token, spec);
          }
        }
        if (specByToken.size == 0) {
          return;
        }

        return recipe => {
          specByToken.forEach((spec, token) => {
            recipe.search.resolveToken(token);
            let particle = recipe.newParticle(spec.name);
            particle.spec = spec;
          });<commit_msg>Add TODO to handle recipe.isResolved and search terms.<commit_after>

// Copyright (c) 2017 Google Inc. All rights reserved.
// This code may only be used under the BSD style license found at
// http://polymer.github.io/LICENSE.txt
// Code distributed by Google as part of this project is also
// subject to an additional IP rights grant found at
// http://polymer.github.io/PATENTS.txt

let {Strategy} = require('../../strategizer/strategizer.js');
let Recipe = require('../recipe/recipe.js');
let RecipeWalker = require('../recipe/walker.js');

module.exports = class SearchTokensToParticles extends Strategy {
  constructor(arc) {
    super();
    // TODO: Recipes. Views?
    this._byToken = {};
    for (let particle of arc.context.particles) {
      let name = particle.name.toLowerCase();
      this._byToken[name] = this._byToken[name] || [];
      this._byToken[name].push(particle);
    }
  }
  async generate(strategizer) {
    let findParticles = token => this._byToken[token] || [];
    var results = Recipe.over(strategizer.generated, new class extends RecipeWalker {
      onRecipe(recipe) {
        // TODO: according to design, the search strategy activates when the recipe is resolved
        // OR when the recipe is a terminal case (did not generate descendants from any other strategies).
        if (/*!recipe.isResolved() ||*/ !recipe.search || !recipe.search.unresolvedTokens.length) {
          return;
        }

        let specByToken = new Map();
        for (let token of recipe.search.unresolvedTokens) {
          for (let spec of findParticles(token)) {
            // TODO: Skip particles that are already in the active recipe?
            specByToken.set(token, spec);
          }
        }
        if (specByToken.size == 0) {
          return;
        }

        return recipe => {
          specByToken.forEach((spec, token) => {
            recipe.search.resolveToken(token);
            let particle = recipe.newParticle(spec.name);
            particle.spec = spec;
          });
{'fraction_non_alphanumeric': 0.12355110642781876, 'fraction_numerical': 0.0026343519494204425, 'mean_word_length': 2.515740740740741, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5209962', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1062, 'n_words': 304}