parts 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/lib/parts/tester.rb +85 -0
- data/lib/parts/treebank3.2.txt +29933 -0
- data/lib/parts.rb +151 -0
- metadata +99 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Joe Root
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= parts
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to parts
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2012 Joe Root. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/lib/parts/tester.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
class Parts::Tester
|
2
|
+
|
3
|
+
attr_accessor :sentences
|
4
|
+
|
5
|
+
def initialize path="#{File.dirname(__FILE__)}/treebank3.2.txt"
|
6
|
+
# Sentences are stored as array's of word-tag pairs, where each sentence
|
7
|
+
# will be [{:word => w1, :tag => t1},...,{:word => wn, :tag => tn}].
|
8
|
+
@sentences = []
|
9
|
+
self.load path
|
10
|
+
end
|
11
|
+
|
12
|
+
def load path
|
13
|
+
# For each sentence we split on empty space, and then use regex to split
|
14
|
+
# each word/tag pair into its word and tag constituents. Whenever a full
|
15
|
+
# stop is encountered we create a new sentence.
|
16
|
+
File.open(path, "r") do |file|
|
17
|
+
sentence = []
|
18
|
+
while (line = file.gets)
|
19
|
+
line.split(' ').each do |part|
|
20
|
+
md = /(.+)+(\/){1}(.+)+/.match part
|
21
|
+
if md
|
22
|
+
if md[3] == "."
|
23
|
+
@sentences << sentence if not sentence.empty?
|
24
|
+
sentence = []
|
25
|
+
else
|
26
|
+
sentence << {:word => md[1].downcase, :tag => md[3]}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def create_tagger
|
35
|
+
Parts::Tagger.new @sentences
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_tagger k=10
|
39
|
+
# This method performs k-fold validation, with the default number being 10
|
40
|
+
# folds. We first shuffle our sentences to ensure that we do not always
|
41
|
+
# run exactly the same test, enabling us to further repeat our k-fold
|
42
|
+
# validation. We then create an offset value along which we make our
|
43
|
+
# folds.
|
44
|
+
sentences = @sentences.shuffle
|
45
|
+
total = sentences.length
|
46
|
+
offset = (total.to_f*k.to_f/100).floor
|
47
|
+
|
48
|
+
# For each fold, we divide our sentences up into test and training
|
49
|
+
# sentences, by rotating the list by our offset amount, then partitioning
|
50
|
+
# accordingly. We then initialise a tagger with our training set
|
51
|
+
# before passing in each of our test sentences for classification.
|
52
|
+
results = (0...k).map do |i|
|
53
|
+
print "Starting fold #{i+1}..."
|
54
|
+
sentences = sentences.rotate offset
|
55
|
+
test = sentences[0...offset]
|
56
|
+
train = sentences[offset...total]
|
57
|
+
|
58
|
+
c = Parts::Tagger.new train
|
59
|
+
|
60
|
+
# For each sentence in our array of test sentences, we calculate the
|
61
|
+
# accuracy with which its words were classified, before mapping these
|
62
|
+
# results to a new array, which we finally take the mean of.
|
63
|
+
percentage = test.map {|s| test_tagger_with_sentence c, s}
|
64
|
+
|
65
|
+
# Here we simply print out that we've completed our fold, along with the
|
66
|
+
# fold's accuracy. "%.2f" returns our accuracy percentage to 2.d.p.
|
67
|
+
puts "done"
|
68
|
+
puts "Fold #{i+1} accuracy: #{"%.2f" % (percentage.mean * 100)}%"
|
69
|
+
percentage.mean
|
70
|
+
end
|
71
|
+
|
72
|
+
# Here we take the mean of each fold and print it out.
|
73
|
+
puts "Avg. #{k}-fold accuracy: #{"%.2f" % (results.mean * 100)}%"
|
74
|
+
|
75
|
+
# Finally return the k-fold validation's mean accuracy.
|
76
|
+
return results.mean
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_tagger_with_sentence tagger, sentence
|
80
|
+
cs = tagger.classify sentence.map{|w| w[:word]}
|
81
|
+
correct = cs.zip(sentence).select{|ws| ws[0][:tag] == ws[1][:tag]}.length
|
82
|
+
correct.to_f / sentence.length
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|