【问题标题】:making a conditional statement with a string containing all possible combination of four digits in 'C'使用包含“C”中所有可能的四位数字组合的字符串创建条件语句
【发布时间】:2014-06-06 14:31:30
【问题描述】:

我在使用 pocketsphinx c 程序代码收集四位数字(个人 ID)的组合时遇到问题。我的所有其他命令都被检测到并正确运行。但我不知道如何创建一个条件语句,当检测到一串可能的四位数字组合时将选择该条件语句。 请建议我该怎么做!

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);
    }   

同样,我想检测像 '1 2 3 4' 、 '3 7 8 9' 这样的字符串(所有可能的四位数组合) 它们也在变量 hyp [ printf("%s: %s\n", uttid, hyp); ] 我应该如何为这种情况制作条件循环。

感谢您的帮助。

【问题讨论】:

  • 您的代码很难理解,请尝试澄清它。你说的数字不见了?
  • 不确定你在问什么,但也许正则表达式可以做到这一点?
  • 当我使用 cmu sphinx - pocketsphinx 的工具箱时,我已经针对 30 个命令集和可能的数字组合训练了我的声学模型。因此,当我在麦克风中说出这些命令时,它会输出 printf("%s: %s\n", uttid, hyp);

标签: c if-statement speech-recognition conditional-statements cmusphinx


【解决方案1】:

要检查字符串是否由 4 位数字组成,您可以编写 if 语句为,

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;
   }

如果字符串以 NULL 结尾,您甚至可以添加一条语句来检查字符串的长度为 strlen(hyp)==7

【讨论】:

  • 当我使用 cmu sphinx - pocketsphinx 的工具箱时,我已经针对 30 个命令集和可能的数字组合训练了我的声学模型。因此,当我在麦克风中说出这些命令时,它会输出 printf("%s: %s\n", uttid, hyp);我所说的。有了这个公认的字符串 hyp 我想根据要求做几个操作,但我无法找到一种方法来思考将所有四个数字的可能组合作为一个字符串。匹配后,我可以稍后打印相应的 id 和他的详细信息。
  • @bsnayak 那么这意味着您要将字符串转换为数字,然后检查该 4 位数字的值以进行比较?所以strtol 应该可以正常工作吗?它将字符串转换为数字,然后您可以比较数字?
  • @LearningC 非常感谢您的建议..我正在尝试实现上面分享的两个想法..很快就会通知您
  • 请问您,因为我的数字输出格式为“1 3 3 4”,我应该考虑空间因子吗?或者上面建议的代码将它们考虑在内并且能够正确扫描它们
  • @bsnayak 我建议的if 语句使 id=1234,你想从字符串中提取的数字
【解决方案2】:

使用strtol()。代码示例:

#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);
    }

【讨论】:

    【解决方案3】:

    只需创建一个谓词函数,该函数接受您的字符串(可能还有定义要接受哪些字符串的附加数据)并在字符串满足条件时返回 true。

    在此谓词函数中,您可以使用您喜欢的任何控制结构(如循环、开关、递归 等),这些结构在if() 条件中不直接可用。它为(复杂的)比较操作命名,这对您的读者(包括您未来的自己)非常很重要。

    【讨论】:

    • else if(hyp) { long int code = strtol(hyp, NULL, 10); if (code > 0 && code
    • 我不确定我是否正确理解了您的评论,但如果您的问题是 strtol() 仅转换字符串“1 2 3 4”中的第一个数字,那么您需要采取第二次查看strtol() 的手册页:此函数可以使用第二个参数返回指向未解析的剩余字符串的指针。因此,如果您执行long a = strtol(hyp, &amp;hyp, 10), b = strtol(hyp, &amp;hyp, 10);,您将获得1 分配给a2 分配给b
    【解决方案4】:
    .....
    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);
    }        
    }
    ....
    

    这解决了问题... 非常感谢您的所有建议和帮助.. 我觉得这很有趣..

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 2012-02-12
      • 1970-01-01
      • 2015-12-30
      • 2017-11-11
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 2022-11-03
      相关资源
      最近更新 更多