-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathphntranscheck.cpp
128 lines (101 loc) · 2.63 KB
/
phntranscheck.cpp
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
/**************************************************************************
* copyright : (C) 2004-2006 by Petr Schwarz & Pavel Matejka *
* UPGM,FIT,VUT,Brno *
* email : {schwarzp,matejkap}@fit.vutbr.cz *
**************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
**************************************************************************/
#include <stdio.h>
#include <string.h>
#include "phntranscheck.h"
PhnTransChecker::PhnTransChecker() :
strtok_buff(0)
{
}
PhnTransChecker::~PhnTransChecker()
{
}
char *PhnTransChecker::StrTok(char *str, char *delims)
{
if(str)
strtok_buff = str;
// skip delims front of token
while(*strtok_buff != '\0' && strchr(delims, *strtok_buff))
strtok_buff++;
if(*strtok_buff == '\0')
return 0;
char *ret = strtok_buff;
// skip token
while(*strtok_buff != '\0' && !strchr(delims, *strtok_buff))
strtok_buff++;
// terminate token and prepare next one
if(*strtok_buff != '\0')
{
*strtok_buff = '\0';
strtok_buff++;
// skip delims front of token
while(*strtok_buff != '\0' && strchr(delims, *strtok_buff))
strtok_buff++;
}
return ret;
}
int PhnTransChecker::LoadPhnList(char *file)
{
FILE *fp;
fp = fopen(file, "r");
if(!fp)
return PTCE_INPUTFILE;
phnList.clear();
char phoneme[256];
while(fscanf(fp, "%255s", phoneme) == 1)
phnList.insert(phoneme);
fclose(fp);
return PTCE_OK;
}
bool PhnTransChecker::Check(char *transc, char *unknownPhns)
{
char tr[1024];
strcpy(tr, transc);
char *tok = StrTok(tr, " \t");
if(unknownPhns)
strcpy(unknownPhns, "");
if(!tok)
return false;
bool ok = true;
while(tok)
{
std::set<std::string>::iterator it = phnList.find(tok);
if(it == phnList.end())
{
ok = false;
if(unknownPhns)
{
if(strlen(unknownPhns) != 0)
strcat(unknownPhns, ", ");
strcat(unknownPhns, tok);
}
}
tok = StrTok(0, " \t");
}
if(!ok)
return false;
return true;
}
int PhnTransChecker::GetTranscLen(char *transc)
{
char tr[1024];
strcpy(tr, transc);
char *tok = StrTok(tr, " \t");
int n = 0;
while(tok)
{
n++;
tok = StrTok(0, " \t");
}
return n;
}