Class: Kotoshu::Cache::FrequencyCache

Inherits:
BaseCache
  • Object
show all
Defined in:
lib/kotoshu/cache/frequency_cache.rb

Overview

Frequency cache for Kelly Project frequency lists.

Manages Kelly frequency list downloads from the kotoshu/frequency-list-kelly repository. Resources are cached locally in $XDG_CACHE_HOME/kotoshu/frequency-lists/{code}/ with metadata for versioning and expiration.

Extends BaseCache for common download, metadata, and validation logic.

Examples:

Getting cached frequency data

cache = FrequencyCache.new
result = cache.get('en')
# => { frequency_path: "~/.cache/kotoshu/frequency-lists/en/frequency.json",
#      tiers: { top_50: Set<...>, top_200: Set<...>, top_1000: Set<...> },
#      metadata: { ... } }

Checking if frequency data is available

cache = FrequencyCache.new
available = cache.available?('en')
# => true

Constant Summary collapse

KELLY_LANGUAGES =

Kelly Project languages available

%w[ar zh en el it no ru sv].freeze
GITHUB_REPO =

GitHub repository for Kelly frequency lists

"kotoshu/frequency-list-kelly"
GITHUB_BRANCH =
"main"

Instance Attribute Summary

Attributes inherited from BaseCache

#cache_path, #cache_ttl, #github_url, #max_cache_size, #source_registry, #url_base

Instance Method Summary collapse

Methods inherited from BaseCache

#available?, #clean, #clear, #clear_all, #download, #evict, #get, #initialize, #read_metadata, #reset_stats, #stats

Constructor Details

This class inherits a constructor from Kotoshu::Cache::BaseCache

Instance Method Details

#available_languagesArray<String>

Get list of available languages.

Returns:

  • (Array<String>)

    List of available language codes



37
38
39
# File 'lib/kotoshu/cache/frequency_cache.rb', line 37

def available_languages
  KELLY_LANGUAGES.dup
end

#cached_resourcesArray<String>

List all cached resources.

Returns:

  • (Array<String>)

    List of cached language codes



98
99
100
101
102
103
104
105
106
# File 'lib/kotoshu/cache/frequency_cache.rb', line 98

def cached_resources
  directories = Dir.glob(File.join(@cache_path, "*")).select do |path|
    basename = File.basename(path)
    # Skip dotfiles AND the working-directory scratch slot that
    # BaseCache#initialize creates at <cache_path>/tmp/.
    File.directory?(path) && !basename.start_with?(".") && basename != "tmp"
  end
  directories.map { |path| File.basename(path) }
end

#get_frequency(language_code, force_download: false) ⇒ Hash?

Get frequency data for a language (alias for get).

Parameters:

  • language_code (String)

    ISO 639-1 language code

  • force_download (Boolean) (defaults to: false)

    Force re-download even if cached

Returns:

  • (Hash, nil)

    Frequency data with :frequency_path, :tiers, :metadata keys



46
47
48
# File 'lib/kotoshu/cache/frequency_cache.rb', line 46

def get_frequency(language_code, force_download: false)
  get(language_code, force_download: force_download)
end

#install_local(language_code, path:, force: false) ⇒ Hash

Install a local frequency file into the cache without going through the network. Mirrors LanguageCache#install_local for the frequency resource type — symlinks the user's file into the cache layout and writes a local-source metadata record so subsequent BaseCache#available? / BaseCache#get calls find it.

Parameters:

  • language_code (String)

    ISO 639-1 language code

  • path (String)

    Path to the local frequency.json file

  • force (Boolean) (defaults to: false)

    Overwrite an existing install

Returns:

  • (Hash)

    { frequency_path:, metadata_path:, source: :local }



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kotoshu/cache/frequency_cache.rb', line 68

def install_local(language_code, path:, force: false)
  lang_path = language_dir(language_code)
  FileUtils.mkdir_p(lang_path)

  target_frequency = File.join(lang_path, "frequency.json")
   = (language_code)

  if File.exist?(target_frequency) || File.symlink?(target_frequency)
    raise ArgumentError, "#{target_frequency} already exists (use force: true to overwrite)" unless force

    File.unlink(target_frequency)
  end

  File.symlink(File.expand_path(path), target_frequency)

  (,
                 "version" => Time.now.utc.iso8601,
                 "url" => "local:#{File.expand_path(path)}",
                 "language" => language_code,
                 "type" => "kelly_frequency",
                 "source" => "local",
                 "checksum" => checksum(File.read(path)),
                 "cached_at" => Time.now.utc.iso8601)

  { frequency_path: target_frequency, metadata_path: , source: :local }
end

#supports_resource?(resource_id) ⇒ Boolean

Check if a resource type is supported.

Parameters:

  • resource_id (String)

    The resource identifier (language code)

Returns:

  • (Boolean)

    True if supported



54
55
56
# File 'lib/kotoshu/cache/frequency_cache.rb', line 54

def supports_resource?(resource_id)
  KELLY_LANGUAGES.include?(resource_id)
end