Class: CrossLanguageSpotter::OracleLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/crosslanguagespotter/oracle.rb

Instance Method Summary collapse

Instance Method Details

#build_weka_classifier(srcpath, oraclepath) ⇒ Object



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
# File 'lib/crosslanguagespotter/oracle.rb', line 16

def build_weka_classifier(srcpath,oraclepath)        
    features_data = to_train_data(srcpath,oraclepath)
    data = []
    features_data.each do |rel,row|
        data.push(row)
    end
    keys = {    
        shared_length: :numeric,
        tfidf_shared: :numeric,
        itfidf_shared: :numeric,
        perc_shared_length_min: :numeric,
        perc_shared_length_max: :numeric,
        diff_min: :numeric,
        diff_max: :numeric,
        perc_diff_min: :numeric,
        perc_diff_max: :numeric,
        context: :numeric,
        jaccard: :numeric,
        jaro: :numeric,
        tversky: :numeric,
        result: :boolean
    }
    train_instances = hash2weka_instances("oracle",data,keys,:result)
    WekaClassifier.new(train_instances)
end

#to_train_data(srcpath, oraclepath) ⇒ Object

TODO Make it private



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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/crosslanguagespotter/oracle.rb', line 43

def to_train_data(srcpath,oraclepath)
    project = Project.new(srcpath)

    spotter = Spotter.new
    features = spotter.features_for_project(project)

    @file_lines = Hash.new do |h,k|
        h[k] = File.readlines(k)
    end

    ok_a = ok_b = ko_a = ko_b = 0
     
    train_data = {}
    File.open(oraclepath,'r').each_with_index do |input_line,l|  
        input_line.strip!
        unless input_line.start_with?('#')
            values = input_line.split ":"
            if values.count!=8
                raise "Line #{l+1}, error: #{input_line}. Values: #{values}"
            end
            # we order them to facilitate searching for duplicates
            end_a = OracleRelationEnd.new values[0], values[1].to_i, values[2].to_i, values[3]
            end_b = OracleRelationEnd.new values[4], values[5].to_i, values[6].to_i, values[7]
            if end_b.file < end_a.file
                end_a, end_b = end_b, end_a
            end

            file_a         = values[0]
            line_a         = values[1].to_i
            col_a          = values[2].to_i
            surface_form_a = values[3]
            file_b         = values[4]
            line_b         = values[5].to_i
            col_b          = values[6].to_i         
            surface_form_b = values[7]

            file_a = "#{srcpath}/#{file_a}"
            file_b = "#{srcpath}/#{file_b}"

            model_a = project.models[file_a]
            model_b = project.models[file_b]

            raise "Model not found for #{file_a}. Available: #{project.models.keys}" unless model_a                    
            raise "Model not found for #{file_b}. Available: #{project.models.keys}" unless model_b

            plain_col_a = convert_from_tabcolumn_to_plaincolumn(file_a,line_a,col_a)
            plain_col_b = convert_from_tabcolumn_to_plaincolumn(file_b,line_b,col_b)

            pos_a = SourcePosition.new(SourcePoint.new(line_a,plain_col_a),SourcePoint.new(line_a,plain_col_a+surface_form_a.length-1))
            pos_b = SourcePosition.new(SourcePoint.new(line_b,plain_col_b),SourcePoint.new(line_b,plain_col_b+surface_form_b.length-1))
            begin
                node_a = find_node(model_a,surface_form_a,pos_a)
                ok_a+=1
            rescue Exception => e
                ko_a+=1
                puts "Line #{l+1}) problem with '#{surface_form_a}', file: #{file_a}, pos #{pos_a}: #{e}"
            end
            begin
                node_b = find_node(model_b,surface_form_b,pos_b)
                ok_b+=1
            rescue Exception => e
                ko_b+=1
                puts "Line #{l+1}) problem with '#{surface_form_b}', file: #{file_b}, pos #{pos_b}: #{e}"
            end

            if node_a and node_b
                trindex_a = traverse_index(node_a)
                trindex_b = traverse_index(node_b)

                metaoracle_end_a = MetaOracleRelationEnd.new file_a,trindex_a
                metaoracle_end_b = MetaOracleRelationEnd.new file_b,trindex_b
                if metaoracle_end_b.file < metaoracle_end_a.file
                    metaoracle_end_a, metaoracle_end_b = metaoracle_end_b, metaoracle_end_a
                end
                #if metaoracle_values.values.include?([metaoracle_end_a,metaoracle_end_b])
                #    raise "Line #{l+1} (#{[metaoracle_end_a,metaoracle_end_b]}) is a duplicate of line #{metaoracle_values.find {|k,v| v==[metaoracle_end_a,metaoracle_end_b]}}"
                #else
                #    metaoracle_values[l+1] = [metaoracle_end_a,metaoracle_end_b]
                #end 

                id_a = NodeId.from_node(node_a)
                id_b = NodeId.from_node(node_b)
                rel = CrossLanguageRelation.new([id_a,id_b])
                f = features[rel]
                raise "Unknown features for #{rel} (a:#{node_a.source.artifact(:absolute).filename} L#{node_a.source.position(:absolute).begin_line},b:#{node_b.source.artifact(:absolute).filename} L#{node_b.source.position(:absolute).begin_line})" unless f
                entry = { result: true }
                f.each do |k,v|
                    entry[k] = v
                end
                train_data[rel] = entry
            end
        end        
    end

    # all the others are implicitly negative examples
    project.iter_over_shared_ids_instances do |node_a,node_b| 
        id_a = NodeId.from_node(node_a)
        id_b = NodeId.from_node(node_b)
        rel = CrossLanguageRelation.new([id_a,id_b])
        unless train_data.has_key?(rel)
            f = features[rel]
            entry = { result: false }
            f.each do |k,v|
                entry[k] = v
            end
            train_data[rel] = entry
        end
    end

    return train_data
end