Class: Persian::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/persian/counter.rb

Overview

Persian count class Basic counters for persian chars, texts, sentences and paragraphs

Class Method Summary collapse

Class Method Details

.character(text, char = nil) ⇒ Object

Return list a hash with list of characters in the text Hash key is the character and Hash value is number of occurrence



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/persian/counter.rb', line 10

def self.character(text, char = nil)
  list = text.split(//)
  occurrence = {}
  occurrence.default = 0

  list.each do |item|
    occurrence[item] += 1
  end

  if char.nil?
    occurrence
  else
    occurrence[char]
  end
end

.character_counter(text) ⇒ Object

Return how many character text is



33
34
35
# File 'lib/persian/counter.rb', line 33

def self.character_counter(text)
  text.length
end

.paragraph(text) ⇒ Object

Return number of paragraph in text



56
57
58
59
# File 'lib/persian/counter.rb', line 56

def self.paragraph(text)
  list = Persian::Tokenizer.split_paragraphs text
  list.length
end

.uniq_character(text) ⇒ Object

Return number of uniq characters used in text



27
28
29
30
# File 'lib/persian/counter.rb', line 27

def self.uniq_character(text)
  text = text.split(//)
  text.uniq.size
end

.word(text, keyword = nil) ⇒ Object

Return list a hash with list of words in the text Hash key is the word and Hash value is number of occurrence



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/persian/counter.rb', line 39

def self.word(text, keyword = nil)
  list = Persian::Tokenizer.tokenize(text)
  occurrence = {}
  occurrence.default = 0

  list.each do |item|
    occurrence[item] += 1
  end

  if keyword.nil?
    occurrence
  else
    occurrence[keyword]
  end
end