? 726088-5_placemaker.patch
? 726088-6_placemaker.patch
? PATCHES.txt
Index: ExtractorSimplePieParser.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/extractor/ExtractorSimplePieParser.inc,v
retrieving revision 1.1
diff -u -p -r1.1 ExtractorSimplePieParser.inc
--- ExtractorSimplePieParser.inc	4 Dec 2009 15:01:46 -0000	1.1
+++ ExtractorSimplePieParser.inc	27 Feb 2010 03:42:04 -0000
@@ -1,6 +1,8 @@
 <?php
 // $Id: ExtractorSimplePieParser.inc,v 1.1 2009/12/04 15:01:46 alexb Exp $
 
+require_once 'extractor.feeds.inc';
+
 /**
  * Extend simplepie parser and add term extraction.
  */
@@ -10,17 +12,7 @@ class ExtractorSimplePieParser extends F
    * Add additional mapping sources.
    */
   public function getMappingSources() {
-    $sources = array(
-      'extracted_terms' => array(
-        'name' => t('Extracted term names'),
-        'description' => t('Array of term names extracted from title and or description.'),
-      ),
-      'extracted_tids' => array(
-        'name' => t('Extracted term tids'),
-        'description' => t('Array of term ids (tid) of term names extracted from title and or description.'),
-      ),
-    );
-    return parent::getMappingSources() + $sources;
+    return parent::getMappingSources() + extractor_feeds_get_mapping_sources();
   }
 
   /**
@@ -28,15 +20,23 @@ class ExtractorSimplePieParser extends F
    * them to item.
    */
   public function getSourceElement($item, $element_key) {
-    if ($element_key == 'extracted_terms') {
-      return extractor_extract($item['title'] .' '. $item['description']);
-    }
-    elseif ($element_key == 'extracted_tids') {
-      $terms = extractor_extract($item['title'] .' '. $item['description']);
-      return array_keys($terms);
-    }
-    else {
-      return parent::getSourceElement($item, $element_key);
+    if ($result = extractor_feeds_get_source_element($item, $element_key, $this->config)) {
+      return $result;
     }
+    return parent::getSourceElement($item, $element_key);
+  }
+
+  /**
+    * Config Form Defaults
+    */
+  public function configDefaults() {
+    return extractor_feeds_defaults();
+  }
+
+  /**
+    * Config Form
+    */
+  public function configForm(&$form_state) {
+    return extractor_feeds_form($this->config);
   }
-}
+}
\ No newline at end of file
Index: ExtractorSyndicationParser.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/extractor/ExtractorSyndicationParser.inc,v
retrieving revision 1.1
diff -u -p -r1.1 ExtractorSyndicationParser.inc
--- ExtractorSyndicationParser.inc	4 Dec 2009 15:01:46 -0000	1.1
+++ ExtractorSyndicationParser.inc	27 Feb 2010 03:42:04 -0000
@@ -1,6 +1,8 @@
 <?php
 // $Id: ExtractorSyndicationParser.inc,v 1.1 2009/12/04 15:01:46 alexb Exp $
 
+require_once 'extractor.feeds.inc';
+
 /**
  * Extend common syndication parser and add term extraction.
  */
@@ -10,17 +12,7 @@ class ExtractorSyndicationParser extends
    * Add additional mapping sources.
    */
   public function getMappingSources() {
-    $sources = array(
-      'extracted_terms' => array(
-        'name' => t('Extracted term names'),
-        'description' => t('Array of term names extracted from title and or description.'),
-      ),
-      'extracted_tids' => array(
-        'name' => t('Extracted term tids'),
-        'description' => t('Array of term ids (tid) of term names extracted from title and or description.'),
-      ),
-    );
-    return parent::getMappingSources() + $sources;
+    return parent::getMappingSources() + extractor_feeds_get_mapping_sources();
   }
 
   /**
@@ -28,15 +20,23 @@ class ExtractorSyndicationParser extends
    * them to item.
    */
   public function getSourceElement($item, $element_key) {
-    if ($element_key == 'extracted_terms') {
-      return extractor_extract($item['title'] .' '. $item['description']);
-    }
-    elseif ($element_key == 'extracted_tids') {
-      $terms = extractor_extract($item['title'] .' '. $item['description']);
-      return array_keys($terms);
-    }
-    else {
-      return parent::getSourceElement($item, $element_key);
+    if ($result = extractor_feeds_get_source_element($item, $element_key, $this->config)) {
+      return $result;
     }
+    return parent::getSourceElement($item, $element_key);
+  }
+
+  /**
+   * Config Form Defaults
+   */
+  public function configDefaults() {
+    return extractor_feeds_defaults();
+  }
+
+  /**
+   * Config Form
+   */
+  public function configForm(&$form_state) {
+    return extractor_feeds_form($this->config);
   }
-}
+}
\ No newline at end of file
Index: README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/extractor/README.txt,v
retrieving revision 1.1
diff -u -p -r1.1 README.txt
--- README.txt	4 Dec 2009 15:01:46 -0000	1.1
+++ README.txt	27 Feb 2010 03:42:04 -0000
@@ -20,3 +20,23 @@ With Feeds module:
 As API:
 
 $terms = extractor_extract($text);
+
+Yahoo Placemaker
+================
+
+The default tagging library in use is "Simple Extractor" which is a simple
+look up algorithm based on a terms in a taxonomy vocabulary. Alternatively, the
+Yahoo Placemaker API can be used as tagging library.
+
+To use Yahoo Placemaker with Feeds module, click on the settings form of the
+term extraction parser and select "Yahoo Placemaker" as extraction library. Then
+supply an API key and if a different language than English should be used for
+terms, specify the language code.
+
+To use Yahoo Placemaker on the API level, call:
+
+$terms = extractor_extract($text, 'placemaker', array('placemaker_key' => '<MYPMKEY>'));
+
+or:
+
+$terms = extractor_extract($text, 'placemaker', array('placemaker_key' => '<MYPMKEY>'), 'language' => 'fr');
\ No newline at end of file
Index: extractor.feeds.inc
===================================================================
RCS file: extractor.feeds.inc
diff -N extractor.feeds.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ extractor.feeds.inc	27 Feb 2010 03:42:04 -0000
@@ -0,0 +1,81 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Feeds integration for extractor.
+ *
+ * @see ExtractorSimplePieParser class
+ * @see ExtractorSyndicationParser class
+ */
+
+/**
+ * Form for feeds integration.
+ */
+function extractor_feeds_form($config) {
+  drupal_add_js(drupal_get_path('module', 'extractor') .'/extractor.js');
+  $form = array();
+  $form['extractors'] = array(
+    '#type' => 'radios',
+    '#title' => t('Extractor libraries'),
+    '#description' => t('Select the library to use for term extraction.'),
+    '#default_value' => $config['extractors'],
+    '#options' => array(
+      'extractor_simple' => t('Simple Extractor'),
+      'placemaker' => t('Yahoo Placemaker')
+    ),
+  );
+  $form['placemaker_key'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Yahoo API Key'),
+    '#description' => t('Supply an !api_key for connecting to the Yahoo Placemaker API.', array('!api_key' => l(t('API key'), 'http://developer.yahoo.com/geo/placemaker/'))),
+    '#default_value' => $config['placemaker_key'],
+  );
+  $form['language'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Language'),
+    '#description' => t('Use specified language (RFC 4646) for place name recognition return of place names.'),
+    '#default_value' => $config['language'],
+  );
+  return $form;
+}
+
+/**
+ * Defaults for feeds integration.
+ */
+function extractor_feeds_defaults() {
+  return array(
+    'extractors' => 'extractor_simple',
+    'language' => 'en-US',
+    'placemaker_key' => '',
+  );
+}
+
+/**
+ * Return a list of available mapping sources.
+ */
+function extractor_feeds_get_mapping_sources() {
+  return array(
+    'extracted_terms' => array(
+      'name' => t('Extracted term names'),
+      'description' => t('Array of term names extracted from title and or description.'),
+    ),
+    'extracted_tids' => array(
+      'name' => t('Extracted term tids'),
+      'description' => t('Array of term ids (tid) of term names extracted from title and or description.'),
+    ),
+  );
+}
+
+/**
+ * Retrieve a source element for an element key.
+ */
+function extractor_feeds_get_source_element($item, $element_key, $config) {
+  if ($element_key == 'extracted_terms') {
+    return extractor_extract($item['title'] .' '. $item['description'], $config['extractors'], $config);
+  }
+  elseif ($element_key == 'extracted_tids') {
+    $terms = extractor_extract($item['title'] .' '. $item['description'], $config['extractors'], $config);
+    return array_keys($terms);
+  }
+}
\ No newline at end of file
Index: extractor.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/extractor/extractor.info,v
retrieving revision 1.1
diff -u -p -r1.1 extractor.info
--- extractor.info	4 Dec 2009 15:01:46 -0000	1.1
+++ extractor.info	27 Feb 2010 03:42:04 -0000
@@ -2,4 +2,10 @@
 name = Extractor
 description = "API for extracting terms from text"
 dependencies[] = taxonomy
-core = 6.x
\ No newline at end of file
+core = 6.x
+; Information added by drupal.org packaging script on 2009-12-04
+version = "6.x-1.0-alpha1"
+core = "6.x"
+project = "extractor"
+datestamp = "1259940075"
+
Index: extractor.js
===================================================================
RCS file: extractor.js
diff -N extractor.js
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ extractor.js	27 Feb 2010 03:42:04 -0000
@@ -0,0 +1,24 @@
+// $Id$
+
+/**
+ * Conditional show behavior for Feeds importer form.
+ * @todo: simplify.
+ */
+Drupal.behaviors.extractor = function() {
+  if ($('#edit-extractors-placemaker').attr('checked')) {
+    $('#edit-placemaker-key-wrapper').show();
+    $('#edit-language-wrapper').show();
+  }
+  else {
+    $('#edit-placemaker-key-wrapper').hide();
+    $('#edit-language-wrapper').hide();
+  }
+  $('#edit-extractors-extractor-simple').click(function() {
+    $('#edit-placemaker-key-wrapper').hide(100);
+    $('#edit-language-wrapper').hide(100);
+  });
+  $('#edit-extractors-placemaker').click(function() {
+    $('#edit-placemaker-key-wrapper').show(100);
+    $('#edit-language-wrapper').show(100);
+  });
+}
\ No newline at end of file
Index: extractor.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/extractor/extractor.module,v
retrieving revision 1.1
diff -u -p -r1.1 extractor.module
--- extractor.module	4 Dec 2009 15:01:46 -0000	1.1
+++ extractor.module	27 Feb 2010 03:42:04 -0000
@@ -78,11 +78,22 @@ function extractor_settings_form() {
 /**
  * Main API function.
  *
- * @todo: make pluggable.
+ * @param $text
+ *   The text to perform the term extraction on.
+ * @param $library
+ *   The extraction library to use. Possible options: 'extractor_simple' or
+ *   'placemaker'.
+ * @param $config
+ *   An array containing a configuration for the library in use.
+ *
+ * @return
+ *   An array of terms found in $text. They keys of the array contain the
+ *   taxonomy term ids and the values the term names.
  */
-function extractor_extract($text) {
-  extractor_include('extractor_simple');
-  return extractor_simple_extract($text);
+function extractor_extract($text, $library = 'extractor_simple', $config = array()) {
+  extractor_include($config['extractors']);
+  $extract = "{$library}_extract";
+  return $extract($text, $config);
 }
 
 /**
@@ -94,4 +105,4 @@ function extractor_include($name) {
     include drupal_get_path('module', 'extractor') .'/libraries/'. $name .'.inc';
     $included[$name] = TRUE;
   }
-}
\ No newline at end of file
+}
Index: libraries/placemaker.inc
===================================================================
RCS file: libraries/placemaker.inc
diff -N libraries/placemaker.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libraries/placemaker.inc	27 Feb 2010 03:42:04 -0000
@@ -0,0 +1,80 @@
+<?php
+// $Id$
+
+/**
+ * Tags a single item.
+ */
+function placemaker_extract($text, $config) {
+  // TODO: ALLOW PER EXTRACTOR VOCABS?
+  $vid = variable_get('extractor_simple_vid', 1);
+  $term = _placemaker_send_text($text, $vid, $config);
+  $tags = array();
+  if ($term) {
+    $tags[$term['tid']] = $term;
+  }
+  return $tags;
+}
+
+/**
+ * Sends items to Placemaker API.
+ */
+function _placemaker_send_text($text, $vid, $config) {
+  // Make placemaker request.
+  $apiendpoint = 'http://wherein.yahooapis.com/v1/document';
+  $inputType = 'text/html';
+
+  $outputType = 'xml';
+  $post = 'appid='. $config['placemaker_key'] .'&documentContent='. $text .'&documentType='. $inputType .'&inputLanguage='. $config['language'] .'&outputType='. $outputType .'&autoDisambiguate=true';
+  $ch = curl_init($apiendpoint);
+
+  curl_setopt($ch, CURLOPT_POST, 1);
+  curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
+  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+  $results = curl_exec($ch);
+  curl_close($ch);
+
+  // Parse results of placemaker request.
+  $xml = simplexml_load_string($results);
+  $item['vid'] = $vid;
+  $item['name'] = (string)$xml->document->placeDetails->place->name;
+  $item['external_id'] = (string)$xml->document->placeDetails->place->woeId;
+  $item['lat'] = (string)$xml->document->placeDetails->place->centroid->latitude;
+  $item['lon'] = (string)$xml->document->placeDetails->place->centroid->longitude;
+  $item['description'] = NULL;
+
+  if ($item['name']) {
+    $tags = _placemaker_tag_text($item, $config);
+  }
+
+  return $tags;
+}
+
+/**
+ * Helper function for tagging a text.
+ */
+function _placemaker_tag_text($item, $config) {
+  // Check to see if the term already exists.
+  $result = db_query('SELECT tid, external_id FROM {term_geo} WHERE external_id = "%s"', $item['external_id']);
+  if ($term = db_fetch_object($result)) {
+    $item['tid'] = $term->tid;
+    $item['name'] = $term->name;
+    return $item;
+  }
+  // Otherwise create new term and add geotaxonomy info.
+  else {
+    // Separate call to Yahoo GeoPlanet API to return placename in language of choice if not US English.
+    if (!empty($config['language']) && $config['language'] != 'en_US') {
+      $apiendpoint = 'http://where.yahooapis.com/v1/place/'. $item['external_id'] .'?appid='. $config['placemaker_key'] .'&lang='. $config['language'] .'&select=short';
+      $ch = curl_init($apiendpoint);
+      $headers = array('Accept: application/xml');
+      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+      $results = curl_exec($ch);
+      curl_close($ch);
+      $xml = simplexml_load_string($results);
+      $item['name'] = (string)$xml->name;
+    }
+    taxonomy_save_term($item);
+    return $item;
+  }
+}
\ No newline at end of file
