【发布时间】:2021-06-16 20:22:59
【问题描述】:
我有下面的代码来遍历列的句子,标记句子中的关键字,并创建由 1 和 0 组成的这些标记的新列。如果存在关键字,则会自动对其进行标记,并在以该标记命名的新列中给出 1。如果它不存在但另一个关键字存在,则给它一个 0。如果该句子没有任何关键字,则将删除整行。
下面的代码还可以,但它仍然遗漏了关键字,它标记并在部分单词和空白单元格(没有句子的行)上输出 1 和 0。我不确定缺少什么?如何保证不漏掉关键词,不标注部分词和空句?
pattern = '|'.join(dict_list)
tags_id = (df['description_summary']
.str.extractall(f'({pattern})')[0]
.map(keyword_dict)
.reset_index(name='col')
.assign(value=1)
.pivot_table(index=[df['issue.id'], df['description_summary']], columns='col', values='value', fill_value=0))
这基本上是我在 excel 文件中使用的数据:
issue.id description_summary
0 753 Long sentence with keywords ball and hot
1 937 Long sentence with keywords cold, stick, and glove
2
3 598 Long sentence with NO keywords
4 574 Long sentence with keywords very cold and cold
这是当前(错误的)输出:
issue.id description_summary Toy Temperature
0 753 Long sentence with keywords ball and hot 1 1
1 937 Long sentence with keywords cold, stick, and glove 1 1
2 1 0
3 598 Long sentence with NO keywords but outputs 1s and 0s 0 1
4 574 Long sentence with keywords very cold and cold 1 1
这是我想要的输出:
issue.id description_summary Toy Temperature
0 753 Long sentence with keywords ball and hot 1 1
1 937 Long sentence with keywords cold, stick, and glove 1 1
4 574 Long sentence with keywords very cold and cold 0 1
这里是关键字和标签的字典('keywords': 'tags'):
dict_list = {'Hot': 'Temperature',
'Cold': 'Temperature',
'Very cold': 'Temperature',
'Ball': 'Toy',
'Glove': 'Toy',
'Stick': 'Toy'
}
如何保证不漏掉关键词,不标注部分词和空句?
【问题讨论】:
标签: python pandas nlp tags keyword