Skip to content

Instantly share code, notes, and snippets.

@Ademord
Forked from Garciat/tweet_entities.py
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save Ademord/769a9516c9a1aaa9e6ab to your computer and use it in GitHub Desktop.

Select an option

Save Ademord/769a9516c9a1aaa9e6ab to your computer and use it in GitHub Desktop.

Revisions

  1. Ademord revised this gist Oct 1, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions tweet_entities.py
    Original 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, j = indices
    i, last_idx = indices

    output.write(text[last_idx : i])
    output.write(entity)

    last_idx = j

    output.write(text[last_idx :])

  2. @Garciat Garciat revised this gist Sep 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tweet_entities.py
    Original 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 = sorted(entities, key = lambda x: x[0][0])
    entities.sort(key = lambda x: x[0][0])

    output = StringIO()
    last_idx = 0
  3. @Garciat Garciat revised this gist Sep 30, 2014. 1 changed file with 13 additions and 5 deletions.
    18 changes: 13 additions & 5 deletions tweet_entities.py
    Original 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 = ''
    output = StringIO()
    last_idx = 0

    for entity_pair in entities:
    indices, entity = entity_pair

    i, j = indices

    output += text[last_idx : i]
    output += entity
    output.write(text[last_idx : i])
    output.write(entity)

    last_idx = j

    output += text[last_idx :]
    output.write(text[last_idx :])

    return output
    return output.getvalue()

    def tweet_replace_links(tweet):
    def url_replacer(url):
  4. @Garciat Garciat revised this gist Sep 30, 2014. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions tweet_entities.py
    Original 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 = replacer(entity)
    entities.append(entity)
    entity_pair = (entity['indices'], replacer(entity))
    entities.append(entity_pair)

    entities = sorted(entities, key = lambda x: x[1]['indices'][0])
    entities = sorted(entities, key = lambda x: x[0][0])

    output = ''
    last_idx = 0

    for entity in entities:
    i, j = entity['indices']
    for entity_pair in entities:
    indices, entity = entity_pair

    i, j = indices

    output += text[last_idx : i]
    output += entity
  5. @Garciat Garciat revised this gist Sep 30, 2014. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions tweet_entities.py
    Original 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_pair = (replacer, entity)
    entities.append(entity_pair)
    entity = replacer(entity)
    entities.append(entity)

    entities = sorted(entities, key = lambda x: x[1]['indices'][0])

    output = ''
    last_idx = 0

    for entity_pair in entities:
    replacer, entity = entity_pair

    for entity in entities:
    i, j = entity['indices']

    output += text[last_idx : i]
    output += replacer(entity)
    output += entity

    last_idx = j

  6. @Garciat Garciat revised this gist Sep 30, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tweet_entities.py
    Original 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 += text[last_idx : i]
    output += replacer(entity)

    last_idx = j

    output += text[last_idx:]
    output += text[last_idx :]

    return output

  7. @Garciat Garciat revised this gist Sep 30, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions tweet_entities.py
    Original 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

    cur_ids = entity['indices']
    i, j = entity['indices']

    output += text[last_idx:cur_ids[0]]
    output += text[last_idx:i]
    output += replacer(entity)

    last_idx = cur_ids[1]
    last_idx = j

    output += text[last_idx:]

  8. @Garciat Garciat revised this gist Sep 30, 2014. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions tweet_entities.py
    Original 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():
    if entity_type not in replacers:
    replacer = replacers.get(entity_type)
    if replacer is None:
    continue
    for entity in entity_list:
    entities.append((entity_type, entity))
    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:
    entity_type, entity = entity_pair
    replacer = replacers.get(entity_type)
    replacer, entity = entity_pair

    cur_ids = entity['indices']

  9. @Garciat Garciat created this gist Sep 30, 2014.
    50 changes: 50 additions & 0 deletions tweet_entities.py
    Original 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))