forked from ivanhk/fastText_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
157 lines (136 loc) · 3.85 KB
/
Copy pathUtils.java
File metadata and controls
157 lines (136 loc) · 3.85 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
package fasttext;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;
import org.apache.commons.math3.random.RandomGenerator;
public class Utils {
static final int SIGMOID_TABLE_SIZE = 512;
static final int MAX_SIGMOID = 8;
static final int LOG_TABLE_SIZE = 512;
static float[] t_sigmoid;
static float[] t_log;
static {
initTables();
}
static void initTables() {
initSigmoid();
initLog();
}
static void initSigmoid() {
t_sigmoid = new float[SIGMOID_TABLE_SIZE + 1];
for (int i = 0; i < SIGMOID_TABLE_SIZE + 1; i++) {
float x = (float) (i * 2 * MAX_SIGMOID) / SIGMOID_TABLE_SIZE - MAX_SIGMOID;
t_sigmoid[i] = (float) (1.0 / (1.0 + Math.exp(-x)));
}
}
static void initLog() {
t_log = new float[LOG_TABLE_SIZE + 1];
for (int i = 0; i < LOG_TABLE_SIZE + 1; i++) {
float x = (float) (((float) (i) + 1e-5) / LOG_TABLE_SIZE);
t_log[i] = (float) Math.log(x);
}
}
public static float log(float x) {
if (x > 1.0) {
return 0.0f;
}
int i = (int) (x * LOG_TABLE_SIZE);
return t_log[i];
}
public static float sigmoid(float x) {
if (x < -MAX_SIGMOID) {
return 0.0f;
} else if (x > MAX_SIGMOID) {
return 1.0f;
} else {
int i = (int) ((x + MAX_SIGMOID) * SIGMOID_TABLE_SIZE / MAX_SIGMOID / 2);
return t_sigmoid[i];
}
}
public static boolean isEmpty(String str) {
return (str == null || str.isEmpty());
}
public static long sizeLine(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
long count = 0;
int readChars = 0;
boolean endsWithoutNewLine = false;
while ((readChars = is.read(c)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n')
++count;
}
endsWithoutNewLine = (c[readChars - 1] != '\n');
}
if (endsWithoutNewLine) {
++count;
}
return count;
} finally {
is.close();
}
}
/**
*
* @param br
* @param pos line numbers start from 1
* @throws IOException
*/
public static void seek(BufferedReader br, long pos) throws IOException {
// br.reset();
String line;
int currentLine = 1;
while (currentLine < pos && (line = br.readLine()) != null) {
if (Utils.isEmpty(line) || line.startsWith("#")) {
continue;
}
currentLine++;
}
}
private static final int SHUFFLE_THRESHOLD = 5;
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void shuffle(List<?> list, RandomGenerator rnd) {
int size = list.size();
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
for (int i = size; i > 1; i--)
swap(list, i - 1, rnd.nextInt(i));
} else {
Object arr[] = list.toArray();
// Shuffle array
for (int i = size; i > 1; i--)
swap(arr, i - 1, rnd.nextInt(i));
// Dump array back into list
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
ListIterator it = list.listIterator();
for (int i = 0; i < arr.length; i++) {
it.next();
it.set(arr[i]);
}
}
}
/**
* Swaps the two specified elements in the specified array.
*/
private static void swap(Object[] arr, int i, int j) {
Object tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void swap(List<?> list, int i, int j) {
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
final List l = list;
l.set(i, l.set(j, l.get(i)));
}
}