Exemple #1
0
 def process_document(self, document: Document,
                      params: Dict[str, Any]) -> Optional[Dict[str, Any]]:
     tested = document.get_label_index(self.tested)
     if self.tested_filter is not None:
         tested = tested.filter(self.tested_filter)
     target = document.get_label_index(self.target)
     if self.target_filter is not None:
         target = target.filter(self.target_filter)
     local = {}
     for metric in self.metrics:
         local[metric.name] = metric.update(document, tested, target)
     return local
Exemple #2
0
def document_to_dict(document: Document,
                     *,
                     include_label_text: bool = False) -> Dict:
    """A helper method that turns a document into a python dictionary.

    Args:
        document (Document): The document object.

    Keyword Args:
        include_label_text (bool): Whether to include the text labels cover with the labels.

    Returns:
        dict: A dictionary object suitable for serialization.
    """
    d = {'text': document.text, 'label_indices': {}}

    for index_info in document.get_label_indices_info():
        if index_info.type == LabelIndexType.OTHER or index_info.type == LabelIndexType.UNKNOWN:
            logger.warning(
                'Index {} of type {} will not be included in serialization.'.
                format(index_info.index_name, index_info.type.name))
            continue
        d['label_indices'][index_info.index_name] = label_index_to_dict(
            document.get_label_index(index_info.index_name),
            include_label_text=include_label_text)
    return d