Python中的操作列表

2024-10-03 04:33:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python(2.x)中有一个小的标记脚本。 我试着用字典标记语料库的每一行。 剧本的表现一般都很好,但我希望有点不同 结果。你知道吗

代码是这样的

def tag_corpus():
    corpus1=open("Corpus1.txt","r")
    dict1=open("Dictnew1.txt","r")
    dictw=dict1.read().lower().split()
    list1=[]
    for line in corpus1:
        linew=line.lower().split()
        for word in linew:
            if word in dictw:
                word_i=dictw.index(word)
                word_i1=word_i+1
                tag=dictw[word_i1]
                str1=word+"/"+tag
                list1.append(str1)
            else:
                str2=word+"/"+"NA"
                list1.append(str2)
    str3=" ".join(list1)
    print str3

其中“Corpus1.txt”的内容是

  London is situtated over Thames . 
  London is a village near Burgundy . 
  London is situated near Ontario .

“Dictnew1.txt”的意思是

伦敦LOC 泰晤士河LOC 勃艮第地区 安大略省LOC

结果是

london/loc is/NA situtated/NA over/NA thames/loc ./NA london/loc is/NA a/NA village/NA near/NA burgundy/loc ./NA london/loc is/NA situated/NA near/NA ontario/loc ./NA

但我正在寻找一个输出的标签字符串,因为它正在打印字符串, 就像

london is situtated over thames .
  london/loc is/NA situtated/NA over/NA thames/loc .

如果有人建议的话。你知道吗


Tags: intxtistaglocwordoverlondon
1条回答
网友
1楼 · 发布于 2024-10-03 04:33:16

这会产生你期望的结果吗?你知道吗

def tag_corpus():
    corpus1=open("Corpus1.txt","r")
    dict1=open("Dictnew1.txt","r")
    dictw=dict1.read().lower().split()
    for line in corpus1:
        list1=[]
        linew=line.lower().split()
        for word in linew:
            if word in dictw:
                word_i=dictw.index(word)
                word_i1=word_i+1
                tag=dictw[word_i1]
                str1=word+"/"+tag
                list1.append(str1)
            else:
                str2=word+"/"+"NA"
                list1.append(str2)
        str3=" ".join(list1)
        print line
        print str3

相关问题 更多 >