/*findPubMedArticle('Lexical patterns, features and knowledge resources for coreference resolution in clinical notes.', function(err, res) {
console.log(err, res);
});
426199
function findPubMedArticle(title, callback) {
  var uri = 'http://www.ncbi.nlm.nih.gov/pubmed/?term=' + title.replace(/ /g, '+');
  utils.retrieve(uri, function(err, contents) {
     var $ = cheerio.load(contents);
     var absid = $('#absid');
     if (absid) {
       var id = $(absid).val();
       callback(null, id, 'http://www.ncbi.nlm.nih.gov/pubmed/' + id);
     } else {
       callback(null, null);
     }

  });
}
790800
function ( source ) {

		var comment = false;
		var chapters = [];
		var chapter = null;
		var sections = [];
		var section = null;
		var marker = new Showdown.converter ();

		function nextchapter ( title ) {
			if ( chapter ) {
				chapter.sections = sections.map ( function ( section, i ) {
					section.desc = marker.makeHtml ( section.desc );
					return section;
				});
				chapters.push ( chapter );
			}
			chapter = new dox.Chapter ({
				title : title || null
			});
			sections = [];
		}

		function nextsection ( line ) {
			if ( section && section.code ) {
				sections.push ( section );
			}
			section = new dox.Block ({
				line : line || 0,
				tags : [],
				desc : "",
				code : ""
			});
		}

		nextsection ();
		nextchapter ();
		source.split ( "\n" ).forEach ( function ( line, index ) {
			var md, trim = line.trim ();
			if ( comment ) {
				if ( trim.startsWith ( "*/" )) {
					comment = false;
				} else {
					if ( trim === "*" ) {
						section.desc += "\n\n";
					} else {
						md = line.replace ( this._GUTTER, "" );
						switch ( md [ 0 ]) {
							case "@" :
								section.tags.push ( new dox.Tag ({
									text : this._encode ( md )
								}));
								break;
							case "#" :
								//chapter.title = md.replace ( this._HEADER, "" );
								section.desc += "\n" + md + "\n\n";
								break;
							default :
								if ( !md.match ( this._LETTER )) {
									section.desc += "\n";
								}
								section.desc += this._encode ( md );
								break;
						}
					}
				}
			} else {
				if ( line.match ( this._CHAPTER )) {
					var title = this._TITLE.exec ( line );
					nextsection ( index );
					nextchapter ( title ? title [ 0 ] : null );
					section.code += this._HELLO.exec ( line );
				} else {
					if ( trim.startsWith ( "/**" )) {
						comment = true;
						nextsection ( index );
					} else {
						section.code += line + "\n";
					}
				}
			}
		}, this );
		nextsection ();
		nextchapter ();
		return chapters;
	}
1072934
function footnote_tail(state) {
    var i, l, j, t, lastParagraph, list, token, tokens, current, currentLabel,
        insideRef = false,
        refTokens = {};

    if (!state.env.footnotes) { return; }

    state.tokens = state.tokens.filter(function (tok) {
      if (tok.type === 'footnote_reference_open') {
        insideRef = true;
        current = [];
        currentLabel = tok.meta.label;
        return false;
      }
      if (tok.type === 'footnote_reference_close') {
        insideRef = false;
        // prepend ':' to avoid conflict with Object.prototype members
        refTokens[':' + currentLabel] = current;
        return false;
      }
      if (insideRef) { current.push(tok); }
      return !insideRef;
    });

    if (!state.env.footnotes.list) { return; }
    list = state.env.footnotes.list;

    token = new state.Token('footnote_block_open', '', 1);
    state.tokens.push(token);

    for (i = 0, l = list.length; i < l; i++) {
      token      = new state.Token('footnote_open', '', 1);
      token.meta = { id: i, label: list[i].label };
      state.tokens.push(token);

      if (list[i].tokens) {
        tokens = [];

        token          = new state.Token('paragraph_open', 'p', 1);
        token.block    = true;
        tokens.push(token);

        token          = new state.Token('inline', '', 0);
        token.children = list[i].tokens;
        token.content  = '';
        tokens.push(token);

        token          = new state.Token('paragraph_close', 'p', -1);
        token.block    = true;
        tokens.push(token);

      } else if (list[i].label) {
        tokens = refTokens[':' + list[i].label];
      }

      state.tokens = state.tokens.concat(tokens);
      if (state.tokens[state.tokens.length - 1].type === 'paragraph_close') {
        lastParagraph = state.tokens.pop();
      } else {
        lastParagraph = null;
      }

      t = list[i].count > 0 ? list[i].count : 1;
      for (j = 0; j < t; j++) {
        token      = new state.Token('footnote_anchor', '', 0);
        token.meta = { id: i, subId: j, label: list[i].label };
        state.tokens.push(token);
      }

      if (lastParagraph) {
        state.tokens.push(lastParagraph);
      }

      token = new state.Token('footnote_close', '', -1);
      state.tokens.push(token);
    }

    token = new state.Token('footnote_block_close', '', -1);
    state.tokens.push(token);
  }
1370169
def _resolve_features(self):
        """"""

        for parentid in self.featsbyparent:
            parent = self.featsbyid[parentid]
            for child in self.featsbyparent[parentid]:
                parent.add_child(child, rangecheck=self.strict)

        # Replace top-level multi-feature reps with a pseudo-feature
        for n, record in enumerate(self.records):
            if not isinstance(record, Feature):
                continue
            if not record.is_multi:
                continue
            assert record.multi_rep == record
            newrep = sorted(record.siblings + [record])[0]
            if newrep != record:
                for sib in sorted(record.siblings + [record]):
                    sib.multi_rep = newrep
                    if sib != newrep:
                        newrep.add_sibling(sib)
                record.siblings = None
            parent = newrep.pseudoify()
            self.records[n] = parent

        if not self.assumesorted:
            for seqid in self.inferred_regions:
                if seqid not in self.declared_regions:
                    seqrange = self.inferred_regions[seqid]
                    srstring = '##sequence-region {:s} {:d} {:d}'.format(
                        seqid, seqrange.start + 1, seqrange.end
                    )
                    seqregion = Directive(srstring)
                    self.records.append(seqregion)

        for record in sorted(self.records):
            yield record
        self._reset()
321769
function determineFreqCategories() {
    if (mean - sd < 0) {
      var lowrange = 0 + mean / 3;
    } else {
      var lowrange = mean - sd;
    }
    var midrange = [mean + sd, lowrange];
    var highrange = mean + sd;

    var i = 0;
    for (; i < potentialexamples.length; i++) {
      var word = potentialexamples[i][0];

      if ('undefined' != typeof wordfreq[word.simplified]) {
        pushFrequency(word);
      }
    }

    function pushFrequency(word) {
      if (wordfreq[word.simplified] < lowrange) {
        lowfreq.push(word);
      }
      if (
        wordfreq[word.simplified] >= midrange[1] &&
        wordfreq[word.simplified] < midrange[0]
      ) {
        midfreq.push(word);
      }
      if (wordfreq[word.simplified] >= highrange) {
        highfreq.push(word);
      }
    }
  }
748626
function getArticle (candidates, $) {
  let topCandidate = null

  candidates.forEach(elem => {
    const linkDensity = getLinkDensity(elem, $)
    const siblings = elem.children('p').length
    const score = elem.data('readabilityScore')
    elem.data(
      'readabilityScore',
      Math.min(2, Math.max(siblings, 1)) * score * (1 - linkDensity)
    )
    if (
      !topCandidate ||
      elem.data('readabilityScore') > topCandidate.data('readabilityScore')
    ) {
      topCandidate = elem
    }
  })
  /**
   * If we still have no top candidate, just use the body as a last resort.
   * Should not happen.
   * */
  if (topCandidate === null) {
    return $('body')
  }

  // Perhaps the topCandidate is the parent?
  let parent
  if (
    !(parent = topCandidate.parent()) ||
    parent.length === 0 ||
    topCandidate.children('p').length > 5
  ) {
    return filterCandidates(topCandidate, topCandidate.children(), $)
  }
  return filterCandidates(topCandidate, parent.children(), $)
}
46160
private String findCommaSeparatedArgument(String functionBody, int index)
    {
        int start = 0;
        int found = -1;
        int brackets = 0;
        int pos = 0;
        int length = functionBody.length();

        while (found < index && pos < length)
        {
            char ch = functionBody.charAt(pos);
            switch (ch)
            {
            case ',':
                if (brackets == 0)
                {
                    found++;

                    if (found < index)
                    {
                        start = pos + 1;
                    }
                }
                break;
            case '(':
            case '[':
            case '{':
                brackets++;
                break;
            case ')':
            case ']':
            case '}':
                brackets--;
                break;
            }

            pos++;
        }

        if (found == index)
        {
            return functionBody.substring(start, pos - 1);
        }
        else if (pos == length)
        {
            return functionBody.substring(start);
        }
        else
        {
            return "";
        }
    }
529775
function _parserPhantomJS(error){
		// start parse the error stack string
		var lines	= error.stack.split("\n").slice(1);
		var stacktrace	= [];
		lines.forEach(function(line){
			if( line.match(/\(native\)$/) ){
				var matches	= line.match(/^\s*at (.+) \(native\)/);
				stacktrace.push(new Stacktrace.Frame({
					fct	: matches[1],
					url	: 'native',
					line	: 1,
					column	: 1
				}));
			}else if( line.match(/\)$/) ){
				var matches	= line.match(/^\s*at (.+) \((.+):(\d+)\)/);
				stacktrace.push(new Stacktrace.Frame({
					fct	: matches[1],
					url	: matches[2],
					line	: parseInt(matches[3], 10),
					column	: 1
				}));
			}else{
				var matches	= line.match(/^\s*at (.+):(\d+)/);
				stacktrace.push(new Stacktrace.Frame({
					url	: matches[1],
					fct	: '<anonymous>',
					line	: parseInt(matches[2], 10),
					column	: 1
				}));
			}
		});
		return stacktrace;
	}
911538
function footnoteTokensToMeta(tokens, md) {
  return tokens
    .map((token, i) => { return token.type === 'footnote_open' ? i : -1; })
    .filter(i => i >= 0)
    .map((i) => {
      const inlineToken = tokens[i + 2];
      assert(inlineToken.type === 'inline');
      const anchorToken = tokens[i + 3];
      assert(anchorToken.type === 'footnote_anchor');

      const content = md.renderer.render([inlineToken], md.options, {});
      const id = anchorToken.meta.id + 1;
      const meta = Object.assign({}, anchorToken.meta, { id });
      return Object.assign({}, { content }, meta);
    });
}
587940
function mapItemScheme (e) {
      var itemName = {
        'CategoryScheme': 'Category',
        'Hierarchy': 'HierarchicalCode',
        'Codelist': 'Code',
        'ConceptScheme': 'Concept',
        'AgencyScheme': 'Agency'
      }[e.localName];

      var itemScheme = mapMaintainableArtefact(e);
      itemScheme.isPartial = toBoolean(getAttrValue(e, 'isPartial'));
    itemScheme.leveled = toBoolean(getAttrValue(e, 'leveled')); // Hierarchy

    var itemURN = [
    'urn:sdmx:org.sdmx.infomodel',
    '.', itemScheme.package,
    '', itemName,
    '=', itemScheme.agencyID,
    ':', itemScheme.id,
    '(', itemScheme.version, ')'
    ].join('');

    function mapItems (node, hierarchy) {
      var items = _getChildrenNS(node, itemName);
      if (items.length === 0) return undefined;

      function mapItem (e) {
        var item = mapVersionableArtefact(e); // HierarchicalCode = versionable
        item.agencyID = itemScheme.agencyID;
        item.maintainableParentID = itemScheme.id;
        item.maintainableParentVersion = itemScheme.version;

        if (item.urn === undefined) {
          item.urn = itemURN + hierarchy + '.' + item.id;
        }

        item.parent = mapLocalReference(_getFirstChildNS(e, 'parent'));
        item.items = mapItems(e, hierarchy + '.' + item.id);

        if (e.localName === 'Concept') {
          item.isoConceptReference = mapISOConceptReference(_getFirstChildNS(e, 'ISOConceptReference'));
          item.coreRepresentation = mapRepresentation(_getFirstChildNS(e, 'CoreRepresentation'));
        }

        if (e.localName === 'HierarchicalCode') {
          item.code = mapReference(_getFirstChildNS(e, 'Code'), 'code', 'code');
        }

        return item;
      }

      return items.map(mapItem);
    }

    itemScheme.items = mapItems(e, '');

    return itemScheme;
  }
559068
function processCompositionExpressions( composition, item, itemPointer, containingPageRef ) {

      const { name, definition } = composition;

      // Feature definitions in compositions may contain generated topics for default resource names or action
      // topics. As such these are generated before instantiating the composition's features.
      const ref = item.composition;
      const validate = validators.features.pages[ name ];

      if( validate && !validate( item, `${itemPointer}` ) ) {
         throw validators.error(
            `Validation of page ${containingPageRef} failed for ${ref} features`,
            validate.errors
         );
      }

      if( typeof definition.mergedFeatures === 'object' ) {
         const mergedFeatures = interpolator.interpolate( item, definition.mergedFeatures );
         Object.keys( mergedFeatures ).forEach( featurePath => {
            const currentValue = path( item.features, featurePath, [] );
            const values = mergedFeatures[ featurePath ];
            setPath( item.features, featurePath, values.concat( currentValue ) );
         } );
      }

      definition.areas = interpolator.interpolate( item, definition.areas );

      return composition;

   }
235530
function findPhrase(match) {
  var node = match.node
  var prev = findPhraseInDirection(node, match.index, match.parent, -1)
  var next = findPhraseInDirection(node, match.index, match.parent, 1)
  var stems = merge(prev.stems, stemNode(node), next.stems)

  return {
    stems: stems,
    value: stems.join(' '),
    nodes: merge(prev.nodes, node, next.nodes)
  }
}
417672
function resolveSubPatterns (pattern) {
        if(!pattern) { return; }

        var expression  = pattern.expression;
        var subPatterns = expression.match(subPatternsRegex) || [];

        subPatterns.forEach(function (matched) {
            // matched is: %{subPatternName} or %{subPatternName:fieldName}
            var subPatternName = matched.substr(2, matched.length - 3);

            var elements   = subPatternName.split(':');
            subPatternName = elements[0];
            var fieldName  = elements[1];

            var subPattern = patterns.get(subPatternName);
            if (!subPattern) {
                console.error('Error: pattern "' + subPatternName + '" not found!');
                return;
            }

            if (!subPattern.resolved) {
                resolvePattern(subPattern);
            }

            if (fieldName) {
                expression = expression.replace(matched, '(?<' + fieldName + '>' + subPattern.resolved + ')');
            } else {
                expression = expression.replace(matched, subPattern.resolved);
            }
        });

        pattern.resolved = expression;
        return pattern;
    }
522895
function morphyPromise( str, pos ) {
	/* jshint: -WO40 */
	var substitutions;
	var query;
	var i;
	if ( !pos ) {
		var resArray = [];
		for ( i = 0; i < POS_TAGS.length; i++ ){
			resArray.push( morphyPromise( str, POS_TAGS[i] ) );
		}
		return Promise.all( resArray ).then( function onDone( data ) {
			var reducedArray = [];
			var j;
			for ( j = 0; j < data.length; j++ ) {
				reducedArray.push( data[ j ] );
			}
			return _.flatten( reducedArray );
		});
	}

	substitutions = getSubstitutions( pos );

	if ( !this.DICTIONARY ) {
		query = 'SELECT DISTINCT ws.lemma AS lemma, syn.pos AS pos FROM words AS ws LEFT JOIN senses AS sen ON ws.wordid = sen.wordid LEFT JOIN ';
		query += 'synsets AS syn ON sen.synsetid = syn.synsetid';
		this.DICTIONARY = this.db.allAsync( query ).map( function mapper( elem ) {
			var obj = {
				lemma: elem.lemma,
				pos: elem.pos
			};
			return obj;
		});
	}

	if ( !this.EXCEPTIONS ) {
		query = 'SELECT lemma, morph, pos FROM morphology';
		this.EXCEPTIONS = db.allAsync( query );
	}

	var wordsPromise = Promise.join( this.DICTIONARY, this.EXCEPTIONS, function( dictionary, exceptions ) {
		var foundExceptions = [];
		var exceptionMorphs = exceptions.map( function( elem ) {
			return elem.morph;
		});
		var baseWord;
		var suffix;
		var index = exceptionMorphs.indexOf( str );

		while ( index !== -1 ) {
			if ( exceptions[index].pos === pos ) {
				baseWord = new this.Word( exceptions[index].lemma );
				baseWord.part_of_speech = pos;
				foundExceptions.push( baseWord );
			}
			index = exceptionMorphs.indexOf( str, index + 1 );
		}

		if ( foundExceptions.length > 0 ) {
			return foundExceptions;
		} else {
			if ( pos === 'n' && str.endsWith( 'ful' ) ) {
				suffix = 'ful';
				str = str.slice( 0, str.length - suffix.length );
			} else {
				suffix = '';
			}
			return rulesOfDetachment( str, substitutions );
		}
	});

	return wordsPromise;
}
116432
private IContext findMultiwordsInContextStructure(IContext context) throws ContextPreprocessorException {
        for (Iterator<INode> i = context.getNodes(); i.hasNext(); ) {
            INode sourceNode = i.next();
            // sense disambiguation within the context structure
            // for all ACoLs in the source node
            for (Iterator<IAtomicConceptOfLabel> j = sourceNode.getNodeData().getACoLs(); j.hasNext(); ) {
                IAtomicConceptOfLabel synSource = j.next();
                // in all descendants and ancestors
                findMultiwordsAmong(sourceNode.getDescendants(), synSource);
                findMultiwordsAmong(sourceNode.getAncestors(), synSource);
            }
        }
        return context;
    }
1128042
function getExclusions(astNode) {
    var comments = parseASTComments(astNode),
        commandCodes = CommandRegistry.getAllCommandCodes(),
        params,
        exclusions = {};

    _.forEach(comments, function(comment) {
        if(comment.indexOf(EXCLUSION_KEYWORD) !== -1) {
            params = comment.match(/\[.*\]/g);
            if(params) {
                // Replace all single quotes with double quotes to be able to JSON parse them
                _.forEach(JSON.parse(params[0].replace(/'/g, '\"')), function(exclusion) {
                    if(commandCodes.indexOf(exclusion) !== -1) {
                        exclusions[exclusion] = true;
                    } else {
                        logger.warn('Encountered an unknown exclusion: %s', exclusion);
                    }
                });
            }

            // Exclude all mutations when none are specifically excluded
            if(_.keys(exclusions).length === 0){
                _.forEach(commandCodes, function(code) {
                    exclusions[code] = true;
                });
            }
        }
    });

    return exclusions;
}
316987
function (xrefs, server){
    this._is_a = 'amigo2.linker';

    // TODO:BUG: With the new dispatcher, relative URLs no longer work.

    this._xrefs = xrefs;

    // Easy app base.
    this.app_base = server.app_base;
    // Internal term matcher.
    this.term_regexp = null;
    var internal_regexp_str = server.term_regexp;
    if( internal_regexp_str ){
	this.term_regexp = new RegExp(internal_regexp_str);
    }

    // Categories for different special cases (internal links).
    this.ont_category = {
	'term': true,
	'ontology_class': true,
	'annotation_class': true,
	'annotation_class_closure': true,
	'annotation_class_list': true,
	// Noctua model stuff.
	'function_class': true,
	'function_class_closure': true,
	'process_class': true,
	'process_class_closure': true,
	'location_list': true,
	'location_list_closure': true
    };
    this.bio_category = {
        'gp': true,
	'gene_product': true,
	'bioentity': true
    };
    this.ref_category = {
	'reference': true
    };
    this.model_category = {
        'model': true
    };
    this.search_category = { // not including the trivial medial_search below
        'search': true,
	'live_search': true
    };
    this.search_modifier = {
	// Possibly "dynamic".
	'gene_product': '/bioentity',
	'bioentity': '/bioentity',
	'ontology': '/ontology',
	'reference': '/reference',
	'annotation': '/annotation',
	'model': '/model',
	'family': '/family',
	'lego_unit': '/lego_unit',
	'general': '/general'
    };
    this.other_interlinks = {
	'medial_search': '/amigo/medial_search',
	'landing': '/amigo/landing',
	'tools': '/amigo/software_list',
	'schema_details': '/amigo/schema_details',
	'load_details': '/amigo/load_details',
	'browse': '/amigo/browse',
	'goose': '/goose',
	'grebe': '/grebe',
	'gannet': '/gannet',
	'repl': '/repl'	
    };
}