def setUp(self): self.g = geosearchclass.GeoSearchClass() self.g.latitude = 37.7821 self.g.longitude = -122.4093 self.g.radius = 100 self.g.search_term = "" self.g.result_type = 'mixed' self.g.count = 100 self.sr = self.g.search()
def main(): parser = get_parser() args = parser.parse_args() if args.doc: print __doc__ sys.exit() g = geosearchclass.GeoSearchClass() if args.params: print 'Using parameters from ' + str(args.params) # turn parameter file into dictionary g.set_params_from_file(args.params) if args.address: print "Finding geocoordates for address:\n{}".format(args.address) coords = geo_converter.get_geocoords_from_address(args.address) if coords: g.latitude = coords[0] print "Found this latitude:" print g.latitude g.longitude = coords[1] print "Found this longitude:" print g.longitude else: print "Failed to find coordinates. Exiting." sys.exit() if args.input: text = utils.load_file(args.input) tokens = utils.tokenize_normal_words(text) for_poem = utils.filter_words(tokens) else: for_poem = get_default_words() if args.markov: if args.input: raise StandardError("Can only input a single text file. \ use --markov <your_text_file.txt>") else: text = utils.load_file(args.markov) # ngram = ngrams.make_ngram(text, 2) ngram = ngrams.make_bigram_trigram_dictionary(text) formatted_poem = create_poem(g, for_poem, ngram) else: formatted_poem = create_poem(g, for_poem) if args.output: print '\nwriting formatted poem to ' + str(args.output) output_file = args.output else: print "\nwriting formatted poem to poem.txt" output_file = "poem.txt" utils.save_file(output_file, formatted_poem)
def main(): # set to DEBUG, INFO, WARNING, ERROR, CRITICAL : logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) import geosearchclass g = geosearchclass.GeoSearchClass() print "Using search values from params.txt" g.set_params_from_file('params.txt') search_results = g.search() parse_tweets(search_results)
def main(): parser = get_parser() args = parser.parse_args() # print args # print args.help if args.doc: print __doc__ import sys sys.exit(0) if args.number: number = int(args.number) else: number = 30 g = geosearchclass.GeoSearchClass() if args.filename: print 'Using parameters from ' + str(args.filename) g.set_params_from_file(args.filename) else: print "Using search values from params.txt" g.set_params_from_file('params.txt') if args.address: print "Finding geocoordates for address:\n{}".format(args.address) coords = geo_converter.get_geocoords_from_address(args.address) if coords: g.latitude = coords[0] print "Found this latitude:" print g.latitude g.longitude = coords[1] print "Found this longitude:" print g.longitude else: print "Failed to find coordinates. Exiting." sys.exit() if args.stream: print "using streaming queue" q = Queue.Queue() bounding_box = geo_converter.get_bounding_box_from(g) search_terms = geo_converter.get_search_terms_from(g) print "bounding_box = {}".format(bounding_box) print "search_terms = {}".format(search_terms) global stream fn = 'tweets.json' stream = streamer.start_stream(q, bounding_box, fn, search_terms) updating_stream_plot(q, number) else: print "using REST API updating plot" updating_plot(g, number, True) # set grow flag to True
def main(): parser = get_parser() args = parser.parse_args() if args.doc: print __doc__ sys.exit() g = geosearchclass.GeoSearchClass() if args.filename: print 'Using parameters from ' + str(args.filename) # turn parameter file into dictionary g.set_params_from_file(args.filename) else: if args.default: print 'Using default search terms' else: print 'Using parameters from params.txt' g.set_params_from_file('params.txt') g.search() # print formatted results with extra info to terminal if args.verbose: g.print_search_results() if args.output: g.write_search_results(args.output) else: g.write_search_results() if args.json: g.json_search_results(args.json) if args.visualize: import utils filtered_words = utils.tokenize_and_filter(g.search_results) utils.visualize(filtered_words)
def main(): parser = get_parser() args = parser.parse_args() if args.doc: print __doc__ import sys sys.exit(0) # pass in an API to GeoSearchClass to get full access for posting (api, __) = utils.get_credentials('consumerkeyandsecret', False) g = geosearchclass.GeoSearchClass('params.txt', None, api) if args.filename: print 'Using parameters from ' + str(args.filename) g.set_params_from_file(args.filename) else: print "Using search values from params.txt" g.set_params_from_file('params.txt') if args.output: fn = str(args.output) else: fn = 'tweets.json' print 'Output file: ' + fn if args.address: print "Finding geocoordates for address:\n{}".format(args.address) coords = geo_converter.get_geocoords_from_address(args.address) if coords: g.latitude = coords[0] g.longitude = coords[1] else: print "Failed to find coordinates" sys.exit() verify(g, fn)
def create_poem(g=None, default_words=None, ngram=None): """ This creates a poem with user input by suggesting from the words supplied. A user can use the word, decline the word, or add their own input. g is for geosearchclass. It is none by default. default_words is a list of words that can be enabled by default. """ words = [] formatted_poem = '''''' # for no, yes and finish (print poem) options = ['y', 'n', 's', 'd', 'r', 'e', 'f', '\n'] keep_adding = True added_default = False use_phrases = False random_word = False print "\n\n\n" print """ This robot poet will present a series of suggestions. You can either use these suggestions, edit them, or type your own input. You may also add more words from geolocated tweets to your word corpus. The words you choose or add will be succeessively added to a poem, which will be printed and saved to an output file. To add a new line, type '\\n'. To finish writing type f (for finish). y: yes use this word n: no, skip this and give me a new phrase s: search: add more geolocated terms from twitter d: default words added to corpus r: get random word, when running markov model e: edit the text \\n: enter line f: finish """ if ngram: print "Populating seed words from markov chain ngram" values = sum(ngram.values(), []) words.extend(values) chosen = "" while keep_adding: if len(words) == 0: print "Nothing in corpus. Type d for default words or s to search\ twitter" if ngram and formatted_poem and not random_word: tokens = utils.tokenize_normal_words(formatted_poem) num = random.random() potential_word = "" if len(tokens) > 0: # This is for trigrams if num > 0.66 and len(tokens) > 1: # 50% of time get trigram potential_word = tokens_to_word(tokens, ngram, 2) if potential_word: chosen = potential_word else: potential_word = tokens_to_word(tokens, ngram, 1) if potential_word: chosen = potential_word else: chosen = random.choice(words) elif num > 0.33: # 30% of time get bigram potential_word = tokens_to_word(tokens, ngram, 1) if potential_word: chosen = potential_word else: chosen = random.choice(words) else: # 20% of time get random word chosen = random.choice(words) else: chosen = random.choice(words) elif words: chosen = random.choice(words) random_word = False else: pass if chosen: print chosen, response_string = " " + str(options) + " or your own :" response = raw_input(response_string) # include the chosen word: if response == "y": if len(words) == 0: continue formatted_poem = formatted_poem + ''' ''' + chosen print print formatted_poem continue elif response == "n": continue elif response == "r": random_word = True elif response == "s": print "Searching geo-located tweets to add to vocab" print "This can only be used once every 5 seconds" if g is None: g = geosearchclass.GeoSearchClass() search_results = g.search() phrase_response = "" while phrase_response not in ["y", "n"]: phrase_response = raw_input("\nWould you like to use phrases (\ (otherwise, just words)? [y/n]: ") if phrase_response == "y": list_of_info_dicts = write.parse_tweets(search_results) filtered_words = [] if len(list_of_info_dicts) < 1: filtered_words = utils.tokenize_and_filter( search_results) else: for d in list_of_info_dicts: filtered_words.append(d['phrase']) elif phrase_response == "n": filtered_words = utils.tokenize_and_filter(search_results) else: continue print "\n\n\nAdding these Twitter words: " print filtered_words print "\n" words.extend(filtered_words) continue elif response == "d": if not added_default: print "\nadding in these words to corpus:" print default_words print "\n\n\n" words.extend(default_words) options.remove('d') added_default = True elif response == "e": formatted_poem = editor.create_editor(formatted_poem) print formatted_poem elif response not in options: response = response.replace('\\n', '\n') formatted_poem = formatted_poem + ''' ''' + response print print formatted_poem continue elif response == "f": print print formatted_poem keep_adding = False return formatted_poem