forked from PolMine/RcppCWB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsugar.cpp
More file actions
178 lines (130 loc) · 4.5 KB
/
sugar.cpp
File metadata and controls
178 lines (130 loc) · 4.5 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "_cl.h"
#include <pcre.h>
}
#include <Rcpp.h>
/* short quasi-header */
Attribute* make_s_attribute(SEXP corpus, SEXP s_attribute, SEXP registry);
Attribute* make_p_attribute(SEXP corpus, SEXP p_attribute, SEXP registry);
int region_matrix_to_size(Rcpp::IntegerMatrix matrix){
int n;
int size = 0;
for (n = 0; n < matrix.nrow(); n++){
size += matrix(n,1) - matrix(n,0) + 1;
}
return size;
}
// [[Rcpp::export(name=".decode_s_attribute")]]
Rcpp::StringVector decode_s_attribute(SEXP corpus, SEXP s_attribute, SEXP registry) {
Attribute* att = make_s_attribute(corpus, s_attribute, registry);
int n;
int att_size = cl_max_struc(att);
Rcpp::StringVector result(att_size);
for (n = 0; n < att_size; n++) {
result[n] = cl_struc2str(att, n);
}
return result;
}
// [[Rcpp::export(name=".get_count_vector")]]
Rcpp::IntegerVector get_count_vector(SEXP corpus, SEXP p_attribute, SEXP registry){
Attribute *att = make_p_attribute(corpus, p_attribute, registry);
int n;
int max_id = cl_max_id(att);
Rcpp::IntegerVector count(max_id);
for (n = 0; n < max_id; n++){
count[n] = cl_id2freq(att, n);
}
return count;
}
// [[Rcpp::export(name=".get_region_matrix")]]
Rcpp::IntegerMatrix get_region_matrix(SEXP corpus, SEXP s_attribute, SEXP strucs, SEXP registry){
Attribute* att = make_s_attribute(corpus, s_attribute, registry);
std::vector<int> strucs_int = Rcpp::as<std::vector<int> >(strucs);
int strucs_length = strucs_int.size();
int n, start, end;
Rcpp::IntegerMatrix cpos_matrix(strucs_length,2);
for (n = 0; n < strucs_length; n++){
cl_struc2cpos(att, strucs_int[n], &start, &end);
cpos_matrix(n,0) = start;
cpos_matrix(n,1) = end;
}
return cpos_matrix;
}
// [[Rcpp::export(name=".get_cbow_matrix")]]
Rcpp::IntegerMatrix get_cbow_matrix(SEXP corpus, SEXP p_attribute, SEXP registry, SEXP matrix, SEXP window){
Attribute* att = make_p_attribute(corpus, p_attribute, registry);
int window_size = Rcpp::as<int>(window);
Rcpp::IntegerMatrix region_matrix(matrix);
int ncol_window_matrix = window_size * 2 + 1;
int nrow_window_matrix = region_matrix_to_size(region_matrix);
Rcpp::IntegerMatrix window_matrix(nrow_window_matrix, ncol_window_matrix);
std::fill(window_matrix.begin(), window_matrix.end(), -1);
int cpos, col, id, region_size, row_to_fill, n;
int row_base = 0;
int row = 0;
for (n = 0; n < region_matrix.nrow(); n++){
region_size = region_matrix(n,1) - region_matrix(n,0) + 1;
for (cpos = region_matrix(n,0); cpos <= region_matrix(n,1); cpos++){
id = cl_cpos2id(att, cpos);
for (col = 0; col < window_matrix.ncol(); col++){
row_to_fill = row - col + window_size;
if (row_to_fill >= row_base && row_to_fill < (row_base + region_size)){
window_matrix(row_to_fill,col) = id;
}
}
row++;
}
row_base = row;
}
return window_matrix;
}
// [[Rcpp::export(name=".region_matrix_to_ids")]]
Rcpp::IntegerVector region_matrix_to_ids(SEXP corpus, SEXP p_attribute, SEXP registry, SEXP matrix){
Attribute* att = make_p_attribute(corpus, p_attribute, registry);
Rcpp::IntegerMatrix region_matrix(matrix);
int size = region_matrix_to_size(region_matrix);
Rcpp::IntegerVector ids(size);
int n, cpos;
int i = 0;
for (n = 0; n < region_matrix.nrow(); n++){
for (cpos = region_matrix(n,0); cpos <= region_matrix(n,1); cpos++){
ids(i) = cl_cpos2id(att, cpos);
i++;
}
}
return ids;
}
// [[Rcpp::export(name=".ids_to_count_matrix")]]
Rcpp::IntegerMatrix ids_to_count_matrix(Rcpp::IntegerVector ids){
Rcpp::IntegerVector count(max(ids) + 1);
int n;
for (n = 0; n < ids.length(); n++){
count(ids[n])++;
}
int filled = 0;
for (n = 0; n < count.length(); n++){
if (count[n] > 0){
filled++;
}
}
Rcpp::IntegerMatrix count_matrix(filled,2);
int i = 0;
for (n = 0; n < count.length(); n++){
if (count[n] > 0){
count_matrix(i,0) = n;
count_matrix(i,1) = count[n];
i++;
}
}
return count_matrix;
}
// [[Rcpp::export(name=".region_matrix_to_count_matrix")]]
Rcpp::IntegerVector region_matrix_to_count_matrix(SEXP corpus, SEXP p_attribute, SEXP registry, SEXP matrix){
Rcpp::IntegerVector ids = region_matrix_to_ids(corpus, p_attribute, registry, matrix);
Rcpp::IntegerMatrix count_matrix = ids_to_count_matrix(ids);
return count_matrix;
}