<commit_before>import os
import json

from cloudant import Cloudant

from watson_developer_cloud import LanguageTranslatorV2
from watson_developer_cloud import TextToSpeechV1
from watson_developer_cloud import SpeechToTextV1
from watson_developer_cloud import VisualRecognitionV3
from watson_developer_cloud import NaturalLanguageUnderstandingV1
from watson_developer_cloud import NaturalLanguageClassifierV1

service_dict = {
    'language_translator': LanguageTranslatorV2,
    'text_to_speech': TextToSpeechV1,
    'speech_to_text': SpeechToTextV1,
    'natural-language-understanding': NaturalLanguageUnderstandingV1,
    'watson_vision_combined': VisualRecognitionV3,
}


def get_credentials():
    if 'VCAP_SERVICES' in os.environ:
        vcap = json.loads(os.getenv('VCAP_SERVICES'))
    elif os.path.isfile('vcap-local.json'):
        with open('vcap-local.json') as f:
            vcap = json.load(f)
            vcap = vcap['services']
    return vcap


def get_database(vcap, dbname):
    assert 'cloudantNoSQLDB' in vcap

    creds = vcap['cloudantNoSQLDB'][0]['credentials']

    user = creds['username']
    password = creds['password']
    url = creds['host']

    client = Cloudant(user, password, url=url, connect=True)
    db = client.create_database(dbname, throw_on_exists=False)

    return db, client


def get_watson_service(vcap, name):

    if name == 'visual-recognition':
        name = 'watson_vision_combined'

    cls = service_dict[name]

    creds = vcap[name][0]['credentials']

    if name in ['watson_vision_combined']:
        api_key = creds["api_key"]
        handler = cls(cls.latest_version, api_key=api_key)
    else:
        user = creds['username']
        password = creds['password']
        url = cls.default_url
        if name in ['natural-language-understanding']:
            handler = cls(cls.latest_version, username=user, password=password, url=url)
        else:
            handler = cls(username=user, password=password, url=url)

    return handler

# def get_translator(vcap):
#     creds = vcap['language_translator'][0]['credentials']

#     user = creds['username']
#     password = creds['password']
#     url = LanguageTranslatorV2.default_url

#     translator = LanguageTranslatorV2(username=user, password=password, url=url)

#     return translator


# def get_text_to_speech(vcap):
#     creds = vcap['text_to_speech'][0]['credentials']

#     user = creds['username']
#     password = creds['password']
#     url = TextToSpeechV1<commit_msg>Clean services.py<commit_after>import os
import json

from cloudant import Cloudant

from watson_developer_cloud import LanguageTranslatorV2
from watson_developer_cloud import TextToSpeechV1
from watson_developer_cloud import SpeechToTextV1
from watson_developer_cloud import VisualRecognitionV3
from watson_developer_cloud import NaturalLanguageUnderstandingV1

service_dict = {
    'language_translator': LanguageTranslatorV2,
    'text_to_speech': TextToSpeechV1,
    'speech_to_text': SpeechToTextV1,
    'natural-language-understanding': NaturalLanguageUnderstandingV1,
    'watson_vision_combined': VisualRecognitionV3,
}


def get_credentials():
    if 'VCAP_SERVICES' in os.environ:
        vcap = json.loads(os.getenv('VCAP_SERVICES'))
    elif os.path.isfile('vcap-local.json'):
        with open('vcap-local.json') as f:
            vcap = json.load(f)
            vcap = vcap['services']
    return vcap


def get_database(vcap, dbname):
    assert 'cloudantNoSQLDB' in vcap

    creds = vcap['cloudantNoSQLDB'][0]['credentials']

    user = creds['username']
    password = creds['password']
    url = creds['host']

    client = Cloudant(user, password, url=url, connect=True)
    db = client.create_database(dbname, throw_on_exists=False)

    return db, client


def get_watson_service(vcap, name):

    if name == 'visual-recognition':
        name = 'watson_vision_combined'

    cls = service_dict[name]

    creds = vcap[name][0]['credentials']

    if name in ['watson_vision_combined']:
        api_key = creds["api_key"]
        handler = cls(cls.latest_version, api_key=api_key)
    else:
        user = creds['username']
        password = creds['password']
        url = cls.default_url
        if name in ['natural-language-understanding']:
            handler = cls(cls.latest_version, username=user, password=password, url=url)
        else:
            handler = cls(username=user, password=password, url=url)

    return handler
{'fraction_non_alphanumeric': 0.10380313199105146, 'fraction_numerical': 0.006711409395973154, 'mean_word_length': 3.913186813186813, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1006678', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1414, 'n_words': 288}