Created
November 22, 2017 22:50
-
-
Save Jahans3/117ba5b403f6206e4a39357d01242910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const H = require ( 'highland' ); | |
const R = require ( 'ramda' ); | |
const itemGenerator = require ( '../lib/itemGenerator.js' ); | |
const itemStore = require ( '../lib/itemStore.js' ); | |
const filterBySpotContext = require ( '../lib/filterBySpotContext.js' ); | |
const config = require ( 'config' ); | |
module.exports = R.curry ( ( utils, req, res ) => { | |
const annotationUris = R.reject ( R.isEmpty, ( req.query.annotation_uri || '' ).split ( '~' ) ); | |
const confidence = parseFloat ( req.query.confidence || config.defaultConfidence ); | |
const count = parseInt ( req.query.count || 100 ); | |
const apiCount = count * 2; | |
if ( R.isEmpty ( annotationUris ) ) { | |
return utils.error ( res, { | |
code: 400, | |
message: 'Please specify at least one annotation_uri' | |
} ); | |
} | |
return H ( R.map ( annotation_uri => { | |
return H.wrapCallback ( itemStore.getItems )( null, 'annotations', { uri: annotation_uri } ) | |
.flatMap ( annotations => { | |
if ( annotations.length === 0 ) { | |
return H ( [] ); | |
} | |
const annotation = R.head ( annotations ); | |
const adjustedConfidence = annotation.item.custom_score || confidence | |
const exclusionAnnotations = annotation.exclusion_annotation_uris || []; | |
const exclusionPublications = annotation.item.exclusion_publication_uris || []; | |
const inclusionPublications = annotation.item.inclusion_publication_uris || []; | |
const placeConceptFilter = R.curry ( ( title, placeConcept ) => R.contains ( placeConcept.annotation_uri_hash, annotationUris ) && | |
placeConcept.confidence >= adjustedConfidence && | |
filterBySpotContext ( title, placeConcept ) ); | |
return itemGenerator ( 'dandelion-news-items', R.merge ( R.omit ( [ | |
'annotation_uri', | |
'confidence' | |
], req.query ), { | |
annotation_uri: annotation_uri, | |
count: apiCount | |
} ), undefined, undefined, undefined, utils ) | |
.filter ( newsItem => { | |
const checkAllInclusionPublications = inclusionPublication => { | |
return newsItem.item.url.match ( inclusionPublication ); | |
}; | |
if ( R.isEmpty ( inclusionPublications ) ) { | |
const checkAllExclusionPublications = exclusionPublication => { | |
return newsItem.item.url.match ( exclusionPublication ); | |
}; | |
return R.isNil ( R.find ( checkAllExclusionPublications, exclusionPublications ) ); | |
} | |
return R.not ( R.isNil ( R.find ( checkAllInclusionPublications, inclusionPublications ) ) ); | |
} ) | |
.map ( newsItem => { | |
const checkAllAnnotations = annotation => { return R.contains ( annotation, exclusionAnnotations ); }; | |
const hasNoExclusionAnnotation = R.isNil ( R.find ( checkAllAnnotations, newsItem.annotation_uri ) ); | |
const placeConcepts = hasNoExclusionAnnotation ? | |
R.filter ( placeConceptFilter ( newsItem.item.title ), newsItem.item.place_concepts || [] ) : []; | |
return R.assocPath ( [ 'item', 'place_concepts' ], placeConcepts, newsItem ); | |
} ) | |
.reject ( newsItem => R.isEmpty ( newsItem.item['place_concepts'] ) ) | |
.take ( count ); | |
} ); | |
}, annotationUris ) ) | |
.parallel ( annotationUris.length ) | |
.scan ( { itemIds: [], itemTitles: [], itemUrls: [], itemSnippets: [], item: null }, ( memo, item ) => ( { | |
itemIds: R.concat ( memo.itemIds, [ item.id ] ), | |
itemTitles: R.concat ( memo.itemTitles, [ item.item.title ] ), | |
itemUrls: R.concat ( memo.itemUrls, [ item.item.url ] ), | |
itemSnippets: R.concat ( memo.itemSnippets, R.map ( R.prop ( 'snippet' ), item.item.place_concepts ) ), | |
item: R.contains ( item.id, memo.itemIds ) || | |
R.contains ( item.item.title, memo.itemTitles ) || | |
R.contains ( item.item.url, memo.itemUrls ) || | |
R.find ( snippet => R.contains ( snippet, memo.itemSnippets ), R.map ( R.prop ( 'snippet' ), item.item.place_concepts ) ) | |
? null : item | |
} ) ) | |
.pluck ( 'item' ) | |
.reject ( R.isNil ) | |
.collect () | |
.map ( R.compose ( R.reverse, R.sortBy ( R.prop ( 'lastModifiedTime' ) ) ) ) | |
.toCallback ( utils.callback ( res ) ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment