-
Notifications
You must be signed in to change notification settings - Fork 274
support CJK string annotation; print readably CJK string in scrapely.tool's output #45
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,19 @@ | |
| from scrapely.template import TemplateMaker, best_match | ||
| from scrapely.extraction import InstanceBasedLearningExtractor | ||
|
|
||
| REPR_UNICODE_CHAR = re.compile(r'(?<!\\)(\\u[0-9a-f]{4,4})') | ||
|
|
||
|
|
||
| def readable_repr(obj): | ||
| '''Return printing-friendly unicode repr string | ||
| ''' | ||
| def replace_unicode_char(repr_char): | ||
| return unichr(int(str(repr_char.group())[2:], base=16)) | ||
|
|
||
| repr_string = repr(obj) | ||
| return REPR_UNICODE_CHAR.sub(replace_unicode_char, repr_string) | ||
|
Member
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. Is it different from
Member
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. what is repr_bytesting?
Member
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. it is repr_string
Author
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. @kmike, it's different. The
Member
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. Hey @xyb, Thanks for the fix and an explanation. This approach makes sense; it is basically undoing what Python 2.x repr is doing for unicode strings (if we don't want to print new lines, etc.) A couple of notes:
The best fix for this issue would be to port scrapely to Python 3 - it doesn't escape non-ascii letters and symbols in repr of unicode strings, but w3lib must be ported before that :) |
||
|
|
||
|
|
||
| class IblTool(cmd.Cmd): | ||
|
|
||
| prompt = 'scrapely> ' | ||
|
|
@@ -17,6 +30,8 @@ def __init__(self, filename, **kw): | |
| def do_ta(self, line): | ||
| """ta <url> [--encoding ENCODING] - add template""" | ||
| opts, (url,) = parse_at(line) | ||
| if assert_or_print(url, "missing url"): | ||
| return | ||
| t = url_to_page(url, opts.encoding) | ||
| templates = self._load_templates() | ||
| templates.append(t) | ||
|
|
@@ -31,7 +46,11 @@ def do_tl(self, line): | |
|
|
||
| def do_td(self, template_id): | ||
| """dt <template> - delete template""" | ||
| if assert_or_print(template_id, "missing template id"): | ||
| return | ||
| templates = self._load_templates() | ||
| if assert_or_print(teamplates, "no templates available"): | ||
| return | ||
| try: | ||
| del templates[int(template_id)] | ||
| self._save_templates(templates) | ||
|
|
@@ -41,22 +60,35 @@ def do_td(self, template_id): | |
|
|
||
| def do_t(self, line): | ||
| """t <template> <text> - test selection text""" | ||
| if assert_or_print(line, "missing template id or selection text"): | ||
| return | ||
| if assert_or_print(' ' in line, "missing template id or selection text"): | ||
| return | ||
| template_id, criteria = line.split(' ', 1) | ||
| t = self._load_template(template_id) | ||
| if assert_or_print(t, "template not found: %s" % template_id): | ||
| return | ||
| criteria = parse_criteria(criteria) | ||
| tm = TemplateMaker(t) | ||
| selection = apply_criteria(criteria, tm) | ||
| for n, i in enumerate(selection): | ||
| print "[%d] %r" % (n, remove_annotation(tm.selected_data(i))) | ||
| print "[%d] %s" % (n, | ||
| readable_repr(remove_annotation(tm.selected_data(i)))) | ||
|
|
||
| def do_a(self, line): | ||
| """a <template> <data> [-n number] [-f field]- add or test annotation | ||
|
|
||
| Add a new annotation (if -f is passed) or test what would be annotated | ||
| otherwise | ||
| """ | ||
| if assert_or_print(line, "missing template id and selection text"): | ||
| return | ||
| if assert_or_print(' ' in line, "missing template id or selection text"): | ||
| return | ||
| template_id, criteria = line.split(' ', 1) | ||
| t = self._load_template(template_id) | ||
| if assert_or_print(t, "template not found: %s" % template_id): | ||
| return | ||
| criteria = parse_criteria(criteria) | ||
| tm = TemplateMaker(t) | ||
| selection = apply_criteria(criteria, tm) | ||
|
|
@@ -65,27 +97,31 @@ def do_a(self, line): | |
| index = selection[0] | ||
| tm.annotate_fragment(index, criteria.field) | ||
| self._save_template(template_id, tm.get_template()) | ||
| print "[new] (%s) %r" % (criteria.field, | ||
| remove_annotation(tm.selected_data(index))) | ||
| print "[new] (%s) %s" % (criteria.field, | ||
| readable_repr(remove_annotation(tm.selected_data(index)))) | ||
| else: | ||
| for n, i in enumerate(selection): | ||
| print "[%d] %r" % (n, remove_annotation(tm.selected_data(i))) | ||
| print "[%d] %s" % (n, readable_repr(remove_annotation(tm.selected_data(i)))) | ||
|
|
||
| def do_al(self, template_id): | ||
| """al <template> - list annotations""" | ||
| if assert_or_print(template_id, "missing template id"): | ||
| return | ||
| t = self._load_template(template_id) | ||
| if assert_or_print(t, "template not found: %s" % template_id): | ||
| return | ||
| tm = TemplateMaker(t) | ||
| for n, (a, i) in enumerate(tm.annotations()): | ||
| print "[%s-%d] (%s) %r" % (template_id, n, a['annotations']['content'], | ||
| remove_annotation(tm.selected_data(i))) | ||
| print "[%s-%d] (%s) %s" % (template_id, n, a['annotations']['content'], | ||
| readable_repr(remove_annotation(tm.selected_data(i)))) | ||
|
|
||
| def do_s(self, url): | ||
| """s <url> - scrape url""" | ||
| templates = self._load_templates() | ||
| if assert_or_print(templates, "no templates available"): | ||
| return | ||
| if assert_or_print(url, "missing url"): | ||
| return | ||
| # fall back to the template encoding if none is specified | ||
| page = url_to_page(url, default_encoding=templates[0].encoding) | ||
| ex = InstanceBasedLearningExtractor((t, None) for t in templates) | ||
|
|
||
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.
Sorry, I haven't looked at your changes in detail, but why are you decoding from UTF-8 and not from some other encoding?
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 have the same question. Also, it's probably more appropiate to do this on
scrapely.tool, when callingbest_match, not here.