Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/recipe-fiddle.git
<?php
namespace com\whitemagicsoftware;

require "class.Recipe.php";

/**
 * Class that can detect ingredients (using the NLP). Usage:
 * <pre>
 * php class.Snarf.php "recipe*.txt" > ingredients.txt
 * </pre>
 */
class Snarf {
  /**
   * Default constructor.
   */
  function __construct() {
  }

  /**
   * Executes the snarfing based on a set of files to parse.
   *
   * @param $filePattern The pattern of file names to match.
   */
  function run( $filePattern ) {
    $recipe = new Recipe();

    foreach (glob( $filePattern ) as $filename) {
      $lines = file($filename, FILE_IGNORE_NEW_LINES);

      foreach( $lines as $line ) {
        if(
          (strlen( $line ) < 5) ||
          (strpos( $line, ":" ) > 0) ) {
          continue;
        }

        $curl = curl_init();
        $url = "http://localhost:8000?nlp=p&q=" . urlencode( "$line" );

        curl_setopt( $curl, CURLOPT_URL, $url );
        curl_setopt( $curl, CURLOPT_HEADER, 0 );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 0 );

        $result = curl_exec( $curl );

        curl_close( $curl );

        // If there is a unit of measurement, parse it out.
        if( strpos( $result, "Zp" ) > 0 ) {
          $nlp = json_decode( $result );

          $ingredient = $recipe->extractIngredientName( $nlp, true, true );

          echo "$ingredient\n";
        }
      }
    }
  }
}

$snarf = new Snarf();
$snarf->run( $argv[1] );