Пример #1
0
    def test_sightings(self):

        if python_version == 3:
            key = input("Testing Sightings: What's your API Key?\n")
        elif python_version == 2:
            key = raw_input("What's your API Key?\n")

        hatebase = HatebaseAPI({"key": key, "debug": False})
        filters = {
            'is_about_nationality': '1',
            'language': 'eng',
            'country_id': 'US'
        }
        output = "json"
        # get sightings
        response = hatebase.getSightings(filters=filters, format=output)
        # get some details from response
        results = response["number_of_results"]
        pages = response["number_of_pages"]

        if hatebase.debug == True:
            print(response)
        self.assertEqual(response["token"], hatebase.token)
        self.assertTrue(int(pages) > 500)
        self.assertTrue(int(results) > 54239)
Пример #2
0
    def test_vocabulary_details(self):
        if python_version == 3:
            key = input("Testing Vocabulary Details: What's your API Key?\n")
        elif python_version == 2:
            key = raw_input(
                "Testing Vocabulary Details: What's your API Key?\n")

        hatebase = HatebaseAPI({"key": key, "debug": False})
        format = "json"
        vocab_filter = {"language": "eng"}
        # get vocabulary and its first result
        response = hatebase.getVocabulary(filters=vocab_filter, format=format)
        vocab = response["result"]
        # get vocabulary_id of first vocab entry
        vocab_id = vocab[0]["vocabulary_id"]
        details_filters = {'vocabulary_id': vocab_id}
        # get vocab details
        response = hatebase.getVocabularyDetails(filters=details_filters,
                                                 format=format)
        resp_vocab_id = response["result"]["vocabulary_id"]
        if hatebase.debug == True:
            print(response)

        self.assertEqual(response["token"], hatebase.token)
        self.assertEqual(vocab_id, resp_vocab_id)
Пример #3
0
def main():
    hatebase = HatebaseAPI({"key": '4UyMTCENrNkmsYgPZiRneBVbVJYHDY7F'})
    filters = {"language": "eng"}
    format = "json"
    # initialize list for all vocabulary entry dictionaries
    eng_vocab = []
    response = hatebase.getVocabulary(filters=filters, format=format)
    pages = response["number_of_pages"]
    # fill the vocabulary list with all entries of all pages
    # this might take some time...
    for page in range(1, pages+1):
        filters["page"] = str(page)
        response = hatebase.getVocabulary(filters=filters, format=format)
        eng_vocab.append(response["result"])

    # create empty pandas df for all vocabulary entries
    df_eng_vocab = pd.DataFrame()
    # fill df
    for elem in eng_vocab:
        df_eng_vocab = df_eng_vocab.append(elem)
    # reset the df index
    df_eng_vocab.reset_index(drop=True, inplace=True)
    df_eng_vocab.to_csv('hatebase_eng.csv')
Пример #4
0
    def test_vocabulary(self):

        if python_version == 3:
            key = input("Testing Vocabulary: What's your API Key?\n")
        elif python_version == 2:
            key = raw_input("Testing Vocabulary: What's your API Key?\n")

        hatebase = HatebaseAPI({"key": key, "debug": False})
        filters = {"language": "deu"}
        format = "json"
        # get vocabulary
        response = hatebase.getVocabulary(filters=filters, format=format)
        # get some details from response
        vocablist = response["result"]
        results = response["number_of_results"]
        pages = response["number_of_pages"]
        lang = response["language"]

        if hatebase.debug == True:
            print("len(vocab): {}, # of pages: {}, # of results: {}, lang: {}".
                  format(len(vocablist), pages, results, lang))
        self.assertEqual(response["token"], hatebase.token)
        self.assertTrue(int(results) >= 18)
        self.assertEqual(int(response['number_of_results']), int(results))
#Check hate word frequency in a list of messages.
#Uses NTLK and Hatebase libraries
#Graphs top ten occurences of hate words!

#ONLY DO ONCE
#DO NOT WASTE API CALLS
key = "ENTER_YOU_KEY_HERE"

from json import loads
from hatebase import HatebaseAPI

hatebase = HatebaseAPI({"key": key})
#"language": "eng"
pagenumber = 1
responses = []

#Loads top 1000 results
while pagenumber <= 11:
    filters = {"country": "US", "page": str(pagenumber)}
    output = "json"
    query_type = "vocabulary"
    response = hatebase.performRequest(filters, output, query_type)

    # convert to Python object
    responses.append(loads(response))
    pagenumber += 1

print "Done getting API results"

#Process Hate Words
data = []
Пример #6
0
import json
import requests
from hatebase import HatebaseAPI

key = "TuwmngrrxcytZkgqyfvtpdUb4yjsKsru"

hatebase = HatebaseAPI({"key": key})
filters = {
    'is_about_nationality': "false",
    'is_about_ethnicity': "false",
    'is_about_religion': "false",
    'is_about_gender': "false",
    'is_about_sexual_orientation': "false",
    'is_about_disability': "false",
    'is_about_class': "true",
    'language': 'ENG',
    'country': 'US',
    'year': "2015"
}
format = "json"
json_response = hatebase.getSightings(filters=filters, format=format)

with open('classOnly2015.txt', 'w+') as outfile:
    outfile.write(json.dumps(json_response, indent=4))
Пример #7
0
from json import loads
import json
from hatebase import HatebaseAPI

hatebase = HatebaseAPI({"key": '1cfe0ef015998aea71f2ad32bf9f601f'})
filters = {'language': 'eng', 'type': 't'}
output = "json"
query_type = "vocabulary"
response = hatebase.performRequest(filters, output, query_type)

# convert to Python object
response = loads(response)

with open('hatefile', 'w') as w:
    w.write(json.dumps(response["data"]))
Пример #8
0
import json

from hatebase import HatebaseAPI

key = input('Please enter your api key for https://hatebase.org/: ')

hatebase = HatebaseAPI({"key": key})
filters = {"language": "eng"}
format = "json"
# initialize list for all vocabulary entry dictionaries
en_vocab = {}
response = hatebase.getVocabulary(filters=filters, format=format)
pages = response["number_of_pages"]
# fill the vocabulary list with all entries of all pages
# this might take some time...
for page in range(1, pages + 1):
    filters["page"] = str(page)
    response = hatebase.getVocabulary(filters=filters, format=format)
    result = response["result"]
    en_vocab[result['term']] = result

with open('en_vocab.json', 'w', encoding='utf-8') as json_file:
    json.dump(en_vocab, json_file)
''' This is the code for using the Hatebase API
It uses this Python wrapper: https://github.com/DanielJDufour/hatebase
which can be installed with: pip install hatebase

'''
from json import loads
from hatebase import HatebaseAPI

key = # get a key here: https://www.hatebase.org/request_api

# Define parameters
hatebase = HatebaseAPI({"key": key})
filters = {'language': 'eng'}
output = 'json'
query_type = 'sightings'

# Query the database
response = hatebase.performRequest(filters, output, query_type)

# Convert to Python object
resp = loads(response)