查找命名嵌套列表和数据框列之间的单词匹配
Finding word matches between named nested list and data frame column
我有一个以特定职位类别命名的列表列表,每个嵌套列表都是该职位类别的关键字列表,我正在尝试检查数据框中包含职位列表的列查看关键字是否在职位名称中。最终目标是尽可能对每项工作进行分类。我提供了一个数据样本,因为有超过 15000 个职位和 25 个不同的职位类别可供检查。
这是在 Rstudio 中。我试过将 lapply 与 str_detect 一起使用。以下是我使用的代码。
library(stringr)
cat.keys <- list(Internship='Intern',
Information.Technology=c('IT', 'Information Technology', 'Software', 'Developer'),
Healthcare=c('RN', 'LPN', 'Doctor', 'Nurse'),
Maintenance=c('Custodian', 'Janitor'))
jobs.df <- data.frame(Company=c('Big Brothers Big Sisters', 'Big Brothers Big Sisters',
'Big Brothers Big Sisters', 'American Red Cross', 'American Red Cross',
'American Red Cross', 'DeMolay International', 'Legal Aid Association',
'St.Mary’s Church'),
Job.Title = c('Intern', 'Marketing Intern', 'Special Events Internship Program',
'RN', 'Nurse', 'Registered Nurse', 'Director of IT - DeMolay International',
'SWITCHBOARD/INTAKE SPECIALIST', 'CHURCH CUSTODIAN - part-time'))
lapply(jobs.df$Job.Title,
function(x) sapply(cat.keys, function(y) str_detect(x, fixed(y))))
我希望它成为 return 我的原始 cat.keys 列表长度但具有 TRUE/FALSE 值的列表列表,这就是它 return 的内容。这完成了我想要的大部分内容,但是我遇到的问题是,当在较长的单词中找到较短的单词时(例如,'intern' 也在 'international' 中找到,它会将类似 'International Ambassador' 作为实习或 SWITCHBOARD 会 return IT)。 IT 示例的问题还在于,我正在寻找完全匹配的大小写,但如果职位有不同的大小写,例如 'intern' 而不是 'Intern',则不会匹配,但是如果我这样做是为了忽略大写,RN 的问题出现在 'Intern'.
中出现小写的 rn
您可以利用正则表达式模式中的单词边界(并使用 regex()
,而不是 fixed()
)来帮助您进行搜索。这应该让您开始了——如果您 运行 遇到更多问题,请告诉我:
# Adding word boundaries to each string
cat.keys2 <- lapply(cat.keys, function(x) paste0("\b", x, "\b"))
# Using new cat.key with regex() and ignoring case
lapply(jobs.df$Job.Title,
function(x) sapply(cat.keys2, function(y) str_detect(x, regex(y, ignore_case = T))))
此外,既然您正在使用正则表达式,您可以将 "\bIntern\b"
更改为 "\bIntern\b|\bInternship\b"
(又名,您可以将您的模式合并为一个),或者您可以像以前一样添加它做,当然。任何适合您的需求。
我有一个以特定职位类别命名的列表列表,每个嵌套列表都是该职位类别的关键字列表,我正在尝试检查数据框中包含职位列表的列查看关键字是否在职位名称中。最终目标是尽可能对每项工作进行分类。我提供了一个数据样本,因为有超过 15000 个职位和 25 个不同的职位类别可供检查。
这是在 Rstudio 中。我试过将 lapply 与 str_detect 一起使用。以下是我使用的代码。
library(stringr)
cat.keys <- list(Internship='Intern',
Information.Technology=c('IT', 'Information Technology', 'Software', 'Developer'),
Healthcare=c('RN', 'LPN', 'Doctor', 'Nurse'),
Maintenance=c('Custodian', 'Janitor'))
jobs.df <- data.frame(Company=c('Big Brothers Big Sisters', 'Big Brothers Big Sisters',
'Big Brothers Big Sisters', 'American Red Cross', 'American Red Cross',
'American Red Cross', 'DeMolay International', 'Legal Aid Association',
'St.Mary’s Church'),
Job.Title = c('Intern', 'Marketing Intern', 'Special Events Internship Program',
'RN', 'Nurse', 'Registered Nurse', 'Director of IT - DeMolay International',
'SWITCHBOARD/INTAKE SPECIALIST', 'CHURCH CUSTODIAN - part-time'))
lapply(jobs.df$Job.Title,
function(x) sapply(cat.keys, function(y) str_detect(x, fixed(y))))
我希望它成为 return 我的原始 cat.keys 列表长度但具有 TRUE/FALSE 值的列表列表,这就是它 return 的内容。这完成了我想要的大部分内容,但是我遇到的问题是,当在较长的单词中找到较短的单词时(例如,'intern' 也在 'international' 中找到,它会将类似 'International Ambassador' 作为实习或 SWITCHBOARD 会 return IT)。 IT 示例的问题还在于,我正在寻找完全匹配的大小写,但如果职位有不同的大小写,例如 'intern' 而不是 'Intern',则不会匹配,但是如果我这样做是为了忽略大写,RN 的问题出现在 'Intern'.
中出现小写的 rn您可以利用正则表达式模式中的单词边界(并使用 regex()
,而不是 fixed()
)来帮助您进行搜索。这应该让您开始了——如果您 运行 遇到更多问题,请告诉我:
# Adding word boundaries to each string
cat.keys2 <- lapply(cat.keys, function(x) paste0("\b", x, "\b"))
# Using new cat.key with regex() and ignoring case
lapply(jobs.df$Job.Title,
function(x) sapply(cat.keys2, function(y) str_detect(x, regex(y, ignore_case = T))))
此外,既然您正在使用正则表达式,您可以将 "\bIntern\b"
更改为 "\bIntern\b|\bInternship\b"
(又名,您可以将您的模式合并为一个),或者您可以像以前一样添加它做,当然。任何适合您的需求。