|
| 1 | +import concurrent.futures |
| 2 | +import wikipedia |
| 3 | + |
| 4 | + |
| 5 | +def get_pages(search_prompt, n_results=5): |
| 6 | + """ |
| 7 | + gets the wikipedia pages for the search prompt using the wikipedia api |
| 8 | + :param search_prompt: search prompt for the wikipedia search |
| 9 | + :param n_results: number of page titles that should be returned |
| 10 | + :return: page titles |
| 11 | + """ |
| 12 | + return wikipedia.search(search_prompt, results=n_results) |
| 13 | + |
| 14 | + |
| 15 | +def get_text_chunks(page_titles, chunk_length=512, verbose=False): |
| 16 | + """ |
| 17 | + gets the content of the wikipedia pages using multiple threads (API calls take time) and splits it into chunks |
| 18 | + :param page_titles: list of page titles for which the content should be extracted |
| 19 | + :param chunk_length: length of characters that a chunk should have |
| 20 | + :param verbose: whether to print the progress |
| 21 | + :return: list of wiki text chunks |
| 22 | + """ |
| 23 | + wiki_chunks = [] |
| 24 | + with concurrent.futures.ThreadPoolExecutor() as executor: |
| 25 | + future_to_page = {executor.submit(get_page_content, page_title): page_title for page_title in page_titles} |
| 26 | + for future in concurrent.futures.as_completed(future_to_page): |
| 27 | + page_title = future_to_page[future] |
| 28 | + try: |
| 29 | + wiki_content = future.result() |
| 30 | + wiki_content = preprocess_and_chunk_wiki_content(wiki_content, chunk_length=chunk_length) |
| 31 | + if verbose: |
| 32 | + print(f"getting content of page {page_title}") |
| 33 | + wiki_chunks.extend(wiki_content) |
| 34 | + except wikipedia.exceptions.PageError or wikipedia.exceptions.DisambiguationError as e: |
| 35 | + if verbose: |
| 36 | + print(f"page {page_title} not found, {e}") |
| 37 | + continue # skip the page if it is not available |
| 38 | + return wiki_chunks |
| 39 | + |
| 40 | + |
| 41 | +def get_page_content(page_title): |
| 42 | + """ |
| 43 | + gets the content of the wikipedia page using the wikipedia api |
| 44 | + :param page_title: page_title of the wikipedia page from which the content should be extracted |
| 45 | + :return: content of the wikipedia page |
| 46 | + """ |
| 47 | + return wikipedia.page(page_title).content |
| 48 | + |
| 49 | + |
| 50 | +def preprocess_and_chunk_wiki_content(wiki_content, chunk_length=512): |
| 51 | + """ |
| 52 | + preprocesses the wiki content: |
| 53 | + - splits the content into paragraphs |
| 54 | + - removes headings and empty lines |
| 55 | + - splits too long paragraphs into smaller chunks without cutting sentences |
| 56 | + :param wiki_content: content of the wikipedia page |
| 57 | + :param chunk_length: length of characters that a chunk should have |
| 58 | + :return: list of wiki text chunks |
| 59 | + """ |
| 60 | + # remove everything from the references section onwards |
| 61 | + wiki_content = wiki_content.split("== References ==")[0] |
| 62 | + # split into paragraphs |
| 63 | + chunks = wiki_content.split("\n") |
| 64 | + # remove headings, empty chunks and too short chunks |
| 65 | + chunks = [chunk for chunk in chunks if not ((chunk.startswith("=") and chunk.endswith("=")) or len(chunk) < 50)] |
| 66 | + # split too long chunks |
| 67 | + additional_chunks = [] |
| 68 | + for i, chunk in enumerate(chunks): |
| 69 | + if len(chunk) > chunk_length: |
| 70 | + # split into sentences |
| 71 | + sentences = chunk.split(". ") |
| 72 | + # split into sub-chunks without cutting sentences |
| 73 | + sub_chunks = [""] |
| 74 | + sub_chunk_index = 0 |
| 75 | + for sentence in sentences: |
| 76 | + if len(sentence) > chunk_length: |
| 77 | + # cut the sentence if it's longer than the chunk_length (this should happen rarely) |
| 78 | + sentence = sentence[:chunk_length] |
| 79 | + # if the sentence fits into the current sub-chunk |
| 80 | + if len(sub_chunks[sub_chunk_index]) + len(sentence) < chunk_length: |
| 81 | + sub_chunks[sub_chunk_index] += sentence + ". " |
| 82 | + else: |
| 83 | + sub_chunk_index += 1 |
| 84 | + sub_chunks.append(sentence + ". ") |
| 85 | + # replace original chunk with first sub-chunk |
| 86 | + chunks[i] = sub_chunks[0] |
| 87 | + # add the other sub-chunks to the list |
| 88 | + for j in range(1, len(sub_chunks)): |
| 89 | + additional_chunks.append(sub_chunks[j]) |
| 90 | + chunks.extend(additional_chunks) |
| 91 | + return chunks |
0 commit comments