Incorporated some corrections from comments.

This commit is contained in:
Konstantin Mamalakis 2018-02-22 14:01:16 +02:00 committed by Josh Harvey
parent a3f8db79b3
commit 402f75f50c
3 changed files with 39 additions and 31 deletions

View file

@ -5,35 +5,32 @@ const _ = require('lodash/fp')
const debug_log = require('./debug') // KOSTIS TODO: remove
const individuals = []
let individuals = []
function load () {
const newList = Array.from(dataParser.parseList())
const oldLength = individuals.length
individuals.splice(0, oldLength, newList)
individuals = Array.from(dataParser.parseList())
}
load()
// MATCHING
// birth date
function isDateWithinSomeYearsOfPeriod (period, date, years) {
const startDate = new Date(period.from.date)
const startYear = startDate.getFullYear()
startDate.setFullYear(startYear - years)
function isDateWithinSomeDaysOfPeriod (period, date, days) {
const inMillisecs = 24 * 60 * 60 * 1000
const endDate = new Date(period.to.date)
const endYear = endDate.getFullYear()
endDate.setFullYear(endYear + years)
const startTime = period.from.date.getTime() - days * inMillisecs
const startDate = new Date(startTime)
const endTime = period.to.date.getTime() + days * inMillisecs
const endDate = new Date(endTime)
return (startDate < date && date < endDate)
}
function isBornWithinTwoYears (individual, dateObject) {
const isWithinTwoYears = _.partialRight(isDateWithinSomeYearsOfPeriod, [dateObject.date, 2])
return _.some(isWithinTwoYears, individual.birthDatePeriods)
function isBornTooLongSince (individual, dateObject, days) {
if (_.isEmpty(individual.birthDatePeriods)) return false
const isWithinSomeYears = _.partialRight(isDateWithinSomeDaysOfPeriod, [dateObject.date, days])
return !_.some(isWithinSomeYears, individual.birthDatePeriods)
}
// exact match
@ -59,9 +56,10 @@ function calcPhoneticMatchScore (candidatePhoneticFullName) {
const similarity = _.curry((candidate, individual) => {
// Calculate if his birth date is within two years of the given date.
// If an individual has multiple birth-date periods, return wether any are
// If an individual has multiple birth-date periods, return whether any are
// within two years. Reject individuals who don't match this criterion.
if (individual.birthDatePeriods.length && !isBornWithinTwoYears(individual, candidate.birthDate)) return 0
const twoYears = 365 * 2
if (!isBornTooLongSince(individual, candidate.birthDate, twoYears)) return 0
// Calculate the Jaro-Winkler similarity of the full name.
// If an individual has multiple aliases, use the maximum score.