Skip to content
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

Add options for a bare text format & removing empty documents #316

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions wikiextractor/WikiExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,13 @@ def main():
metavar="n[KMG]")
groupO.add_argument("-c", "--compress", action="store_true",
help="compress output files using bzip")
groupO.add_argument("--json", action="store_true",
help="write output in json format instead of the default <doc> format")
groupOFormat = groupO.add_mutually_exclusive_group()
groupOFormat.add_argument("--json", action="store_true",
help="write output in json format instead of the default <doc> format")
groupOFormat.add_argument("--text", action="store_true",
help="write output in text format (body only, no title) instead of the default <doc> format")
groupO.add_argument("--discard_empty", action="store_true",
help="discard empty articles (such as redirects) rather than writing just the title")

groupP = parser.add_argument_group('Processing')
groupP.add_argument("--html", action="store_true",
Expand Down Expand Up @@ -584,6 +589,8 @@ def main():
if args.html:
Extractor.keepLinks = True
Extractor.to_json = args.json
Extractor.to_text = args.text
Extractor.discard_empty = args.discard_empty

try:
power = 'kmg'.find(args.bytes[-1].lower()) + 1
Expand All @@ -607,6 +614,13 @@ def main():
if args.debug:
logger.setLevel(logging.DEBUG)

if args.json:
logger.debug("Outputting to json format")
elif args.text:
logger.debug("Outputting to text format")
else:
logger.debug("Outputting to <doc> format")

input_file = args.input

if not Extractor.keepLinks:
Expand Down
7 changes: 6 additions & 1 deletion wikiextractor/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,9 @@ def extract(self, out, html_safe=True):
text = ''.join(self.page)
text = self.clean_text(text, html_safe=html_safe)

if self.to_json:
if self.discard_empty and not text:
pass
elif self.to_json:
json_data = {
'id': self.id,
'revid': self.revid,
Expand All @@ -985,6 +987,9 @@ def extract(self, out, html_safe=True):
out_str = json.dumps(json_data)
out.write(out_str)
out.write('\n')
elif self.to_text:
out.write('\n'.join(text))
out.write('\n\n\n')
else:
header = '<doc id="%s" url="%s" title="%s">\n' % (self.id, self.url, self.title)
# Separate header from text with a newline.
Expand Down