-
-
Save Ademord/769a9516c9a1aaa9e6ab to your computer and use it in GitHub Desktop.
Revisions
-
Ademord revised this gist
Oct 1, 2014 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -24,12 +24,11 @@ def replace_entities(text, grouped_entities, **replacers): for entity_pair in entities: indices, entity = entity_pair i, last_idx = indices output.write(text[last_idx : i]) output.write(entity) output.write(text[last_idx :]) -
Garciat revised this gist
Sep 30, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -16,7 +16,7 @@ def replace_entities(text, grouped_entities, **replacers): entity_pair = (entity['indices'], replacer(entity)) entities.append(entity_pair) entities.sort(key = lambda x: x[0][0]) output = StringIO() last_idx = 0 -
Garciat revised this gist
Sep 30, 2014 . 1 changed file with 13 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,11 @@ import sys if sys.version_info[0] == 3: from io import StringIO else: from cStringIO import StringIO def replace_entities(text, grouped_entities, **replacers): entities = [] for entity_type, entity_list in grouped_entities.items(): @@ -10,22 +18,22 @@ def replace_entities(text, grouped_entities, **replacers): entities = sorted(entities, key = lambda x: x[0][0]) output = StringIO() last_idx = 0 for entity_pair in entities: indices, entity = entity_pair i, j = indices output.write(text[last_idx : i]) output.write(entity) last_idx = j output.write(text[last_idx :]) return output.getvalue() def tweet_replace_links(tweet): def url_replacer(url): -
Garciat revised this gist
Sep 30, 2014 . 1 changed file with 7 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,16 +5,18 @@ def replace_entities(text, grouped_entities, **replacers): if replacer is None: continue for entity in entity_list: entity_pair = (entity['indices'], replacer(entity)) entities.append(entity_pair) entities = sorted(entities, key = lambda x: x[0][0]) output = '' last_idx = 0 for entity_pair in entities: indices, entity = entity_pair i, j = indices output += text[last_idx : i] output += entity -
Garciat revised this gist
Sep 30, 2014 . 1 changed file with 4 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,21 +5,19 @@ def replace_entities(text, grouped_entities, **replacers): if replacer is None: continue for entity in entity_list: entity = replacer(entity) entities.append(entity) entities = sorted(entities, key = lambda x: x[1]['indices'][0]) output = '' last_idx = 0 for entity in entities: i, j = entity['indices'] output += text[last_idx : i] output += entity last_idx = j -
Garciat revised this gist
Sep 30, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -18,12 +18,12 @@ def replace_entities(text, grouped_entities, **replacers): i, j = entity['indices'] output += text[last_idx : i] output += replacer(entity) last_idx = j output += text[last_idx :] return output -
Garciat revised this gist
Sep 30, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -16,12 +16,12 @@ def replace_entities(text, grouped_entities, **replacers): for entity_pair in entities: replacer, entity = entity_pair i, j = entity['indices'] output += text[last_idx:i] output += replacer(entity) last_idx = j output += text[last_idx:] -
Garciat revised this gist
Sep 30, 2014 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,20 @@ def replace_entities(text, grouped_entities, **replacers): entities = [] for entity_type, entity_list in grouped_entities.items(): replacer = replacers.get(entity_type) if replacer is None: continue for entity in entity_list: entity_pair = (replacer, entity) entities.append(entity_pair) entities = sorted(entities, key = lambda x: x[1]['indices'][0]) output = '' last_idx = 0 for entity_pair in entities: replacer, entity = entity_pair cur_ids = entity['indices'] -
Garciat created this gist
Sep 30, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ def replace_entities(text, grouped_entities, **replacers): entities = [] for entity_type, entity_list in grouped_entities.items(): if entity_type not in replacers: continue for entity in entity_list: entities.append((entity_type, entity)) entities = sorted(entities, key = lambda x: x[1]['indices'][0]) output = '' last_idx = 0 for entity_pair in entities: entity_type, entity = entity_pair replacer = replacers.get(entity_type) cur_ids = entity['indices'] output += text[last_idx:cur_ids[0]] output += replacer(entity) last_idx = cur_ids[1] output += text[last_idx:] return output def tweet_replace_links(tweet): def url_replacer(url): return '<a href="%s">%s</a>' % (url['expanded_url'], url['display_url']) return replace_entities(tweet['text'], tweet['entities'], urls = url_replacer) tweet = { "text": "Today, Twitter is updating embedded Tweets to enable a richer photo experience: https://t.co/XdXRudPXH5", "entities": { "hashtags": [], "symbols": [], "urls": [{ "url": "https://t.co/XdXRudPXH5", "expanded_url": "https://blog.twitter.com/2013/rich-photo-experience-now-in-embedded-tweets-3", "display_url": "blog.twitter.com/2013/rich-phot...", "indices": [80, 103] }], "user_mentions": [] } } print(tweet_replace_links(tweet))