我想执行一个简单的文本分类,执行以下操作: - 检查每个“损失描述”是否包含与灾难相关的关键字 - 如果包含,则根据该类别对其进行分类;否则只显示“非灾难”
请指出我在代码中的错误(数据记录中的第一行只是字段名称)或更有效的编写代码的方法:
disaster_cat = [(('lightning'),'lightning'),
(('hurricane', 'sandy', 'irene', 'isaac', 'gustav'),'Hurricane'),
(('tornado'),'Tornado'),
(('flood'),'Flood'),
(('wildfire', 'wild fire'),'Wild Fire')]
disaster_type = 'Non-Disaster'
for record in datarecords[1:]:
record.append(disaster_type) #pre-populate every field with 'Non-Disaster'
for record in datarecords[1:]:
for pairs in disaster_cat:
for phrase in pairs[0]:
if phrase in record[loss_desc_idx]: #check to see if the loss description contains kw
record[-1] = pairs[1] #if has kw, change disaster type 'Non-Disaster'
#to appropriate diaster category
理想的最终结果,如果损失描述是“我的车被超级风暴桑迪摧毁”,则相应的灾难类型将是“飓风”。