GithubHelp home page GithubHelp logo

tuzz / frequency_analyser Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 17 KB

Compose a hash containing the frequencies of characters in text.

Home Page: https://rubygems.org/gems/frequency_analyser

Ruby 100.00%

frequency_analyser's Introduction

Frequency Analyser

Compose a hash containing the frequencies of characters in text.

Usage

gem install frequency_analyser
require 'frequency_analyser'

file = File.new('a_tale_of_two_cities.txt')
FrequencyAnalyser.analyse(file)
#=> {
  'a'=>47063, 'b'=>8139,  'c'=>13224, 'd'=>27484, 'e'=>72877, 'f'=>13152,
  'g'=>12121, 'h'=>38358, 'i'=>39784, 'j'=>622,   'k'=>4634,  'l'=>21533,
  'm'=>14922, 'n'=>41310, 'o'=>45115, 'p'=>9453,  'q'=>655,   'r'=>35956,
  's'=>36771, 't'=>52393, 'u'=>16216, 'v'=>5065,  'w'=>13835, 'x'=>666,
  'y'=>11849, 'z'=>213
}

You can analyse strings too:

FrequencyAnalyser.analyse('Hello, world!')
#=> { 'd'=>1, 'e'=>1, 'h'=>1, 'l'=>3, 'o'=>2, 'r'=>1, 'w'=>1 }

And collections of things:

FrequencyAnalyser.analyse('foo', File.new('bar'), StringIO.new('baz'), ['q', 'u', 'x'])
#=> { 'a'=>2, 'b'=>2, 'f'=>1, 'o'=>2, 'q'=>1, 'r'=>1, 'u'=>1, 'x'=>1, 'z'=>1 }

Probabilities and Percentages

If you'd like to calculate the frequency probabilities instead, you can pass in an optional symbol:

FrequencyAnalyser.analyse('Hello, world!', :probability)
#=> { 'd'=>0.1, 'e'=>0.1, 'h'=>0.1, 'l'=>0.3, 'o'=>0.2, 'r'=>0.1, 'w'=>0.1 }

The same goes for percentages:

FrequencyAnalyser.analyse('Hello, world!', :percentage)
#=> { 'd'=>10, 'e'=>10, 'h'=>10, 'l'=>30, 'o'=>20, 'r'=>10, 'w'=>10 }

Counting other things

By default, Frequency Analyser counts alphabetic characters. You can change this by instantiating your own support classes:

counter    = FrequencyAnalyser::Counter.new(%w(1 3 5 !))
aggregator = FrequencyAnalyser::Aggregator.new(counter)
analyser   = FrequencyAnalyser.new(aggregator)

analyser.analyse('!12321!')
#=> { '!'=>2, '1'=>2, '3'=>1 }

Most of the gem is architected in this way, so it should be straightforward to add new modes, for example.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.