class_docstr: Implements top-p (nucleus) sampling for document filtering based on cumulative probability scores.
  
  This class provides functionality to filter a list of documents by selecting those whose scores fall
  within the top 'p' percent of the cumulative distribution. The method is useful for focusing on high-probability
  documents while filtering out less relevant ones based on their assigned scores.
  
  Usage example:
  
  ```python
  from haystack import Document
  from haystack.components.samplers import TopPSampler
  
  sampler = TopPSampler(top_p=0.95, score_field="similarity_score")
  docs = [
      Document(text="Berlin", meta={"similarity_score": -10.6}),
      Document(text="Belgrade", meta={"similarity_score": -8.9}),
      Document(text="Sarajevo", meta={"similarity_score": -4.6}),
  ]
  output = sampler.run(documents=docs)
  docs = output["documents"]
  assert len(docs) == 1
  assert docs[0].content == "Sarajevo"
  ```
docstr: Collect the scores from the documents' metadata.
  :param documents: List of Documents.
  :return: List of scores.
docstr_tok: ["Collect","the","scores","from","the","documents","'","metadata",".",":","param","documents",":","List","of","Documents",".",":","return",":","List","of","scores","."]
code: def _collect_scores(self, documents: List[Document]) -> List[float]:
      """
      Collect the scores from the documents' metadata.
      :param documents: List of Documents.
      :return: List of scores.
      """
      if self.score_field:
          missing_scores_docs = [
              d for d in documents if self.score_field not in d.meta
          ]
          if missing_scores_docs:
              missing_scores_docs_ids = [
                  d.id for d in missing_scores_docs if d.id
              ]
              raise ComponentError(
                  f"Score field '{self.score_field}' not found in metadata of documents "
                  f"with IDs: {missing_scores_docs_ids}."
                  f"Make sure that all documents have a score field '{self.score_field}' in their metadata."
              )
          return [d.meta[self.score_field] for d in documents]
      else:
          missing_scores_docs = [
              d for d in documents if d.score is None
          ]
          if missing_scores_docs:
              missing_scores_docs_ids = [
                  d.id for d in missing_scores_docs if d.id
              ]
              raise ComponentError(
                  f"Ensure all documents have a valid score value. These docs  {missing_scores_docs_ids} don't."
              )
          return [d.score for d in documents]
  
imports: import logging
  from typing import List, Optional
  from haystack import ComponentError, Document, component
  from haystack.lazy_imports import LazyImport
label_desc: Use image node_id 3 for calling the TopPSampler obj's underlying member method code with example usage: obj._collect_scores(documents) and returns: unknown, unknown