文本挖掘抓取数据 (R)
Text Mining Scraped Data (R)
我编写了下面的代码以在职位发布数据集中查找“国籍”一词,我实际上是在尝试查看有多少雇主指定给定候选人必须具有特定签证类型或国籍。
我知道在原始数据本身(在excel中),有几种情况在职位描述中提到了“国籍”一词。
nationality_finder = function(string){
nationality = c(" ")
split_string = strsplit(string, split = NULL)
split_string = split_string[[1]]
flag = 0
for(letter in split_string){
if(flag > 0){nationality = append(nationality, letter)}
if(letter == "nationality "){flag = 1}
if(letter == " "){flag = flag-0.5}
}
nationality = paste(nationality, collapse = '')
return(nationality)
}
for(n in 1:length(df2$description)){
df2$nationality[n] <- nationality_finder(df2$description[n])
}
df2%>%
view()
此外,代码运行 w/out 错误,但它没有产生我正在寻找的东西。我本质上是想创建另一个变量,其中 1 表示提及“国籍”一词,否则为 0。具体来说,我在职位描述变量下寻找诸如“公民”和“国籍”之类的词。而且每个职位描述下的文字都非常长,但在这里,为了简洁起见,我只给出了一个摘要版本。
数据集中职位描述的文本示例
Title: Event Planner
Nationality: Saudi National
Location: Riyadh, Saudi Arabia
Salary: Open
Salary depends on the candidates skills, experience, and other attributes.
另一个职位描述:
- Have recently graduated or looking for a career change and be looking for
an entry level role (we will offer full training)
- Priority will be taken for applications by U.S. nationality holders
你可以尝试这样的事情。我假设您有一个 data.frame
作为数据,并且您想要添加一个新列。
dats$check <- as.numeric(grepl("nationality",dats$description,ignore.case=TRUE))
dats$check
[1] 1 1 0 1
grepl()
将在 dats$description
列中检测字符串国籍,忽略大小写 (ignore.case = TRUE
) 并且 as.numeric()
将转换 TRUE
FALSE
变成 1
0
.
假数据:
dats <- structure(list(description = c("Title: Event Planner\n \n Nationality: Saudi National\n \n Location: Riyadh, Saudi Arabia\n \n Salary: Open\n \n Salary depends on the candidates skills, experience, and other attributes.",
"- Have recently graduated or looking for a career change and be looking for\n an entry level role (we will offer full training) \n \n - Priority will be taken for applications by U.S. nationality holders ",
"do not have that word here", "aaaaNationalitybb"), check = c(1,
1, 0, 1)), row.names = c(NA, -4L), class = "data.frame")
我编写了下面的代码以在职位发布数据集中查找“国籍”一词,我实际上是在尝试查看有多少雇主指定给定候选人必须具有特定签证类型或国籍。
我知道在原始数据本身(在excel中),有几种情况在职位描述中提到了“国籍”一词。
nationality_finder = function(string){
nationality = c(" ")
split_string = strsplit(string, split = NULL)
split_string = split_string[[1]]
flag = 0
for(letter in split_string){
if(flag > 0){nationality = append(nationality, letter)}
if(letter == "nationality "){flag = 1}
if(letter == " "){flag = flag-0.5}
}
nationality = paste(nationality, collapse = '')
return(nationality)
}
for(n in 1:length(df2$description)){
df2$nationality[n] <- nationality_finder(df2$description[n])
}
df2%>%
view()
此外,代码运行 w/out 错误,但它没有产生我正在寻找的东西。我本质上是想创建另一个变量,其中 1 表示提及“国籍”一词,否则为 0。具体来说,我在职位描述变量下寻找诸如“公民”和“国籍”之类的词。而且每个职位描述下的文字都非常长,但在这里,为了简洁起见,我只给出了一个摘要版本。
数据集中职位描述的文本示例
Title: Event Planner
Nationality: Saudi National
Location: Riyadh, Saudi Arabia
Salary: Open
Salary depends on the candidates skills, experience, and other attributes.
另一个职位描述:
- Have recently graduated or looking for a career change and be looking for
an entry level role (we will offer full training)
- Priority will be taken for applications by U.S. nationality holders
你可以尝试这样的事情。我假设您有一个 data.frame
作为数据,并且您想要添加一个新列。
dats$check <- as.numeric(grepl("nationality",dats$description,ignore.case=TRUE))
dats$check
[1] 1 1 0 1
grepl()
将在 dats$description
列中检测字符串国籍,忽略大小写 (ignore.case = TRUE
) 并且 as.numeric()
将转换 TRUE
FALSE
变成 1
0
.
假数据:
dats <- structure(list(description = c("Title: Event Planner\n \n Nationality: Saudi National\n \n Location: Riyadh, Saudi Arabia\n \n Salary: Open\n \n Salary depends on the candidates skills, experience, and other attributes.",
"- Have recently graduated or looking for a career change and be looking for\n an entry level role (we will offer full training) \n \n - Priority will be taken for applications by U.S. nationality holders ",
"do not have that word here", "aaaaNationalitybb"), check = c(1,
1, 0, 1)), row.names = c(NA, -4L), class = "data.frame")