Generate a dictionary of all bib keys in the document (and input
        documents), with rich of metadata about the context of each
        citation in the document.

        For example, suppose ``'Sick:2014'`` is cited twice within a document.
        Then the dictionary returned by this method will have a length-2
        list under the ``'Sick:2014'`` key. Each item in this list
        will be a dictionary providing metadata of the context for that
        citation. Fields of this dictionary are:

        - ``position``: (int) the cumulative word count at which the
          citation occurs.
        - ``wordsbefore``: (unicode) text occuring before the citation.
        - ``wordsafter``: (unicode) text occuring after the citation.
        - ``section``: (unicode) name of the section in which the citation
          occurs.

        Parameters
        ----------
        n_words : int
            Number of words before and after the citation to extract for
            context.

        Returns
        -------
        bib_keys : dict
            Dictionary, keyed by BibTeX cite key, where entires are lists
            of instances of citations. See above for the format of the
            instance metadata.

def extract_citation_context(self, n_words=20):
        """Generate a dictionary of all bib keys in the document (and input
        documents), with rich of metadata about the context of each
        citation in the document.

        For example, suppose ``'Sick:2014'`` is cited twice within a document.
        Then the dictionary returned by this method will have a length-2
        list under the ``'Sick:2014'`` key. Each item in this list
        will be a dictionary providing metadata of the context for that
        citation. Fields of this dictionary are:

        - ``position``: (int) the cumulative word count at which the
          citation occurs.
        - ``wordsbefore``: (unicode) text occuring before the citation.
        - ``wordsafter``: (unicode) text occuring after the citation.
        - ``section``: (unicode) name of the section in which the citation
          occurs.

        Parameters
        ----------
        n_words : int
            Number of words before and after the citation to extract for
            context.

        Returns
        -------
        bib_keys : dict
            Dictionary, keyed by BibTeX cite key, where entires are lists
            of instances of citations. See above for the format of the
            instance metadata.
        """
        bib_keys = defaultdict(list)
        # Get bib keys in this document
        for match in texutils.cite_pattern.finditer(self.text):

            textbefore = self.text[0:match.start()]
            textafter = self.text[match.end():-1]

            wordsbefore = nlputils.wordify(textbefore)
            wordsafter = nlputils.wordify(textafter)
            numwordsbefore = len(wordsbefore)
            # numwordsafter = len(wordsafter)

            containing_section = None
            for (section_pos, section_name) in self._sections:
                if section_pos < numwordsbefore:
                    containing_section = (section_pos, section_name)

            citebody = match.groups()
            keys = (citebody[-1].replace(" ", "")).split(',')
            for key in keys:
                cite_instance = {
                    "position": numwordsbefore,
                    "wordsbefore": (" ".join(wordsbefore[-n_words:])),
                    "wordsafter": (" ".join(wordsafter[:n_words])),
                    "section": containing_section}
                bib_keys[key] += [cite_instance]

        # Recursion
        for path, document in self._children.iteritems():
            sub_bib_keys = document.rich_bib_keys(n_words=n_words)
            for k, cite_instances in sub_bib_keys.iteritems():
                bib_keys[k] += cite_instances

        return bib_keys