mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-07-26 02:10:41 +00:00

* first draft / notes on new primitives * wip label / feedback refactor * rename doc.text -> doc.content. add doc.content_type * add datatype for content * remove faq_question_field from ES and weaviate. rename text_field -> content_field in docstores. update tutorials for content field * update converters for . Add warning for empty * renam label.question -> label.query. Allow sorting of Answers. * WIP primitives * update ui/reader for new Answer format * Improve Label. First refactoring of MultiLabel. Adjust eval code * fixed workflow conflict with introducing new one (#1472) * Add latest docstring and tutorial changes * make add_eval_data() work again * fix reader formats. WIP fix _extract_docs_and_labels_from_dict * fix test reader * Add latest docstring and tutorial changes * fix another test case for reader * fix mypy in farm reader.eval() * fix mypy in farm reader.eval() * WIP ORM refactor * Add latest docstring and tutorial changes * fix mypy weaviate * make label and multilabel dataclasses * bump mypy env in CI to python 3.8 * WIP refactor Label ORM * WIP refactor Label ORM * simplify tests for individual doc stores * WIP refactoring markers of tests * test alternative approach for tests with existing parametrization * WIP refactor ORMs * fix skip logic of already parametrized tests * fix weaviate behaviour in tests - not parametrizing it in our general test cases. * Add latest docstring and tutorial changes * fix some tests * remove sql from document_store_types * fix markers for generator and pipeline test * remove inmemory marker * remove unneeded elasticsearch markers * add dataclasses-json dependency. adjust ORM to just store JSON repr * ignore type as dataclasses_json seems to miss functionality here * update readme and contributing.md * update contributing * adjust example * fix duplicate doc handling for custom index * Add latest docstring and tutorial changes * fix some ORM issues. fix get_all_labels_aggregated. * update drop flags where get_all_labels_aggregated() was used before * Add latest docstring and tutorial changes * add to_json(). add + fix tests * fix no_answer handling in label / multilabel * fix duplicate docs in memory doc store. change primary key for sql doc table * fix mypy issues * fix mypy issues * haystack/retriever/base.py * fix test_write_document_meta[elastic] * fix test_elasticsearch_custom_fields * fix test_labels[elastic] * fix crawler * fix converter * fix docx converter * fix preprocessor * fix test_utils * fix tfidf retriever. fix selection of docstore in tests with multiple fixtures / parameterizations * Add latest docstring and tutorial changes * fix crawler test. fix ocrconverter attribute * fix test_elasticsearch_custom_query * fix generator pipeline * fix ocr converter * fix ragenerator * Add latest docstring and tutorial changes * fix test_load_and_save_yaml for elasticsearch * fixes for pipeline tests * fix faq pipeline * fix pipeline tests * Add latest docstring and tutorial changes * fix weaviate * Add latest docstring and tutorial changes * trigger CI * satisfy mypy * Add latest docstring and tutorial changes * satisfy mypy * Add latest docstring and tutorial changes * trigger CI * fix question generation test * fix ray. fix Q-generation * fix translator test * satisfy mypy * wip refactor feedback rest api * fix rest api feedback endpoint * fix doc classifier * remove relation of Labels -> Docs in SQL ORM * fix faiss/milvus tests * fix doc classifier test * fix eval test * fixing eval issues * Add latest docstring and tutorial changes * fix mypy * WIP replace dataclasses-json with manual serialization * Add latest docstring and tutorial changes * revert to dataclass-json serialization for now. remove debug prints. * update docstrings * fix extractor. fix Answer Span init * fix api test * keep meta data of answers in reader.run() * fix meta handling * adress review feedback * Add latest docstring and tutorial changes * make document=None for open domain labels * add import * fix print utils * fix rest api * adress review feedback * Add latest docstring and tutorial changes * fix mypy Co-authored-by: Markus Paff <markuspaff.mp@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
153 lines
6.1 KiB
Python
153 lines
6.1 KiB
Python
"""
|
|
Preprocessing
|
|
|
|
Haystack includes a suite of tools to extract text from different file types, normalize white space
|
|
and split text into smaller pieces to optimize retrieval.
|
|
These data preprocessing steps can have a big impact on the systems performance and effective handling of data is key to getting the most out of Haystack.
|
|
|
|
Ultimately, Haystack pipelines expect data to be provided as a list documents in the following dictionary format:
|
|
|
|
docs = [
|
|
{
|
|
'text': DOCUMENT_TEXT_HERE,
|
|
'meta': {'name': DOCUMENT_NAME, ...}
|
|
}, ...
|
|
]
|
|
|
|
This tutorial will show you all the tools that Haystack provides to help you cast your data into the right format.
|
|
"""
|
|
|
|
# Here are the imports we need
|
|
|
|
from haystack.file_converter.txt import TextConverter
|
|
from haystack.file_converter.pdf import PDFToTextConverter
|
|
from haystack.file_converter.docx import DocxToTextConverter
|
|
|
|
from haystack.preprocessor.utils import convert_files_to_dicts, fetch_archive_from_http
|
|
from haystack.preprocessor.preprocessor import PreProcessor
|
|
|
|
|
|
def tutorial8_preprocessing():
|
|
# This fetches some sample files to work with
|
|
|
|
doc_dir = "data/preprocessing_tutorial"
|
|
s3_url = "https://s3.eu-central-1.amazonaws.com/deepset.ai-farm-qa/datasets/documents/preprocessing_tutorial.zip"
|
|
fetch_archive_from_http(url=s3_url, output_dir=doc_dir)
|
|
|
|
"""
|
|
## Converters
|
|
|
|
Haystack's converter classes are designed to help you turn files on your computer into the documents
|
|
that can be processed by the Haystack pipeline.
|
|
There are file converters for txt, pdf, docx files as well as a converter that is powered by Apache Tika.
|
|
The parameter `valid_langugages` does not convert files to the target language, but checks if the conversion worked as expected.
|
|
For converting PDFs, try changing the encoding to UTF-8 if the conversion isn't great.
|
|
"""
|
|
|
|
# Here are some examples of how you would use file converters
|
|
|
|
converter = TextConverter(remove_numeric_tables=True, valid_languages=["en"])
|
|
doc_txt = converter.convert(file_path="data/preprocessing_tutorial/classics.txt", meta=None)
|
|
|
|
converter = PDFToTextConverter(remove_numeric_tables=True, valid_languages=["en"])
|
|
doc_pdf = converter.convert(file_path="data/preprocessing_tutorial/bert.pdf", meta=None)
|
|
|
|
converter = DocxToTextConverter(remove_numeric_tables=True, valid_languages=["en"])
|
|
doc_docx = converter.convert(file_path="data/preprocessing_tutorial/heavy_metal.docx", meta=None)
|
|
|
|
# Haystack also has a convenience function that will automatically apply the right converter to each file in a directory.
|
|
|
|
all_docs = convert_files_to_dicts(dir_path="data/preprocessing_tutorial")
|
|
|
|
"""
|
|
|
|
## PreProcessor
|
|
|
|
The PreProcessor class is designed to help you clean text and split text into sensible units.
|
|
File splitting can have a very significant impact on the system's performance.
|
|
Have a look at the [Preprocessing](https://haystack.deepset.ai/docs/latest/preprocessingmd)
|
|
and [Optimization](https://haystack.deepset.ai/docs/latest/optimizationmd) pages on our website for more details.
|
|
"""
|
|
|
|
|
|
# This is a default usage of the PreProcessor.
|
|
# Here, it performs cleaning of consecutive whitespaces
|
|
# and splits a single large document into smaller documents.
|
|
# Each document is up to 1000 words long and document breaks cannot fall in the middle of sentences
|
|
# Note how the single document passed into the document gets split into 5 smaller documents
|
|
|
|
preprocessor = PreProcessor(
|
|
clean_empty_lines=True,
|
|
clean_whitespace=True,
|
|
clean_header_footer=False,
|
|
split_by="word",
|
|
split_length=1000,
|
|
split_respect_sentence_boundary=True
|
|
)
|
|
docs_default = preprocessor.process(doc_txt)
|
|
print(f"n_docs_input: 1\nn_docs_output: {len(docs_default)}")
|
|
|
|
"""
|
|
## Cleaning
|
|
|
|
- `clean_empty_lines` will normalize 3 or more consecutive empty lines to be just a two empty lines
|
|
- `clean_whitespace` will remove any whitespace at the beginning or end of each line in the text
|
|
- `clean_header_footer` will remove any long header or footer texts that are repeated on each page
|
|
|
|
## Splitting
|
|
By default, the PreProcessor will respect sentence boundaries, meaning that documents will not start or end
|
|
midway through a sentence.
|
|
This will help reduce the possibility of answer phrases being split between two documents.
|
|
This feature can be turned off by setting `split_respect_sentence_boundary=False`.
|
|
"""
|
|
|
|
# Not respecting sentence boundary vs respecting sentence boundary
|
|
|
|
preprocessor_nrsb = PreProcessor(split_respect_sentence_boundary=False)
|
|
docs_nrsb = preprocessor_nrsb.process(doc_txt)
|
|
|
|
print("RESPECTING SENTENCE BOUNDARY")
|
|
end_text = docs_default[0]["content"][-50:]
|
|
print("End of document: \"..." + end_text + "\"")
|
|
print()
|
|
print("NOT RESPECTING SENTENCE BOUNDARY")
|
|
end_text_nrsb = docs_nrsb[0]["content"][-50:]
|
|
print("End of document: \"..." + end_text_nrsb + "\"")
|
|
|
|
"""
|
|
A commonly used strategy to split long documents, especially in the field of Question Answering,
|
|
is the sliding window approach. If `split_length=10` and `split_overlap=3`, your documents will look like this:
|
|
|
|
- doc1 = words[0:10]
|
|
- doc2 = words[7:17]
|
|
- doc3 = words[14:24]
|
|
- ...
|
|
|
|
You can use this strategy by following the code below.
|
|
"""
|
|
|
|
# Sliding window approach
|
|
|
|
preprocessor_sliding_window = PreProcessor(
|
|
split_overlap=3,
|
|
split_length=10,
|
|
split_respect_sentence_boundary=False
|
|
)
|
|
docs_sliding_window = preprocessor_sliding_window.process(doc_txt)
|
|
|
|
doc1 = docs_sliding_window[0]["content"][:200]
|
|
doc2 = docs_sliding_window[1]["content"][:100]
|
|
doc3 = docs_sliding_window[2]["content"][:100]
|
|
|
|
print("Document 1: \"" + doc1 + "...\"")
|
|
print("Document 2: \"" + doc2 + "...\"")
|
|
print("Document 3: \"" + doc3 + "...\"")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
tutorial8_preprocessing()
|
|
|
|
# This Haystack script was made with love by deepset in Berlin, Germany
|
|
# Haystack: https://github.com/deepset-ai/haystack
|
|
# deepset: https://deepset.ai/
|