如何从字符串创建有意义的列值对列表?
How to create meaningful column value pair lists from a string?
我正在尝试使用 Python 词典对输入字符串中的列和值(列=值)进行有意义的分类。
input_string = "the status is processing and product subtypes are HL year 30 ARM and applicant name is Ryan"
我已经创建了键值对的字典。在第一种情况下,key 是列名。 值 表示在 input_string
中找到的键的最低索引。
这是列名字典:
dict_columns = {'status': 4, 'product subtypes': 29, 'applicant name': 69}
在上面的字典中,'status'
在input_string
中的索引最低4
。
同样,这是值的字典:
dict_values = {'processing': 14, 'hl': 50, 'year': 53, '30': 58, 'arm': 61, 'ryan': 87}
问题是:
如何获得预期的输出:
list_parsed_values = ['processing', 'hl year 30 arm', 'ryan']
和(可选)对应的列列表为:
list_parsed_columns = ['status', 'product subtypes', 'applicant name']
如何明确区分列表中的值?
检查以下方法:
- 构建正则表达式以根据英语 nltk 停用词列表从结果中删除不相关的词
- 构建正则表达式以使用
dict_columns
键拆分文本
- 拆分后,将结果列表压缩成元组列表
- 从值中删除不相关的词并去除空格
这是我到目前为止的代码:
import nltk, re
s = "the status is processing and product subtypes are HL year 30 ARM and applicant name is Ryan"
dict_columns = {'status': 4, 'product subtypes': 29, 'applicant name': 69}
dict_values = {'processing': 14, 'hl': 50, 'year': 53, '30': 58, 'arm': 61, 'ryan': 87}
# Build the regex to remove irrelevant words from the results
rx_stopwords = r"\b(?:{})\b".format("|".join([x for x in nltk.corpus.stopwords.words("English")]))
# Build the regex to split the text with using the dict_columns keys
rx_split = r"\b({})\b".format("|".join([x for x in dict_columns]))
chunks = re.split(rx_split, s)
# After splitting, zip the resulting list into a tuple list
it = iter(chunks[1:])
lst = list(zip(it, it))
# Remove the irrelevant words from the values and trim them (this can be further enhanced
res = [(x, re.sub(rx_stopwords, "", y).strip()) for x, y in lst]
# =>
# [('status', 'processing'), ('product subtypes', 'HL year 30 ARM'), ('applicant name', 'Ryan')]
# It can be cast to a dictionary
dict(res)
# =>
# {'product subtypes': 'HL year 30 ARM', 'status': 'processing', 'applicant name': 'Ryan'}
我正在尝试使用 Python 词典对输入字符串中的列和值(列=值)进行有意义的分类。
input_string = "the status is processing and product subtypes are HL year 30 ARM and applicant name is Ryan"
我已经创建了键值对的字典。在第一种情况下,key 是列名。 值 表示在 input_string
中找到的键的最低索引。
这是列名字典:
dict_columns = {'status': 4, 'product subtypes': 29, 'applicant name': 69}
在上面的字典中,'status'
在input_string
中的索引最低4
。
同样,这是值的字典:
dict_values = {'processing': 14, 'hl': 50, 'year': 53, '30': 58, 'arm': 61, 'ryan': 87}
问题是:
如何获得预期的输出:
list_parsed_values = ['processing', 'hl year 30 arm', 'ryan']
和(可选)对应的列列表为:
list_parsed_columns = ['status', 'product subtypes', 'applicant name']
如何明确区分列表中的值?
检查以下方法:
- 构建正则表达式以根据英语 nltk 停用词列表从结果中删除不相关的词
- 构建正则表达式以使用
dict_columns
键拆分文本 - 拆分后,将结果列表压缩成元组列表
- 从值中删除不相关的词并去除空格
这是我到目前为止的代码:
import nltk, re
s = "the status is processing and product subtypes are HL year 30 ARM and applicant name is Ryan"
dict_columns = {'status': 4, 'product subtypes': 29, 'applicant name': 69}
dict_values = {'processing': 14, 'hl': 50, 'year': 53, '30': 58, 'arm': 61, 'ryan': 87}
# Build the regex to remove irrelevant words from the results
rx_stopwords = r"\b(?:{})\b".format("|".join([x for x in nltk.corpus.stopwords.words("English")]))
# Build the regex to split the text with using the dict_columns keys
rx_split = r"\b({})\b".format("|".join([x for x in dict_columns]))
chunks = re.split(rx_split, s)
# After splitting, zip the resulting list into a tuple list
it = iter(chunks[1:])
lst = list(zip(it, it))
# Remove the irrelevant words from the values and trim them (this can be further enhanced
res = [(x, re.sub(rx_stopwords, "", y).strip()) for x, y in lst]
# =>
# [('status', 'processing'), ('product subtypes', 'HL year 30 ARM'), ('applicant name', 'Ryan')]
# It can be cast to a dictionary
dict(res)
# =>
# {'product subtypes': 'HL year 30 ARM', 'status': 'processing', 'applicant name': 'Ryan'}