A high-performance Ruby gem for converting French written numbers into their numeric equivalents. Features intelligent caching, thread-safe operations, and support for complex French number formats.
- High Performance: Up to 460x faster than naive implementations with intelligent caching
- Thread-Safe: Concurrent access support with proper locking mechanisms
- Comprehensive: Handles complex French number formats including:
- Basic numbers (zΓ©ro, un, deux...)
- Compound numbers (vingt et un, quatre-vingt-quatorze...)
- Large numbers (millions, milliards, billions...)
- Special cases (quatre-vingts, soixante-dix...)
- Memory Efficient: LRU cache with configurable limits
- Backward Compatible: Maintains compatibility with original implementation
Input Size | Original | Optimized | Improvement |
---|---|---|---|
Short | 0.5ms | 0.035ms | 14x |
Medium | 2.1ms | 0.045ms | 47x |
Long | 23ms | 0.05ms | 460x |
Add this line to your application's Gemfile:
gem 'string_to_number'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install string_to_number
require 'string_to_number'
# Simple numbers
StringToNumber.in_numbers('zΓ©ro') #=> 0
StringToNumber.in_numbers('quinze') #=> 15
StringToNumber.in_numbers('cent') #=> 100
# Compound numbers
StringToNumber.in_numbers('vingt et un') #=> 21
StringToNumber.in_numbers('quatre-vingt-quatorze') #=> 94
StringToNumber.in_numbers('soixante-dix') #=> 70
# Large numbers
StringToNumber.in_numbers('mille deux cent trente-quatre') #=> 1234
StringToNumber.in_numbers('un million') #=> 1_000_000
StringToNumber.in_numbers('trois milliards') #=> 3_000_000_000
# Complex expressions
StringToNumber.in_numbers('neuf mille neuf cent quatre-vingt-dix-neuf') #=> 9999
StringToNumber.in_numbers('deux millions trois cent mille') #=> 2_300_000
# Validation
StringToNumber.valid_french_number?('vingt et un') #=> true
StringToNumber.valid_french_number?('hello world') #=> false
# Cache management
StringToNumber.clear_caches! # Clear all internal caches
stats = StringToNumber.cache_stats
puts "Cache hit ratio: #{stats[:cache_hit_ratio]}"
# Backward compatibility mode
StringToNumber.in_numbers('cent', use_optimized: false) #=> 100
Range | Examples |
---|---|
0-19 | zΓ©ro, un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix, onze, douze, treize, quatorze, quinze, seize, dix-sept, dix-huit, dix-neuf |
20-99 | vingt, trente, quarante, cinquante, soixante, soixante-dix, quatre-vingts, quatre-vingt-dix |
100+ | cent, mille, million, milliard, billion |
Compounds | vingt et un, quatre-vingt-quatorze, deux mille trois |
- Reuse conversions: The gem automatically caches results for better performance
- Batch processing: Use the optimized parser (default) for better throughput
- Memory management: Call
clear_caches!
periodically if processing many unique inputs - Thread safety: The gem is thread-safe and can be used in concurrent environments
After checking out the repo, run bin/setup
to install dependencies:
$ git clone https://github.com/FabienPiette/string_to_number.git
$ cd string_to_number
$ bin/setup
# Run all tests
$ rake spec
# Run performance tests
$ ruby benchmark.rb
# Run specific test files
$ rspec spec/string_to_number_spec.rb
# Compare implementations
$ ruby performance_comparison.rb
# Detailed micro-benchmarks
$ ruby microbenchmark.rb
$ bin/console
# => Interactive prompt for experimentation
The gem uses a dual-architecture approach:
- Optimized Parser (
StringToNumber::Parser
): High-performance implementation with caching - Original Implementation (
StringToNumber::ToNumber
): Reference implementation for compatibility
Key performance optimizations:
- Pre-compiled regex patterns
- LRU caching with thread-safe access
- Memoized parser instances
- Zero-allocation number matching
Bug reports and pull requests are welcome on GitHub at https://github.com/FabienPiette/string_to_number.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Write tests for your changes
- Ensure all tests pass (
rake spec
) - Run performance tests to avoid regressions
- Commit your changes (
git commit -am 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is intended to be a safe, welcoming space for collaboration. Contributors are expected to adhere to the Contributor Covenant code of conduct.
- Ruby 2.5 or higher
- No external dependencies (uses only Ruby standard library)
Q: Numbers aren't parsing correctly
A: Ensure your input uses proper French number words. Use valid_french_number?
to validate input.
Q: Performance seems slow
A: Make sure you're using the default optimized parser. Check cache statistics with cache_stats
.
Q: Memory usage is high
A: Call clear_caches!
periodically if processing many unique number strings.
See CHANGELOG.md for version history and updates.
The gem is available as open source under the terms of the MIT License.
- Original implementation by Fabien Piette
- Performance optimizations and enhancements
- Community contributors and testers