-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix segment-wiki script #1694
Merged
Merged
Fix segment-wiki script #1694
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
68df069
Fix script docstring (format description), remove pruning through t…
menshikh-iv 5ea0700
Add link to fresh en wiki
menshikh-iv 357f8d2
Add link to fresh en wiki, examples section, filter redirect
menshikh-iv 0749351
strip -> lstrip
menshikh-iv 849dfc9
Added time for processing & ignore empty articles
menshikh-iv d4f81a4
extend filtering section
menshikh-iv a691623
reduce threshold
menshikh-iv 3eb057a
parametrize minimal article length, reduce to 200
menshikh-iv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,29 @@ | |
'section_titles' (list) - list of titles of sections, | ||
'section_texts' (list) - list of content from sections. | ||
|
||
English Wikipedia dump available | ||
`here <https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-pages-articles.xml.bz2>`_. Approximate time | ||
for processing is 2.5 hours (i7-6700HQ, SSD). | ||
|
||
Examples | ||
-------- | ||
|
||
Convert wiki to json-lines format: | ||
`python -m gensim.scripts.segment_wiki -f enwiki-latest-pages-articles.xml.bz2 | gzip > enwiki-latest.json.gz` | ||
|
||
Read json-lines dump | ||
|
||
>>> # iterate over the plain text file we just created | ||
>>> for line in smart_open('enwiki-latest.json.gz'): | ||
>>> # decode JSON into a Python object | ||
>>> article = json.loads(line) | ||
>>> | ||
>>> # each article has a "title", "section_titles" and "section_texts" fields | ||
>>> print("Article title: %s" % article['title']) | ||
>>> for section_title, section_text in zip(article['section_titles'], article['section_texts']): | ||
>>> print("Section title: %s" % section_title) | ||
>>> print("Section text: %s" % section_text) | ||
|
||
""" | ||
|
||
import argparse | ||
|
@@ -226,7 +249,9 @@ def get_texts_with_sections(self): | |
for group in utils.chunkize(page_xmls, chunksize=10 * self.processes, maxsize=1): | ||
for article_title, sections in pool.imap(segment, group): # chunksize=10): | ||
# article redirects are pruned here | ||
if any(article_title.startswith(ignore + ':') for ignore in IGNORED_NAMESPACES): | ||
if any(article_title.startswith(ignore + ':') for ignore in IGNORED_NAMESPACES) \ | ||
or len(sections) == 0 \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
or sections[0][1].lstrip().startswith("#REDIRECT"): | ||
continue | ||
articles += 1 | ||
yield (article_title, sections) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to include a concrete hands-on example, something like this:
Process a raw Wikipedia dump (XML.bz2 format, for example https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-pages-articles.xml.bz2 for the English Wikipedia) and extract all articles and their sections as plain text::
The output format of the parsed plain text Wikipedia is json-lines = one article per line, serialized into JSON. Here's an example how to work with it from Python::