-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordfreq.rb
executable file
·64 lines (56 loc) · 1.54 KB
/
wordfreq.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/ruby
# coding: utf-8
#
# == NAME
# wordfreq.rb
#
# == USAGE
# Call this from generator.rb.
#
# == DESCRIPTION
# A library that gives information about word frequency.
# A properly formatted distribution.txt file must be in place.
#
# == AUTHOR
# Douglas Perkins - https://dperkins.org
# Word frequency list. Words that are too short or too long are excluded.
class Wordfreq
# The maximum kanji count for a sample word.
Max_example_word_width = 8
# Creates a Wordfreq.
def initialize
puts 'Parsing distribution.txt ...'
path = Script_dir + '/dictionaries/distribution.txt'
wordfreq = IO.readlines path
wordfreq.keep_if {|line| line =~ /^\d+\t.*/}
wordfreq.keep_if {|line| line.include? "\t"}
wordfreq.delete_if {|line|
word = line.split[4].split('|')[0]
length = word.scan(/./u).length
length < 2 or length > Max_example_word_width
}
puts 'Usable words in distribution.txt: ' + wordfreq.size.to_s + '.'
@lookup_table = {}
wordfreq.each do |line|
word = line.split[4].split('|')[0]
frequency = line.split[1]
pair = [word, frequency]
word.scan(/./u).each do |char|
if not @lookup_table.key? char
@lookup_table[char] = [pair]
else
@lookup_table[char] << pair
end
end
end
end
# Returns a list of lines in Wordfreq that contain the kanji.
# Words in this list are sorted most to least common.
def lookup kanji
return @lookup_table[kanji]
end
# Returns true if the kanji is in this word list, and false otherwise.
def include? kanji
return @lookup_table.key? kanji
end
end