/
styletool.rb
131 lines (122 loc) · 3.74 KB
/
styletool.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'tk'
class Document
attr_reader :name, :wordCount, :countedWords
def initialize(name,text)
@name = name
@countedWords = Hash.new(0)
words = text.downcase.scan(/\w+/)
if block_given?
words.each{|word| @countedWords[word] += 1 if yield(word)}
else
words.each{|word| @countedWords[word] += 1}
end
@wordCount = words.size
end
def words
@countedWords.keys
end
def count(word)
#will return 0 if 'word' not in 'countedWords' since 0 is the default value
@countedWords[word]
end
def relativeFrequency(word)
@countedWords[word] / @wordCount.to_f
end
end
class Interface
def initialize
@documents = Array.new
@masterWordList = Array.new
#and now for the GUI
@root = TkRoot.new() {title 'Style Tool'}
TkLabel.new{
@root
text "Loaded files:"
}.grid('column'=>0,'row'=>0, 'sticky'=>'w')
yscroll = proc{|*args| @lbscroll.set(*args)}
scroll = proc{|*args| @listbox.yview(*args)}
@listbox = TkListbox.new(@root) {
selectmode "none"
height 5
yscrollcommand yscroll
}.grid('column'=>1, 'row'=>0,'sticky'=>'w', 'padx'=>5, 'pady'=>5)
@lbscroll = TkScrollbar.new(@root) {
orient 'vertical'
command scroll
}.grid('column'=>2, 'row'=>0,'sticky'=>'ns')
addfile = proc {
filename = Tk.getOpenFile
#if the user clicks "cancel" in the dialog box then filename == ""
self.addFile(filename) unless filename == ""
}
addfolder = proc {
foldername = Tk.chooseDirectory
self.addFolder(foldername) unless foldername == ""
}
save = proc {
filename = Tk.getSaveFile("filetypes"=>[["CSV", ".csv"]])
self.saveToCSV(filename) unless filename == ""
}
TkButton.new(@root) {
text 'Add file'
command addfile
}.grid('column'=>0, 'row'=>1,'sticky'=>'w', 'padx'=>5, 'pady'=>5)
TkButton.new(@root) {
text 'Add folder'
command addfolder
}.grid('column'=>1, 'row'=>1,'sticky'=>'w', 'padx'=>5, 'pady'=>5)
TkButton.new(@root) {
text 'Save as CSV'
command save
}.grid('column'=>2, 'row'=>1,'sticky'=>'w', 'padx'=>5, 'pady'=>5)
wordlisttoggled = proc {
if @wordListSpecified.get_value == "1"
filename = Tk.getOpenFile
unless filename == ""
@masterWordList = IO.read(filename).downcase.scan(/\w+/).uniq
@documents = Array.new
@listbox.delete(0) while @listbox.size != 0
else #the user hit 'cancel' -- don't change anything!
@wordListSpecified.set_value("0")
end
else
@documents = Array.new
@masterWordList = Array.new
@listbox.delete(0) while @listbox.size != 0
end
}
@wordListSpecified = TkCheckButton.new(@root){
text "Count specific words only"
command wordlisttoggled
}.grid('column'=>0,'row'=> 2, 'sticky'=>'w')
end
def addFile(filename)
@listbox.insert('end', filename)
if @wordListSpecified
newdoc = Document.new(filename,IO.read(filename)) {|word| @masterWordList.include?(word)}
else
newdoc = Document.new(filename,IO.read(filename))
@masterWordList = (@masterWordList | newdoc.words).sort
end
@documents.push(newdoc)
end
def addFolder(path)
Dir.chdir(path){Dir.foreach(path){|file| self.addFile(file) if File.file?(file)}}
end
def saveToCSV(filename)
File.open(filename, "w") do |file|
#prints the file name at the top of each column
@documents.each{|doc| file.print(",",doc.name)}
file.print("\n")
@masterWordList.each do |word|
file.print(word)
@documents.each{|doc| file.print(",",doc.relativeFrequency(word))}
file.print("\n")
end
end
end
end
if __FILE__ == $0
Interface.new
Tk.mainloop()
end