<commit_before>import argparse
from googleapiclient import discovery
import httplib2
import json
from oauth2client.client import GoogleCredentials

# Notes to self
# pip install --upgrade google-api-python-client
# set up application default credentials in GOOGLE_APPLICATION_CREDENTIALS

DISCOVERY_URL = ('https://{api}.googleapis.com/'
                 '$discovery/rest?version={apiVersion}')
ENCODING_TYPE = 'UTF32'

class GoogleNlp:
    """
        This class is a wrapper for the Google Natural Language API. Authentication
        is handled using the recommended Application Default Credentials.

        Todo:
            * Pick and adhere to a consistent style
    """
    def __init__(self):
        # Authenticate (boilerplate stuff from the API docs)
        self.http = httplib2.Http()
        self.credentials = GoogleCredentials.get_application_default().create_scoped(
            ['https://www.googleapis.com/auth/cloud-platform'])
        self.credentials.authorize(self.http)
        self.service = discovery.build('language',
                                       'v1beta1',
                                       http = self.http,
                                       discoveryServiceUrl = DISCOVERY_URL)

    def analyze_sentiment(self, text_string):
        """
            Run sentiment analysis on text from a string using the Google
            natural language API

            args:
                text_file (string) : a file path to the text file to be analyzed
        """
        service_request = self.service.documents().analyzeSentiment(
            body = {
                'document': {
                    'type': 'PLAIN_TEXT',
                    'content': text_string
                }
            }
        )
        response = service_request.execute()
        return response



    def analyze_entities(self, text_string):
        """
            Run entity extraction on text from a string using the Google
            natural language API

            args:
                text_file (string): a file path to the text file to be analyzed
        """
        service_request = self.service.documents().analyzeEntities(
            body = {
                'document': {
                    'type': 'PLAIN_TEXT',
                    'content': text_string
                },
                'encodingType': ENCODING_TYPE
            }
        )
        response = service_request.execute()
        return response


    def annotate_text(self, text_string, extract_syntax = True, extract_entities = True, extract_sentiment = True):
        """
            Run entity extraction on text from a string using the Google
            natural language API

            args:
                text_file (string): a file path to the text file to be analyzed
                extract_syntax (boolean): Extract syntax information?
                    default = True
                extract_entities (boolean): Extract entities?
                    default = True
                extract_sentiment (boolean): Extract sentiment for the whole document?
                    default = True
        """
        service_request = self.service.documents().annotateText(
            body = {
                'document': {
                    'type': 'PLAIN_TEXT',
                    'content': text_string
                },
                'features': {
                    'extractSyntax': extract_syntax,
                    'extractEntities': extract_entities,
                    'extractDocumentSentiment': extract_sentiment
                },
                'encodingType': ENCODING_TYPE
            }
        )
        response = service_request.execute()
        return response



    def analyze_sentiment_file(self, text_file):
        """<commit_msg>fixing some typos in the docstrings<commit_after>import argparse
from googleapiclient import discovery
import httplib2
import json
from oauth2client.client import GoogleCredentials

# Notes to self
# pip install --upgrade google-api-python-client
# set up application default credentials in GOOGLE_APPLICATION_CREDENTIALS

DISCOVERY_URL = ('https://{api}.googleapis.com/'
                 '$discovery/rest?version={apiVersion}')
ENCODING_TYPE = 'UTF32'

class GoogleNlp:
    """
        This class is a wrapper for the Google Natural Language API. Authentication
        is handled using the recommended Application Default Credentials.

        Todo:
            * Pick and adhere to a consistent style
    """
    def __init__(self):
        # Authenticate (boilerplate stuff from the API docs)
        self.http = httplib2.Http()
        self.credentials = GoogleCredentials.get_application_default().create_scoped(
            ['https://www.googleapis.com/auth/cloud-platform'])
        self.credentials.authorize(self.http)
        self.service = discovery.build('language',
                                       'v1beta1',
                                       http = self.http,
                                       discoveryServiceUrl = DISCOVERY_URL)

    def analyze_sentiment(self, text_string):
        """
            Run sentiment analysis on text from a string using the Google
            natural language API

            args:
                text_string (string): a text string to be analyzed
        """
        service_request = self.service.documents().analyzeSentiment(
            body = {
                'document': {
                    'type': 'PLAIN_TEXT',
                    'content': text_string
                }
            }
        )
        response = service_request.execute()
        return response



    def analyze_entities(self, text_string):
        """
            Run entity extraction on text from a string using the Google
            natural language API

            args:
                text_string (string): a text string to be analyzed
        """
        service_request = self.service.documents().analyzeEntities(
            body = {
                'document': {
                    'type': 'PLAIN_TEXT',
                    'content': text_string
                },
                'encodingType': ENCODING_TYPE
            }
        )
        response = service_request.execute()
        return response


    def annotate_text(self, text_string, extract_syntax = True, extract_entities = True, extract_sentiment = True):
        """
            Run entity extraction on text from a string using the Google
            natural language API

            args:
                text_string (string): a text string to be analyzed
                extract_syntax (boolean): Extract syntax information?
                    default = True
                extract_entities (boolean): Extract entities?
                    default = True
                extract_sentiment (boolean): Extract sentiment for the whole document?
                    default = True
        """
        service_request = self.service.documents().annotateText(
            body = {
                'document': {
                    'type': 'PLAIN_TEXT',
                    'content': text_string
                },
                'features': {
                    'extractSyntax': extract_syntax,
                    'extractEntities': extract_entities,
                    'extractDocumentSentiment': extract_sentiment
                },
                'encodingType': ENCODING_TYPE
            }
        )
        response = service_request.execute()
        return response



    def analyze_sentiment_file(self, text_file):
        """
{'fraction_non_alphanumeric': 0.07401533174729051, 'fraction_numerical': 0.0018503832936822627, 'mean_word_length': 1.6246964967048214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5486789', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1711, 'n_words': 501}