<commit_before>require 'osm_leeds'
require 'ckan'

# This whole thing is a bit of a mess - ideally the city should be stored as
# config (perhaps in a DSL) and this data generated that way.

namespace :gsoh do
  
  desc 'Regenerate all the features in the DB'
  task :seed_features => [:destroy_all_features, :seed_nnses, :seed_changing_places, :seed_osm, :regenerate_matches]

  task :destroy_all_features => :environment do
    # Obliterate all the existing features (FIXME)
    Feature.destroy_all
  end
    
  task :seed_osm => :environment do
    leeds = OSMLeeds.new
    OSM_TYPES = {
      'pharmacy'         => 'amenity',
      'doctors'          => 'amenity',
      'place_of_worship' => 'amenity',
      'restaurant'       => 'amenity',
      'pub'              => 'amenity',
      'cafe'             => 'amenity',
      'bank'             => 'amenity',
      'library'          => 'amenity',
      'shop'             => true,
      'sports_centre'    => 'leisure',
      'bus_stop'         => 'highway',
    }
    OSM_TYPES.each do |ftype, tagname|
      puts "Finding #{ftype}:"
      query = case tagname
      when String
        {tagname => ftype}
      else
        # E.g. shop where the ftype is the tag itself
        {ftype => true}
      end
      leeds.query(query).each {|osm| Feature.from_osm(osm).save }
    end
  end
  <commit_msg>Add a retry when rate-limited<commit_after>require 'osm_leeds'
require 'ckan'

# This whole thing is a bit of a mess - ideally the city should be stored as
# config (perhaps in a DSL) and this data generated that way.

namespace :gsoh do
  
  desc 'Regenerate all the features in the DB'
  task :seed_features => [:destroy_all_features, :seed_nnses, :seed_changing_places, :seed_osm, :regenerate_matches]

  task :destroy_all_features => :environment do
    # Obliterate all the existing features (FIXME)
    puts "Destroying existing features (FIXME)..."
    Feature.destroy_all
  end
    
  task :seed_osm => :environment do
    leeds = OSMLeeds.new
    OSM_TYPES = {
      'pharmacy'         => 'amenity',
      'doctors'          => 'amenity',
      'place_of_worship' => 'amenity',
      'restaurant'       => 'amenity',
      'pub'              => 'amenity',
      'cafe'             => 'amenity',
      'bank'             => 'amenity',
      'library'          => 'amenity',
      'shop'             => true,
      'sports_centre'    => 'leisure',
      'bus_stop'         => 'highway',
    }
    OSM_TYPES.each do |ftype, tagname|
      puts "Finding #{ftype}:"
      query = case tagname
      when String
        {tagname => ftype}
      else
        # E.g. shop where the ftype is the tag itself
        {ftype => true}
      end
      begin
        leeds.query(query).each {|osm| Feature.from_osm(osm).save }
      rescue JSON::ParserError => e
        if e.message =~ /rate_limited/
          puts "Oops! Too fast... hold on"
          sleep 3
          redo
        end
        raise
      end
    end
  end
  
{'fraction_non_alphanumeric': 0.11241610738255034, 'fraction_numerical': 0.0003355704697986577, 'mean_word_length': 2.038735983690112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 36, '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': '4711476', 'n_tokens_mistral': 983, 'n_tokens_neox': 974, 'n_words': 272}