Skip to content

Instantly share code, notes, and snippets.

@williardx
Created February 1, 2017 21:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williardx/828f0f2c5cd8ce06a3f203da926821b2 to your computer and use it in GitHub Desktop.
Save williardx/828f0f2c5cd8ce06a3f203da926821b2 to your computer and use it in GitHub Desktop.
Create a list of random topics from ConceptNet
"""Generates a list of random related concepts from ConceptNet. Concepts are
generated via a random walk through ConceptNet using ConceptNet's /assoc
endpoint, which takes in a list of concepts and ouptputs a list of related
concepts ranked by similarity to the given concepts.
Example:
| => python random_topic.py
Current topics are apocalypse and desert_island
Current topics are desert_island and real_world
Current topics are real_world and scienceless
Current topics are scienceless and cyberteaching
Current topics are cyberteaching and radiochemistry
Current topics are radiochemistry and metaprogramming
Current topics are metaprogramming and text_file
Current topics are text_file and catalogue
Current topics are catalogue and mole_cricket
Current topics are mole_cricket and scroll
Done!
Your topics are: apocalypse, desert_island, real_world, scienceless, cyberteaching,
radiochemistry, metaprogramming, text_file, catalogue, mole_cricket, scroll,
air_transportation
"""
import requests
import random
from operator import itemgetter
conceptnet_uri = ("http://conceptnet5.media.mit.edu/data/5.4/assoc/list/en"
"/{concept_one},{concept_two}?filter=/c/en&limit=1000")
current_topics = ["apocalypse", "desert_island"]
all_topics = []
num_topics = 10
if __name__ == "__main__":
all_topics += current_topics
for i in xrange(num_topics):
print "Current topics are %s and %s" % (current_topics[0],
current_topics[1])
# Choose a random similarity score to decide how to similar the next
# term should be to the two terms we passed to /assoc
similarity_level = random.random()
req = conceptnet_uri.format(
concept_one=current_topics[0],
concept_two=current_topics[1]
)
response = requests.get(req)
similar_terms = sorted(response.json()["similar"], key=itemgetter(1))
num_terms = len(similar_terms)
for j in xrange(num_terms):
# Choose the least dissimilar word defined by similarity_level or
# the last word in the list, whichever is first
if similar_terms[j][1] > random.random() or j == (num_terms - 1):
next_topic = similar_terms[j][0].replace("/c/en/", "")
break
current_topics.pop(0)
current_topics.append(next_topic)
all_topics.append(next_topic)
print "Done!"
print "Your topics are: %s" % ", ".join(all_topics)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment