在python中用句子和标签拆分行

2024-04-29 13:14:59 发布

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

I have a sample of a file with sentences and labels. How can it be split into sentences and labels?

一部非常,非常,非常缓慢,漫无目的的电影,讲述一个痛苦的,漂泊的年轻人。0

不确定是谁更迷失了方向——平淡的角色还是观众,其中近一半的人都走了。0

这部电影试图用黑白和巧妙的镜头角度来表现艺术性,但由于演技不佳,情节和台词几乎不存在,这部电影令人失望——变得更加荒谬。0

很少有音乐或任何值得一提的东西。0

输出
句子列表:
[“一部非常,非常,非常缓慢,漫无目的的电影,讲述一个痛苦的,漂泊的年轻人,'不确定谁更迷失了-平淡的角色或观众,其中近一半的人退出了']

对应标签:
['0','0']


Tags: andofsample角色labels电影havewith
2条回答

假设最后一个“.”(点)后面的数字是标签

对于给定的示例,当存储在文件中时你的数据.txt'下面的代码应该生成2个列表sentence_listlabel_list。您可以将这些列表中的数据分别写入文件,然后根据您的要求。在

fmov=open('yourdata.txt','r')
sentence_list=[]
label_list=[]
for f in fmov.readlines():
    lineinfo=f.split('.')
    sentenceline=".".join(lineinfo[0:-1])
    sentence_list.append(sentenceline)
    label_list.append(str(lineinfo[-1]).replace('\n',''))
print(sentence_list)
print(label_list) 

OUT:
['A very, very, very slow-moving, aimless movie about a distressed, drifting young man', 'Not sure who was more lost - the flat characters or the audience, nearly half of whom walked out', 'Attempting artiness with black & white and clever camera angles, the movie disappointed - became even more ridiculous - as the acting was poor and the plot and lines almost non-existent', 'Very little music or anything to speak of']
[' 0', ' 0', ' 0', ' 0']

“0”是标签吗?如果只有一个句子,可以使用句点作为分隔符。但是如果你有一个像“先生”或“夫人”这样的句子,这可能会捕捉到一些错误,所以你可能需要添加一些if语句来处理这些错误。在

相关问题 更多 >