zoukankan      html  css  js  c++  java
  • 估值为一亿的AI核心代码

    本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:
    无论用户说什么,首先把对方说的话在一行中原样打印出来;
    消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
    把原文中所有大写英文字母变成小写,除了 I;
    把原文中所有独立的 can you、could you 对应地换成 I can、I could—— 这里“独立”是指被空格或标点符号分隔开的单词;
    把原文中所有独立的 I 和 me 换成 you;
    把原文中所有的问号 ? 换成惊叹号 !;
    在一行中输出替换后的句子作为 AI 的回答。
     
    输入格式:输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。
    输出格式:按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。
    输入样例:
    6
    Hello ?
    Good to chat   with you
    can   you speak Chinese?
    Really?
    Could you show me 5
    What Is this prime? I,don 't know
     
    输出样例:
    Hello ?
    AI: hello!
    Good to chat   with you
    AI: good to chat with you
    can   you speak Chinese?
    AI: I can speak chinese!
    Really?
    AI: really!
    Could you show me 5
    AI: I could show you 5
    What Is this prime? I,don 't know
    AI: what Is this prime! you,don't know
     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 char b[1005];
     5 int judge(int x)
     6 {
     7     return !((b[x]>='A' && b[x]<='Z') || (b[x]>='a' && b[x]<='z') || (b[x]>='0' && b[x]<='9'));
     8 }
     9 
    10 
    11 int main(void)
    12 {
    13     int i,j;
    14     int n,c;
    15     char a[1005];
    16     
    17     scanf("%d",&n);
    18     getchar();
    19     
    20     while(n--)
    21     {
    22         gets(a);
    23         puts(a);
    24         printf("AI: ");
    25         c = i = 0;
    26         
    27         for(i=0; a[i]; i++)
    28         {
    29             if(a[i] == ' ')   //把所有空格窦归为一个 
    30             {
    31                 while(a[i] == ' ')
    32                 {
    33                     i++;
    34                 }
    35                 b[c++] = ' ';
    36                 i--;
    37             }
    38             else if(a[i] == '?')
    39             {
    40                 b[c++] = '!';
    41             }
    42             else if(a[i]>='A' && a[i]<='Z' && a[i]!='I')
    43             {
    44                 b[c++] = a[i]+32;
    45             }
    46             else
    47             {
    48                 b[c++] = a[i];
    49             }
    50         }
    51         
    52         for(i=0; i<c; i++)   //strstr返回的是地址! 
    53         {
    54             if(i==0 && b[i]==' ') continue;
    55             if((i==0 || judge(i-1)) && strstr(b+i,"could you")==&b[i] && (i+9==c || judge(i+9))) //i使用来检查是否在首尾独立 
    56             {                                                                                    //judge检查是否在句中独立 
    57                 printf("I could");
    58                 i+=8;
    59             }
    60             else if((i==0 || judge(i-1)) && strstr(b+i,"can you")==&b[i] && (i+7==c || judge(i+7)))
    61             {
    62                 printf("I can");
    63                 i+=6; 
    64             } 
    65             else if((i==0 || judge(i-1)) && b[i]=='m' && b[i+1]=='e' && (i+2==c || judge(i+2)))
    66             {
    67                 printf("you");
    68                 i++;
    69             }
    70             else if((i==0 || judge(i-1)) && b[i]=='I' && (i+1==c || judge(i+1)))
    71             {
    72                 printf("you");
    73             }
    74             else if(b[i]==' ')
    75             {
    76                 if(i==c-1)  continue;  //结尾空格 
    77                 if(!judge(i+1))//如果空格后一个是数字或者字母 直接过滤掉标点 
    78                 {
    79                     printf(" ");
    80                 }
    81             }
    82             else 
    83             {
    84                 printf("%c",b[i]);
    85             }
    86         }
    87         printf("\n");
    88     }
    89     
    90     return 0;
    91 }
  • 相关阅读:
    Android Studio轻松上手指南(1)
    在MyEclipse下创建Java Web项目 入门(图文并茂)经典教程
    opencv提取截获图像(总结摘来)
    10.正则表达式匹配
    遍历 Map 的方式
    09.回文数
    08.字符串转换位整数
    07.整数反转
    06. Z字型变换
    05. 求最长回文子串
  • 原文地址:https://www.cnblogs.com/ZhengLijie/p/12491443.html
Copyright © 2011-2022 走看看