Created
April 17, 2015 09:04
-
-
Save gr-a-m/ed6327966b99dedef365 to your computer and use it in GitHub Desktop.
get_itemsets.py
This file contains 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 characters
import fileinput | |
import json | |
import urllib2 | |
def main(): | |
submit_object = { | |
"threshold": 12, | |
"documents": [] | |
} | |
lines = [] | |
for line in fileinput.input(): | |
lines.append(line) | |
link, hand, generated = line.split("\t") | |
submit_object["documents"].append(generated.strip().split(",")) | |
request = urllib2.Request('https://api.algorithmia.com/api/GrantMarshall/FrequentItemsets/0.1.1') | |
request.add_header('Content-Type', 'application/json') | |
request.add_header('Authorization', '###') # Put you authorization here | |
request.add_header('Accept', 'application/json') | |
response = urllib2.urlopen(request, json.dumps(submit_object)) | |
response_string = response.read() | |
print(response_string) | |
response_object = json.loads(response_string) | |
large_itemsets = [] | |
for itemset in response_object["result"]["itemsets"]: | |
if len(itemset) > 1: | |
large_itemsets.append(set(itemset)) | |
for line in lines: | |
link, hand, generated = line.split("\t") | |
new_tags = generated.split(",") | |
generated_tags = set(generated.split(",")) | |
for itemset in large_itemsets: | |
if set(itemset) <= generated_tags: | |
new_tags.append(" ".join(itemset)) | |
print("{}\t{}\t{}".format(link, hand, ",".new_tags)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment