3

I have an issue with collecting the combination of four digits (persons id) using pocketsphinx c program code. All of my other commands are detected and operated correctly. But i dont know how to create a conditional statement which will be selected when a string of possible four digit combination is detected. Kindly suggest how can i do it !!

printf("Processing...\n");
fflush(stdout);
/* Finish decoding, obtain and print result */
ps_end_utt(ps);
hyp = ps_get_hyp(ps, NULL, &uttid);
//printf("%s: %s\n", uttid, hyp); 
fflush(stdout);
if (hyp) {
    sscanf(hyp, "%s", word);

    if (strcmp(hyp, "LASCHE FAHRPLAN") == 0) {
        counter_correct = counter_correct + 1;
        printf("LASCHE FAHRPLAN is confirmed\n");
        printf("The correct number of utterances calculated %d, Total number of utterances calculated %s \n",counter_correct,uttid);
    }

    else if (strcmp(hyp, "LASCHE VORSCHAU") == 0) {
        counter_correct = counter_correct + 1;
        printf("LASCHE VORSCHAU is confirmed \n");
        printf("The correct number of utterances calculated %d, Total number of utterances calculated %s \n",counter_correct,uttid);
    }   

similarly i want to detect the strings like '1 2 3 4' , '3 7 8 9' (all possible combination of four digits) which are also generated at variable hyp [ printf("%s: %s\n", uttid, hyp); ] How should i make the conditional loop for this case.

Thank you for your help.

Barmar
  • 741,623
  • 53
  • 500
  • 612
bsnayak
  • 58
  • 7
  • Your code is very hard to follow, please try to clarify it. The numbers you're talking about are nowhere to be seen? – unwind Apr 22 '14 at 09:40
  • Not sure what you're asking, but maybe a regular expression will do it? – Barmar Apr 22 '14 at 09:45
  • As i am using the tool box from cmu sphinx - pocketsphinx , i have trained my acoustic model for set of 30 commands and possible combination of digits. so when i say those commands in microphone it gives an output printf("%s: %s\n", uttid, hyp); – bsnayak Apr 22 '14 at 09:45

4 Answers4

4

Use strtol(). Example of code:

#include <stdlib.h>

/* your code */

if (hyp) {
    sscanf(hyp, "%s", word);

    long int code = strtol(hyp, NULL, 10);

    if (code > 0 && code < 10000) {
        printf("Numeric code %ld\n", code);
        /* perform other work */
    }

    if (strcmp(hyp, "LASCHE FAHRPLAN") == 0) {
        counter_correct = counter_correct + 1;
        printf("LASCHE FAHRPLAN is confirmed\n");
        printf("The correct number of utterances calculated %d, Total number of utterances calculated %s \n",counter_correct,uttid);
    }
Roberto Reale
  • 4,247
  • 1
  • 17
  • 21
2

To check whether a string consists of 4 digits you can write a if statement as,

int a,b,c,d,id;
if(isdigit(hyp[0])&&isdigit(hyp[2])&&isdigit(hyp[4])&&isdigit(hyp[6]))
   {
       sscanf(hyp,"%d %d %d %d",&a,&b,&c,&d);
       id=a*1000+b*100+c*10+d;
   }

provided the string is NULL terminated and you can add even a statement to check the length of string as strlen(hyp)==7

LearningC
  • 3,182
  • 1
  • 12
  • 19
  • As i am using the tool box from cmu sphinx - pocketsphinx , i have trained my acoustic model for set of 30 commands and possible combination of digits. so when i say those commands in microphone it gives an output printf("%s: %s\n", uttid, hyp); of what i said. with this recognized strings hyp i want to do several operations as per requirement, but i am unable to find a way to think for detetcting possible combination of all four digits as a string. After they are matched i can print the corresponding id and his details later on. – bsnayak Apr 22 '14 at 09:56
  • @bsnayak then it means you want to convert the string to number and then check the value of that 4 digit number for comparison? so `strtol` should work right? it converts string to number then you can compare the number? – LearningC Apr 22 '14 at 10:02
  • @ LearningC Thank you very much for your suggestion ..I am trying to implement both of the ideas shared above.. will let you know soon – bsnayak Apr 22 '14 at 10:05
  • May i ask you as my output for numbers is in form "1 3 3 4" Should i consider the spacefactor? or the above code suggested takes them in to consideration and will be able to scan them correctly – bsnayak Apr 22 '14 at 10:12
  • @bsnayak ok. space will need to be considered. `strtol` will read first number so it will get only `1`. need to read it digit by digit. you are using even the `if` statement i suggested in my answer? – LearningC Apr 22 '14 at 10:17
  • @bsnayak i added code to test for id and then retrieve it from string. check it – LearningC Apr 22 '14 at 10:25
  • Dear All, I tried with the above two suggestions 1) else if(hyp) { long int code = strtol(hyp, NULL, 10); if (code > 0 && code < 10000) { printf("Drivers id is %ld\n", code); /* perform other work */ } } The following returns only the first number of the string what is detected.. How can i extend it to four digits or more? I was expecting if i said 1 2 3 4 and in my program, hyp is returning 1 2 3 4.. This else if would give me drivers id as 1234 2)else if((strlen(hyp)==5)&&isdigit(hyp[0])&&isdigit(hyp[2])&&isdigit(hyp[4])&&isdigit(hyp[6])){...} -this method isn't giving any output – bsnayak Apr 22 '14 at 14:07
  • 1
    @bsnayak the `if` statement i suggested makes id=1234, the number you wanted to extract from the string – LearningC Apr 23 '14 at 15:35
  • Yes .. I followed your suggestion .. Actually at first i made a small mistake. So it didn't come .. But now it is solved and worked fine. Thank you – bsnayak Apr 24 '14 at 15:09
2

Just create a predicate function that takes your string (and possibly additional data defining which strings to accept) and returns true iff the string meets the criteria.

In this predicate function you can use any control structures you like (like loops, switches, recursion, etc.) which are not directly available within an if() condition. And it gives a name to the (complex) comparison operation, which is very important for your readers (including your own future self).

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
  • else if(hyp) { long int code = strtol(hyp, NULL, 10); if (code > 0 && code < 10000) { printf("Drivers id is %ld\n", code); } } I think the space factor is an issue.. as when i say 1, 2, 3, 4 The decoder recognizes properly and hyp value is returning 1 2 3 4 but using this method only 1 is printed.. kindly suggest if there is any solution to print all the four numbers.. The other method using strlen(hyp) and isdigit(isdigit([0])&&.. is not giving any output Thank you – bsnayak Apr 22 '14 at 14:27
  • I'm not sure whether I understood your comment correctly, but if your problem is that `strtol()` converts only the first number in the string "1 2 3 4", then you need to take a second look at the man-page of `strtol()`: This function can return a pointer to the unparsed rest-string using the second argument. So, if you do `long a = strtol(hyp, &hyp, 10), b = strtol(hyp, &hyp, 10);` you will get `1` assigned to `a` and `2` assigned to `b`. – cmaster - reinstate monica Apr 22 '14 at 22:58
0
.....
else if(isdigit(hyp[0])&&isdigit(hyp[2])&&isdigit(hyp[4])&&isdigit(hyp[6]))
{
counter_correct = counter_correct + 1;
sscanf(hyp,"%ld %ld %ld %ld",&Nummer1,&Nummer2,&Nummer3,&Nummer4);
if(Nummer1==0){
id = Nummer1*1000+Nummer2*100+Nummer3*10+Nummer4;
printf("The drivers id detected %.4ld \n",id);
}        
else 
{
id = Nummer1*1000+Nummer2*100+Nummer3*10+Nummer4;
printf("The drivers id detected %ld \n",id);
}        
}
....

This solved the issue... Thank you very much for all your suggestions and help.. I find this very interesting to do..

bsnayak
  • 58
  • 7