免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3240 | 回复: 9
打印 上一主题 下一主题

一道难题... [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-31 19:45 |只看该作者 |倒序浏览
10可用积分
要你导入一个TXT文件
以下的格式:
hi! ~ hello!
my name is <n1> ~ hi <n1>
意思是如果输入hi! 电脑就回答hello!
如果输入my name is james回答就变成hi james
不过那个TXT文件是可以用以上的格式改的..
我做的脑子疼死了..
望大家可以帮下忙!!

论坛徽章:
0
2 [报告]
发表于 2008-08-31 19:49 |只看该作者
这是那个的规则

rules.rar

328 Bytes, 下载次数: 40

论坛徽章:
0
3 [报告]
发表于 2008-08-31 22:44 |只看该作者
先解析
在比较
<>内容填充
生成输出
应该是这个思路
等我慢慢写

论坛徽章:
0
4 [报告]
发表于 2008-08-31 23:15 |只看该作者

回复 #1 .js. 的帖子

def sepintext(intext):
    intext+=' '
    words=[]
    tempword=''
    for char in intext:
        if char!=' ':
            tempword+=char
        else :
            words.append(tempword)
            tempword=''
    return words

def getfirt(sepedtext):
    firstpart=[]
    for temp in sepedtext:
        if temp!='~':
            firstpart.append(temp)
        else:
            break
    return firstpart

def getlast(sepedtext):
    lastpart=[]
    sepedtext.reverse()
    for temp in sepedtext:
        if temp!='~':
            lastpart.append(temp)
        else:
            break
    lastpart.reverse()
    return lastpart
def cmptext(firstpart,sepedintext):
    if(len(firstpart)==len(sepedintext)):
        for i in range (0,len(firstpart)):
            if '<'not in firstpart[i]:
                if firstpart[i]==sepedintext[i]:
                    return True
                else:
                    print firstpart[i],sepedintext[i]
    return False
def getfix(firstpart,sepedintext):
    fixtext={}
    for i in range (0,len(firstpart)):
        if '<'in firstpart[i]:
            fixtext[firstpart[i]]=sepedintext[i]
    return fixtext
def getoutput(fixtext,lastpart):
    output=''
    for temp in lastpart:
        if'<'in temp:
            output+=fixtext[temp]+' '
        else:
            output+=temp+' '
    return output




fp = open(r'e:\rules.txt')
intext=''
while intext!='exit':
    intext=raw_input()
    for text in fp:
        sepedtext=sepintext(text)
        firstpart=getfirt(sepedtext)
        lastpart=getlast(sepedtext)
        sepedintext=sepintext(intext)
        fixed=cmptext(firstpart,sepedintext)
        if fixed:
            fixtext=getfix(firstpart,sepedintext)
        print getoutput(fixtext,lastpart)

改下open 的文件就可以了

[ 本帖最后由 lich_wang 于 2008-8-31 23:18 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-09-01 03:28 |只看该作者
又是一留学垃圾上来骗作业了。

论坛徽章:
0
6 [报告]
发表于 2008-09-01 09:14 |只看该作者
有问题直接提,别把整个题目都放上来

pass

论坛徽章:
0
7 [报告]
发表于 2008-09-01 12:25 |只看该作者
楼上的,看在他分的面子上,让让他吧.发个贴.也不容易.

论坛徽章:
0
8 [报告]
发表于 2008-09-01 16:02 |只看该作者
这个题目挺有意思的呀,像个聊天机器人.
我写的(很粗糙):



  1. #coding:utf-8
  2. import fileinput, re,string

  3. regobj = re.compile(r'<\w+>')

  4. def replace(s,a):
  5.         inter=regobj.finditer(s)
  6.         if len(re.findall(r'<\w+>',s))>1:
  7.                 i=0
  8.                 for match in inter:
  9.                         s=string.replace(s,s[match.span()[0]:match.span()[1]],a[0][i])
  10.                         i=i+1
  11.         else:
  12.                 for match in inter:
  13.                         s=string.replace(s,s[match.span()[0]:match.span()[1]],a[0])
  14.         return s


  15. file=open('rules.txt','r')
  16. answer={}

  17. for i in file:
  18.         answer[i.split('~')[0]]=i.split('~')[1]

  19. for i in answer.keys():
  20.         answer[i]=[re.sub(regobj,r'(\w+)',i),answer[i]]

  21. file.close()

  22. while 1:
  23.         inputstr=raw_input("You say:")
  24.         for i in answer.keys():
  25.                 reg=answer[i][0]
  26.                 match = re.findall(reg, inputstr)
  27.                 if match:
  28.                         print replace(answer[i][1],match)
  29.                        
复制代码



运行:
You say:my name is two
You've already told me your name is two!

Hi there two!

You say:i feel happy
How often do you feel that way?

You say:you love me
What makes you think I love you?

You say:you hat me
What makes you think I hat you?

You say:you love sincerely me
What makes you think I love sincerely you?

论坛徽章:
0
9 [报告]
发表于 2008-09-02 09:13 |只看该作者
看看

[ 本帖最后由 Vinge 于 2008-9-2 09:14 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2008-09-02 13:28 |只看该作者
自己搞个Parser?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP