w2v-server: use qsort
diff --git a/w2v-server.pl b/w2v-server.pl
index cc17cf3..6d6f421 100755
--- a/w2v-server.pl
+++ b/w2v-server.pl
@@ -616,6 +616,18 @@
pthread_exit(nbs);
}
+int cmp_activation (const void * a, const void * b) {
+ float fb = ((collocator *)a)->activation;
+ float fa = ((collocator *)b)->activation;
+ return (fa > fb) - (fa < fb);
+}
+
+int cmp_probability (const void * a, const void * b) {
+ float fb = ((collocator *)a)->probability;
+ float fa = ((collocator *)b)->probability;
+ return (fa > fb) - (fa < fb);
+}
+
SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
HV *result = newHV();
@@ -830,17 +842,7 @@
}
}
}
- collocator *tmp = malloc(sizeof(collocator));
- for(i=0; i<found_index;i++) {
- for(j=0;j<found_index-1;j++) {
- if(best[j].probability < best[j+1].probability) {
- memmove(tmp, best + j, sizeof(collocator));
- memmove(best + j, best + j + 1, sizeof(collocator));
- memmove(best + j + 1, tmp, sizeof(collocator));
- }
- }
- }
- free(tmp);
+ qsort(best, found_index, sizeof(collocator), cmp_probability);
// for(i=0; i < found_index; i++) {
// printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
// }