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, #source_registry, #url_base

Instance Method Summary collapse

Methods inherited from BaseCache

#available?, #clean, #clear, #clear_all, #download, #get, #initialize, #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



61
62
63
64
65
66
# File 'lib/kotoshu/cache/frequency_cache.rb', line 61

def cached_resources
  directories = Dir.glob(File.join(@cache_path, "*")).select do |path|
    File.directory?(path) && !File.basename(path).start_with?(".")
  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)
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