Class: StringToNumber::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/string_to_number/parser.rb

Overview

High-performance French text to number parser

This class provides a clean, optimized implementation that maintains compatibility with the original algorithm while adding significant performance improvements through caching and memoization.

Examples:

Basic usage

parser = StringToNumber::Parser.new
parser.parse('vingt et un')  #=> 21
parser.parse('trois millions') #=> 3_000_000

Class method usage

StringToNumber::Parser.convert('mille deux cent') #=> 1200

Constant Summary collapse

WORD_VALUES =

Import the proven data structures from the original implementation

StringToNumber::ToNumber::EXCEPTIONS.freeze
MULTIPLIERS =
StringToNumber::ToNumber::POWERS_OF_TEN.freeze
MULTIPLIER_KEYS =

Pre-compiled regex patterns for optimal performance

MULTIPLIERS.keys.reject { |k| %w[un dix].include?(k) }
.sort_by(&:length).reverse.freeze
MULTIPLIER_PATTERN =
/(?<f>.*?)\s?(?<m>#{MULTIPLIER_KEYS.join('|')})/
QUATRE_VINGT_PATTERN =
/(quatre(-|\s)vingt(s?)((-|\s)dix)?)((-|\s)?)(\w*)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = '') ⇒ Parser

Initialize parser with normalized text



137
138
139
# File 'lib/string_to_number/parser.rb', line 137

def initialize(text = '')
  @normalized_text = self.class.send(:normalize_text, text)
end

Class Method Details

.cache_statsObject

Get cache statistics



78
79
80
81
82
83
84
85
86
87
# File 'lib/string_to_number/parser.rb', line 78

def cache_stats
  @cache_mutex.synchronize do
    {
      conversion_cache_size: @conversion_cache.size,
      conversion_cache_limit: MAX_CACHE_SIZE,
      instance_cache_size: @instance_cache.size,
      cache_hit_ratio: calculate_hit_ratio
    }
  end
end

.clear_caches!Object

Clear all caches



66
67
68
69
70
71
72
73
74
75
# File 'lib/string_to_number/parser.rb', line 66

def clear_caches!
  @cache_mutex.synchronize do
    @conversion_cache.clear
    @cache_access_order.clear
  end
  
  @instance_mutex.synchronize do
    @instance_cache.clear
  end
end

.convert(text) ⇒ Integer

Convert French text to number using cached parser instance

Parameters:

  • text (String)

    French number text to convert

Returns:

  • (Integer)

    The numeric value

Raises:

  • (ArgumentError)

    if text is not a string



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/string_to_number/parser.rb', line 46

def convert(text)
  validate_input!(text)
  
  normalized = normalize_text(text)
  return 0 if normalized.empty?

  # Check conversion cache first
  cached_result = get_cached_conversion(normalized)
  return cached_result if cached_result

  # Get or create parser instance and convert
  parser = get_cached_instance(normalized)
  result = parser.parse_optimized(normalized)
  
  # Cache the result
  cache_conversion(normalized, result)
  result
end

Instance Method Details

#parseObject

Parse the text to numeric value



142
143
144
# File 'lib/string_to_number/parser.rb', line 142

def parse
  self.class.convert(@normalized_text)
end

#parse_optimized(text) ⇒ Object

Internal optimized parsing method using the original proven algorithm but with performance optimizations



148
149
150
151
152
153
154
155
156
# File 'lib/string_to_number/parser.rb', line 148

def parse_optimized(text)
  return 0 if text.nil? || text.empty?
  
  # Direct lookup (fastest path)
  return WORD_VALUES[text] if WORD_VALUES.key?(text)

  # Use the proven extraction algorithm from the original implementation
  extract_optimized(text, MULTIPLIER_KEYS.join('|'))
end