Class: Precise::TypesList

Inherits:
Object
  • Object
show all
Defined in:
lib/precise/types_list.rb

Constant Summary collapse

@@types =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypesList

Returns a new instance of TypesList.



9
10
11
12
13
14
15
# File 'lib/precise/types_list.rb', line 9

def initialize
  resdir = File.join __dir__,'..','..','res'
  FileUtils.mkdir_p resdir
  typesfile = File.absolute_path(File.join resdir,'types.lst')
  download(typesfile) unless File.exist? typesfile
  @types ||= File.readlines typesfile, chomp: true
end

Class Method Details

.percentage_of_tokens_present(string) ⇒ Object



33
34
35
# File 'lib/precise/types_list.rb', line 33

def self.percentage_of_tokens_present(string)
  new.percentage_of_tokens_present(string)
end

Instance Method Details

#download(path) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/precise/types_list.rb', line 17

def download(path)
  puts 'downloading types database (only needed once)...'
  require 'net/http'
  require 'open-uri'
			require 'progressbar'
  url = 'https://raw.githubusercontent.com/sixtyfive/arabic-types/main/types.lst'
  data = URI.open(url)
  IO.copy_stream data, path
end

#percentage_of_tokens_present(string) ⇒ Object



27
28
29
30
31
# File 'lib/precise/types_list.rb', line 27

def percentage_of_tokens_present(string)
  words = string.split
  n_present = words.map{|w| @types.include? w}.count(true)
  100.0 / words.length * n_present
end