@ -3,7 +3,8 @@
#' Generate actor search queries based on data in actor db
#' Generate actor search queries based on data in actor db
#' @param actor A row from the output of elasticizer() when run on the 'actor' index
#' @param actor A row from the output of elasticizer() when run on the 'actor' index
#' @param country 2-letter string indicating the country for which to generate the queries, is related to inflected nouns, definitive forms and genitive forms of names etc.
#' @param country 2-letter string indicating the country for which to generate the queries, is related to inflected nouns, definitive forms and genitive forms of names etc.
#' @param identifier Identifier used to mark hits in the text, identifiers are prepended before the actual hit
#' @param pre_tags Highlighter pre-tag
#' @param post_tags Highlighter post-tag
#' @return A data frame containing the queries, related actor ids and actor function
#' @return A data frame containing the queries, related actor ids and actor function
#' @export
#' @export
#' @examples
#' @examples
@ -12,8 +13,27 @@
#################################################################################################
#################################################################################################
#################################### Actor search query generator ###############################
#################################### Actor search query generator ###############################
#################################################################################################
#################################################################################################
query_gen_actors <- function ( actor , country , identifier ) {
query_gen_actors <- function ( actor , country , pre_tags , post_tags ) {
highlight <- paste0 ( ' " h i g h l i g h t " : {
generator <- function ( country , startdate , enddate , querystring , pre_tags , post_tags , actorid ) {
return ( paste0 ( ' { " q u e r y " :
{ " bool" : {
" filter" : [
{ " term" : { " country" : " ',country,'" } } ,
{ " range" : { " publication_date" : { " gte" : " ',startdate,'" , " lte" : " ',enddate,'" } } } ,
{ " query_string" : {
" default_operator" : " OR" ,
" allow_leading_wildcard" : " false" ,
" fields" : [ " text" , " teaser" , " preteaser" , " title" , " subtitle" ] ,
" query" : " ', querystring,'"
}
}
] ,
" must_not" : [
{ " term" : { " computerCodes.actors" : " ',actorid,'" } }
]
}
} ,
" highlight" : {
" fields" : {
" fields" : {
" text" : { } ,
" text" : { } ,
" teaser" : { } ,
" teaser" : { } ,
@ -25,27 +45,31 @@ query_gen_actors <- function(actor, country, identifier) {
" order" : " none" ,
" order" : " none" ,
" type" : " unified" ,
" type" : " unified" ,
" fragment_size" : 0 ,
" fragment_size" : 0 ,
" pre_tags" : " ',identifier,'" ,
" pre_tags" : " ', pre_tags,'" ,
" post_tags" : " "
" post_tags" : " ',post_tags,'"
} ' )
}
if ( country == " no" ) {
} ' ) )
}
### Setting linguistic forms for each country ###
if ( country == " no" | country == " dk" ) {
genitive <- ' s'
genitive <- ' s'
definitive <- ' en'
definitive <- ' en'
definitive_genitive <- ' ens'
definitive_genitive <- ' ens'
} else if ( country == ' uk' ) {
} else if ( country == ' uk' ) {
genitive <- ' \'s'
genitive <- ' \'s'
definitive <- ' s'
} else if ( country == ' nl' | country == ' be' ) {
definitive_genitive <- ' '
genitive <- ' s'
} else {
genitive <- ' '
definitive <- ' '
definitive_genitive <- ' '
}
}
if ( actor $ `_source.function` == " Min" | actor $ `_source.function` == " PM" | actor $ `_source.function` == " PartyLeader" ) {
### Generating queries for individuals (ministers, PM, Party leaders and MPs)
if ( actor $ `_source.function` == " Minister" | actor $ `_source.function` == " PM" | actor $ `_source.function` == " PartyLeader" | actor $ `_source.function` == " MP" ) {
lastname <- paste0 ( ' (' , actor $ `_source.lastName` , ' OR ' , actor $ `_source.lastName` , genitive , ' )' )
lastname <- paste0 ( ' (' , actor $ `_source.lastName` , ' OR ' , actor $ `_source.lastName` , genitive , ' )' )
## Adding a separate AND clause for inclusion of only last name to highlight all occurences of last name
## Adding a separate AND clause for inclusion of only last name to highlight all occurences of last name
## Regardless of whether the last name hit is because of a minister name or a full name proximity hit
## Regardless of whether the last name hit is because of a minister name or a full name proximity hit
### If country is belgium, check if there is an apostrophe in middlenames, if so, search for last name both with capitalized and lowercased last name
if ( country == ' be' ) {
if ( T %in% str_detect ( actor $ `_source.middleNames` , " '" ) ) {
query_string <- paste0 ( ' (((\\"' ,
query_string <- paste0 ( ' (((\\"' ,
actor $ `_source.firstName` ,
actor $ `_source.firstName` ,
' ' ,
' ' ,
@ -53,93 +77,106 @@ if (country == "no") {
' \\"~5 OR \\"' ,
' \\"~5 OR \\"' ,
actor $ `_source.firstName` ,
actor $ `_source.firstName` ,
' ' ,
' ' ,
tolower ( actor $ `_source.lastName` ) ,
' \\"~5 OR \\"' ,
actor $ `_source.firstName` ,
' ' ,
tolower ( actor $ `_source.lastName` ) , genitive ,
' \\"~5 OR \\"' ,
actor $ `_source.firstName` ,
' ' ,
actor $ `_source.lastName` , genitive ,
actor $ `_source.lastName` , genitive ,
' \\"~5) AND ' , lastname )
' \\"~5) AND ' , lastname )
}
}
if ( actor $ `_source.function` == " PartyLeader" ) {
} else {
query_string <- paste0 ( query_string , ' ))' )
query_string <- paste0 ( ' (((\\"' ,
ids <- toJSON ( unlist ( lapply ( c ( actor $ `_source.actorId` , actor $ `_source.partyId` ) , str_c , " _pl" ) ) )
actor $ `_source.firstName` ,
' ' ,
actor $ `_source.lastName` ,
' \\"~5 OR \\"' ,
actor $ `_source.firstName` ,
' ' ,
actor $ `_source.lastName` , genitive ,
' \\"~5) AND ' , lastname )
}
}
if ( actor $ `_source.function` == " Min" | actor $ `_source.function` == " PM" ) {
## Modifiers are only applied to minister titles
### If actor is a minister, generate minister search
if ( actor $ `_source.function` == " Minister" | actor $ `_source.function` == " PM" ) {
capital <- unlist ( lapply ( actor $ `_source.ministerSearch` , str_to_title ) )
capital <- unlist ( lapply ( actor $ `_source.ministerSearch` , str_to_title ) )
capital_gen <- unlist ( lapply ( capital , str_c , genitive ) )
capital_gen <- unlist ( lapply ( capital , str_c , genitive ) )
gen <- unlist ( lapply ( actor $ `_source.ministerSearch` , str_c , genitive ) )
# If country is no or dk, search for definitive minister forms as well (including genitive forms)
if ( country == " no" || country == " dk" ) {
capital_def <- unlist ( lapply ( capital , str_c , definitive ) )
capital_def <- unlist ( lapply ( capital , str_c , definitive ) )
capital_defgen <- unlist ( lapply ( capital , str_c , definitive_genitive ) )
capital_defgen <- unlist ( lapply ( capital , str_c , definitive_genitive ) )
gen <- unlist ( lapply ( actor $ `_source.ministerSearch` , str_c , genitive ) )
def <- unlist ( lapply ( actor $ `_source.ministerSearch` , str_c , definitive ) )
def <- unlist ( lapply ( actor $ `_source.ministerSearch` , str_c , definitive ) )
defgen <- unlist ( lapply ( actor $ `_source.ministerSearch` , str_c , definitive_genitive ) )
defgen <- unlist ( lapply ( actor $ `_source.ministerSearch` , str_c , definitive_genitive ) )
names <- paste ( c ( capital , capital_gen , gen , capital_def , def , defgen , capital_defgen ) , collapse = ' ' )
names <- paste ( c ( capital , capital_gen , gen , capital_def , def , defgen , capital_defgen ) , collapse = ' ' )
} else {
names <- paste ( c ( capital , capital_gen , gen , actor $ `_source.ministerSearch` ) , collapse = ' ' )
}
# If country is uk, search for last name, minister names and minister titles (i.e. Johnson AND state AND secretary)
if ( country == " uk" ) {
minister <- paste ( c ( str_to_title ( actor $ `_source.ministerName` ) ,
actor $ `_source.ministerName` ,
str_c ( str_to_title ( actor $ `_source.ministerName` ) , genitive ) ,
str_c ( actor $ `_source.ministerName` , genitive ) ) , collapse = ' ' )
query_string <- paste0 ( query_string , ' ) OR (' , lastname , ' AND (' , names , ' ) AND (' , unlist ( minister ) , ' )))' )
}
# If country is nl or be, add a requirement for Minister to the query
else if ( country == " nl" | country == " be" ) {
query_string <- paste0 ( query_string , ' ) OR (' , lastname , ' AND (' , names , ' ) AND ("Minister" OR "minister")))' )
} else {
query_string <- paste0 ( query_string , ' ) OR (' , lastname , ' AND (' , names , ' )))' )
query_string <- paste0 ( query_string , ' ) OR (' , lastname , ' AND (' , names , ' )))' )
ids <- toJSON ( unlist ( lapply ( c ( actor $ `_source.actorId` , actor $ `_source.ministryId` , actor $ `_source.partyId` ) , str_c , " _min" ) ) )
}
}
if ( actor $ `_source.function` == " Institution" ) {
} else {
#uppercasing
### Else, generate search for first/last name only (MPs and Party leaders, currently)
firstup <- function ( x ) {
query_string <- paste0 ( query_string , ' ))' )
substr ( x , 1 , 1 ) <- toupper ( substr ( x , 1 , 1 ) )
x
}
}
ids <- list ( c ( actor $ `_source.actorId` , str_c ( actor $ `_source.partyId` , ' _a' ) ) )
actorid <- actor $ `_source.actorId`
query <- generator ( country , actor $ `_source.startDate` , actor $ `_source.endDate` , query_string , pre_tags , post_tags , actorid )
return ( data.frame ( query = query , ids = I ( ids ) , stringsAsFactors = F ) )
}
### Query generation for party searches
if ( actor $ `_source.function` == " Party" ) {
actor $ `_source.startDate` <- " 2000-01-01"
actor $ `_source.startDate` <- " 2000-01-01"
actor $ `_source.endDate` <- " 2099-01-01"
actor $ `_source.endDate` <- " 2099-01-01"
if ( nchar ( actor $ `_source.institutionNameSearch` [ [1 ] ] ) > 0 ) {
if ( nchar ( actor $ `_source.partyNameSearchShort` [ [1 ] ] ) > 0 ) {
upper <- unlist ( lapply ( actor $ `_source.institutionNameSearch` , firstup ) )
# If uk, no or dk, search for both regular abbreviations, and genitive forms
upper <- c ( upper , unlist ( lapply ( upper , str_c , genitive ) ) ,
if ( country == " uk" | country == " no" | country == " dk" ) {
unlist ( lapply ( upper , str_c , definitive ) ) ,
gen <- unlist ( lapply ( actor $ `_source.partyNameSearchShort` , str_c , genitive ) )
unlist ( lapply ( upper , str_c , definitive_genitive ) ) )
names <- paste ( c ( gen , actor $ `_source.partyNameSearchShort` ) , collapse = ' \\\" \\\"' )
capital <- unlist ( lapply ( actor $ `_source.institutionNameSearch` , str_to_title ) )
} else {
capital <- c ( capital , unlist ( lapply ( capital , str_c , genitive ) ) ,
names <- paste ( actor $ `_source.partyNameSearchShort` , collapse = ' \\\" \\\"' )
unlist ( lapply ( capital , str_c , definitive ) ) ,
unlist ( lapply ( capital , str_c , definitive_genitive ) ) )
base <- actor $ `_source.institutionNameSearch`
base <- c ( base , unlist ( lapply ( base , str_c , genitive ) ) ,
unlist ( lapply ( base , str_c , definitive ) ) ,
unlist ( lapply ( base , str_c , definitive_genitive ) ) )
names <- paste ( unique ( c ( upper , capital , base ) ) , collapse = ' \\" \\"' )
query_string <- paste0 ( ' (\\"' , names , ' \\")' )
ids <- toJSON ( unlist ( lapply ( c ( actor $ `_source.institutionId` ) , str_c , " _f" ) ) )
query <- paste0 ( ' { " q u e r y " :
{ " bool" : { " filter" : [ { " term" : { " country" : " ',country,'" } } ,
{ " range" : { " publication_date" : { " gte" : " ',actor$`_source.startDate`,'" , " lte" : " ',actor$`_source.endDate`,'" } } } ,
{ " query_string" : {
" default_operator" : " OR" ,
" allow_leading_wildcard" : " false" ,
" fields" : [ " text" , " teaser" , " preteaser" , " title" , " subtitle" ] ,
" query" : " ', query_string,'"
}
}
# If no or dk, only keep genitive forms if the party abbreviation is longer than 1 character (2 including the genitive s itself)
if ( country == " dk" | country == " no" ) {
gen <- gen [which ( nchar ( gen ) > 2 ) ]
}
}
]
query_string <- paste0 ( ' (\\\"' , names , ' \\\")' )
} } , ' ,highlight,' } ' )
ids <- str_c ( actor $ `_source.partyId` , ' _s' )
df1 <- data.frame ( query = query , ids = I ( ids ) , type = actor $ `_source.function` , stringsAsFactors = F )
actorid <- str_c ( actor $ `_source.partyId` , ' _s' )
}
query <- generator ( country , actor $ `_source.startDate` , actor $ `_source.endDate` , query_string , pre_tags , post_tags , actorid )
if ( nchar ( actor $ `_source.institutionNameSearchShort` [ [1 ] ] ) > 0 ) {
df1 <- data.frame ( query = query , ids = I ( ids ) , prefix = actor $ `_source.notPrecededBy` , postfix = actor $ `_source.notFollowedBy` , stringsAsFactors = F )
upper <- unlist ( lapply ( actor $ `_source.institutionNameSearchShort` , firstup ) )
upper <- c ( upper , unlist ( lapply ( upper , str_c , genitive ) ) ,
unlist ( lapply ( upper , str_c , definitive ) ) ,
unlist ( lapply ( upper , str_c , definitive_genitive ) ) )
capital <- unlist ( lapply ( actor $ `_source.institutionNameSearchShort` , str_to_title ) )
capital <- c ( capital , unlist ( lapply ( capital , str_c , genitive ) ) ,
unlist ( lapply ( capital , str_c , definitive ) ) ,
unlist ( lapply ( capital , str_c , definitive_genitive ) ) )
base <- actor $ `_source.institutionNameSearchShort`
base <- c ( base , unlist ( lapply ( base , str_c , genitive ) ) ,
unlist ( lapply ( base , str_c , definitive ) ) ,
unlist ( lapply ( base , str_c , definitive_genitive ) ) )
names <- paste ( unique ( c ( upper , capital , base ) ) , collapse = ' \\" \\"' )
query_string <- paste0 ( ' (\\"' , names , ' \\")' )
ids <- toJSON ( unlist ( lapply ( c ( actor $ `_source.institutionId` ) , str_c , " _s" ) ) )
query <- paste0 ( ' { " q u e r y " :
{ " bool" : { " filter" : [ { " term" : { " country" : " ',country,'" } } ,
{ " range" : { " publication_date" : { " gte" : " ',actor$`_source.startDate`,'" , " lte" : " ',actor$`_source.endDate`,'" } } } ,
{ " query_string" : {
" default_operator" : " OR" ,
" allow_leading_wildcard" : " false" ,
" fields" : [ " text" , " teaser" , " preteaser" , " title" , " subtitle" ] ,
" query" : " ', query_string,'"
}
}
if ( nchar ( actor $ `_source.partyNameSearch` [ [1 ] ] ) > 0 ) {
if ( country == " uk" | country == " no" | country == " dk" ) {
gen <- unlist ( lapply ( actor $ `_source.partyNameSearch` , str_c , genitive ) )
names <- paste ( c ( gen , actor $ `_source.partyNameSearch` ) , collapse = ' \\\" \\\"' )
} else {
names <- paste ( actor $ `_source.partyNameSearch` , collapse = ' \\\" \\\"' )
}
query_string <- paste0 ( ' (\\\"' , names , ' \\\")' )
ids <- str_c ( actor $ `_source.partyId` , ' _f' )
actorid <- str_c ( actor $ `_source.partyId` , ' _f' )
query <- generator ( country , actor $ `_source.startDate` , actor $ `_source.endDate` , query_string , pre_tags , post_tags , actorid )
if ( country == ' uk' ) {
df2 <- data.frame ( query = query , ids = I ( ids ) , prefix = actor $ `_source.notPrecededBy` , postfix = actor $ `_source.notFollowedBy` , stringsAsFactors = F )
} else {
df2 <- data.frame ( query = query , ids = I ( ids ) , stringsAsFactors = F )
}
}
]
} } , ' ,highlight,' } ' )
df2 <- data.frame ( query = query , ids = I ( ids ) , type = actor $ `_source.function` , stringsAsFactors = F )
}
}
if ( exists ( ' df1' ) == T & exists ( ' df2' ) == T ) {
if ( exists ( ' df1' ) == T & exists ( ' df2' ) == T ) {
return ( bind_rows ( df1 , df2 ) )
return ( bind_rows ( df1 , df2 ) )
@ -149,63 +186,61 @@ if (country == "no") {
return ( df2 )
return ( df2 )
}
}
}
}
if ( actor $ `_source.function` == " Party" ) {
### Institution function currently not used
actor $ `_source.startDate` <- " 2000-01-01"
# if (actor$`_source.function` == "Institution") {
actor $ `_source.endDate` <- " 2099-01-01"
# #uppercasing
names <- paste ( c ( unlist ( actor $ `_source.partyNameSearchShort` ) ) , collapse = ' \\" \\"' )
# firstup <- function(x) {
query_string <- paste0 ( ' (\\"' , names , ' \\")' )
# substr(x, 1, 1) <- toupper(substr(x, 1, 1))
query <- paste0 ( ' { " q u e r y " :
# x
{ " bool" : { " filter" : [ { " term" : { " country" : " ',country,'" } } ,
# }
{ " range" : { " publication_date" : { " gte" : " ',actor$`_source.startDate`,'" , " lte" : " ',actor$`_source.endDate`,'" } } } ,
# actor$`_source.startDate` <- "2000-01-01"
{ " query_string" : {
# actor$`_source.endDate` <- "2099-01-01"
" default_operator" : " OR" ,
# if (nchar(actor$`_source.institutionNameSearch`[[1]]) > 0) {
" allow_leading_wildcard" : " false" ,
# upper <- unlist(lapply(actor$`_source.institutionNameSearch`, firstup))
" fields" : [ " text" , " teaser" , " preteaser" , " title" , " subtitle" ] ,
# upper <- c(upper, unlist(lapply(upper, str_c, genitive)),
" query" : " ', query_string,'"
# unlist(lapply(upper, str_c, definitive)),
}
# unlist(lapply(upper, str_c, definitive_genitive)))
}
# capital <- unlist(lapply(actor$`_source.institutionNameSearch`, str_to_title))
]
# capital <- c(capital, unlist(lapply(capital, str_c, genitive)),
} } , ' ,highlight,' } ' )
# unlist(lapply(capital, str_c, definitive)),
ids <- c ( toJSON ( unlist ( lapply ( c ( actor $ `_source.partyId` ) , str_c , " _p" ) ) ) )
# unlist(lapply(capital, str_c, definitive_genitive)))
# base <- actor$`_source.institutionNameSearch`
if ( nchar ( actor $ `_source.partyNameSearch` [ [1 ] ] ) > 0 ) {
# base <- c(base, unlist(lapply(base, str_c, genitive)),
names <- paste ( c ( unlist ( actor $ `_source.partyNameSearch` ) ) , collapse = ' \\" \\"' )
# unlist(lapply(base, str_c, definitive)),
query_string <- paste0 ( ' (\\"' , names , ' \\")' )
# unlist(lapply(base, str_c, definitive_genitive)))
query2 <- paste0 ( ' { " q u e r y " :
# names <- paste(unique(c(upper,capital,base)), collapse = '\\" \\"')
{ " bool" : { " filter" : [ { " term" : { " country" : " ',country,'" } } ,
# query_string <- paste0('(\\"',names,'\\")')
{ " range" : { " publication_date" : { " gte" : " ',actor$`_source.startDate`,'" , " lte" : " ',actor$`_source.endDate`,'" } } } ,
# ids <- toJSON(unlist(lapply(c(actor$`_source.institutionId`),str_c, "_f")))
{ " query_string" : {
# actorid <- str_c(actor$`_source.institutionId`,'_f')
" default_operator" : " OR" ,
# query <- generator(country, actor$`_source.startDate`, actor$`_source.endDate`, query_string, pre_tags, post_tags, actorid)
" allow_leading_wildcard" : " false" ,
# df1 <- data.frame(query = query, ids = I(ids), stringsAsFactors = F)
" fields" : [ " text" , " teaser" , " preteaser" , " title" , " subtitle" ] ,
# }
" query" : " ', query_string,'"
# if (nchar(actor$`_source.institutionNameSearchShort`[[1]]) > 0) {
}
# upper <- unlist(lapply(actor$`_source.institutionNameSearchShort`, firstup))
}
# upper <- c(upper, unlist(lapply(upper, str_c, genitive)),
]
# unlist(lapply(upper, str_c, definitive)),
} } , ' ,highlight,' } ' )
# unlist(lapply(upper, str_c, definitive_genitive)))
# capital <- unlist(lapply(actor$`_source.institutionNameSearchShort`, str_to_title))
# capital <- c(capital, unlist(lapply(capital, str_c, genitive)),
ids <- c ( ids , toJSON ( unlist ( lapply ( c ( actor $ `_source.partyId` ) , str_c , " _p" ) ) ) )
# unlist(lapply(capital, str_c, definitive)),
query <- c ( query , query2 )
# unlist(lapply(capital, str_c, definitive_genitive)))
fn <- c ( ' PartyAbbreviation' , ' Party' )
# base <- actor$`_source.institutionNameSearchShort`
} else {
# base <- c(base, unlist(lapply(base, str_c, genitive)),
fn <- c ( ' PartyAbbreviation' )
# unlist(lapply(base, str_c, definitive)),
}
# unlist(lapply(base, str_c, definitive_genitive)))
return ( data.frame ( query = query , ids = I ( ids ) , type = fn , prefix = actor $ `_source.searchAnd` , postfix = actor $ `_source.searchAndNot` , stringsAsFactors = F ) )
# names <- paste(unique(c(upper,capital,base)), collapse = '\\" \\"')
}
# query_string <- paste0('(\\"',names,'\\")')
# ids <- toJSON(unlist(lapply(c(actor$`_source.institutionId`),str_c, "_s")))
query <- paste0 ( ' { " q u e r y " :
# actorid <- str_c(actor$`_source.institutionId`,'_s')
{ " bool" : { " filter" : [ { " term" : { " country" : " ',country,'" } } ,
# query <- generator(country, actor$`_source.startDate`, actor$`_source.endDate`, query_string, pre_tags, post_tags, actorid)
{ " range" : { " publication_date" : { " gte" : " ',actor$`_source.startDate`,'" , " lte" : " ',actor$`_source.endDate`,'" } } } ,
# df2 <- data.frame(query = query, ids = I(ids), stringsAsFactors = F)
{ " query_string" : {
# }
" default_operator" : " OR" ,
# if (exists('df1') == T & exists('df2') == T) {
" allow_leading_wildcard" : " false" ,
# return(bind_rows(df1,df2))
" fields" : [ " text" , " teaser" , " preteaser" , " title" , " subtitle" ] ,
# } else if (exists('df1') == T) {
" query" : " ', query_string,'"
# return(df1)
}
# } else if (exists('df2') == T) {
}
# return(df2)
]
# }
} } , ' ,highlight,' } ' )
# }
fn <- actor $ `_source.function`
return ( data.frame ( query = query , ids = I ( ids ) , type = fn , stringsAsFactors = F ) )
}
}