PocketSphinx 5prealpha
kws_detections.c
1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/* ====================================================================
3 * Copyright (c) 2014 Carnegie Mellon University. All rights
4 * reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 *
19 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * ====================================================================
32 *
33 */
34
35/*
36* kws_detections.c -- Object for storing keyphrase search results
37*/
38
39#include "kws_detections.h"
40
41void
42kws_detections_reset(kws_detections_t *detections)
43{
44 gnode_t *gn;
45
46 if (!detections->detect_list)
47 return;
48
49 for (gn = detections->detect_list; gn; gn = gnode_next(gn))
50 ckd_free(gnode_ptr(gn));
51 glist_free(detections->detect_list);
52 detections->detect_list = NULL;
53}
54
55void
56kws_detections_add(kws_detections_t *detections, const char* keyphrase, int sf, int ef, int prob, int ascr)
57{
58 gnode_t *gn;
59 kws_detection_t* detection;
60 for (gn = detections->detect_list; gn; gn = gnode_next(gn)) {
61 kws_detection_t *det = (kws_detection_t *)gnode_ptr(gn);
62 if (strcmp(keyphrase, det->keyphrase) == 0 && det->sf < ef && det->ef > sf) {
63 if (det->prob < prob) {
64 det->sf = sf;
65 det->ef = ef;
66 det->prob = prob;
67 det->ascr = ascr;
68 }
69 return;
70 }
71 }
72
73 /* Nothing found */
74 detection = (kws_detection_t *)ckd_calloc(1, sizeof(*detection));
75 detection->sf = sf;
76 detection->ef = ef;
77 detection->keyphrase = keyphrase;
78 detection->prob = prob;
79 detection->ascr = ascr;
80 detections->detect_list = glist_add_ptr(detections->detect_list, detection);
81}
82
83char *
84kws_detections_hyp_str(kws_detections_t *detections, int frame, int delay)
85{
86 gnode_t *gn;
87 char *c;
88 int len;
89 char *hyp_str;
90
91 len = 0;
92 for (gn = detections->detect_list; gn; gn = gnode_next(gn)) {
93 kws_detection_t *det = (kws_detection_t *)gnode_ptr(gn);
94 if (det->ef < frame - delay) {
95 len += strlen(det->keyphrase) + 1;
96 }
97 }
98
99 if (len == 0) {
100 return NULL;
101 }
102
103 hyp_str = (char *)ckd_calloc(len, sizeof(char));
104 c = hyp_str;
105 for (gn = detections->detect_list; gn; gn = gnode_next(gn)) {
106 kws_detection_t *det = (kws_detection_t *)gnode_ptr(gn);
107 if (det->ef < frame - delay) {
108 memcpy(c, det->keyphrase, strlen(det->keyphrase));
109 c += strlen(det->keyphrase);
110 *c = ' ';
111 c++;
112 }
113 }
114 if (c > hyp_str) {
115 c--;
116 *c = '\0';
117 }
118 return hyp_str;
119}
120