<commit_before>from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types

from tagnews.senteval import police_words


def process_google_result(text):
    document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT)
    sentiment = client.analyze_entity_sentiment(document=document)

    for entity in sentiment.entities:
        clean_entity = "".join(filter(str.isalpha, entity)).lower()

        if clean_entity in police_words:

            for mention in entity.mentions:
                return mention.sentiment.score


class SentimentGoogler:
    def __init__(self):
        self.client = self.connect_to_client()

    def run(self, doc_text):
        sentiment_ = self.call_api(doc_text)
        for entity in sentiment_.entities:
            police_entity = self.is_police_entity(entity)
            if police_entity:
                return self.sentiment_from_entity(police_entity)

    def connect_to_client(self):
        return language.LanguageServiceClient()

    def sentiment_from_entity(self, entity):
        return entity.sentiment.score

    def call_api(self, doc_text):
        """
        Parameters
        ----------
        doc_text : str
            article text

        Returns
        -------
        sentiment : json
            google response call
        """
        document = types.Document(content=doc_text, type=enums.Document.Type.PLAIN_TEXT)
        sentiment = self.client.analyze_entity_sentiment(document=document)

        return sentiment

    def is_police_entity(self, entity):
        possible_responses = [
            "police",
            "officer",
            "cop",
            "officers",
            "pigs",
            "policeofficer",
        ]
        if entity in possible_responses:
            return entity
        for mention in entity.mentions:
            if pre_process_text(mention.text.content) in possible_responses:
                return entity
            return False<commit_msg>police words in separate file<commit_after>from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types

from tagnews.senteval import police_words


def process_google_result(text):
    document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT)
    sentiment = client.analyze_entity_sentiment(document=document)

    for entity in sentiment.entities:
        clean_entity = "".join(filter(str.isalpha, entity)).lower()

        if clean_entity in police_words:

            for mention in entity.mentions:
                return mention.sentiment.score


class SentimentGoogler:
    def __init__(self):
        self.client = self.connect_to_client()
        self.polce_words = police_words

    def run(self, doc_text):
        sentiment_ = self.call_api(doc_text)
        for entity in sentiment_.entities:
            police_entity = self.is_police_entity(entity)
            if police_entity:
                return self.sentiment_from_entity(police_entity)

    def connect_to_client(self):
        return language.LanguageServiceClient()

    def sentiment_from_entity(self, entity):
        return entity.sentiment.score

    def call_api(self, doc_text):
        """
        Parameters
        ----------
        doc_text : str
            article text

        Returns
        -------
        sentiment : json
            google response call
        """
        document = types.Document(content=doc_text, type=enums.Document.Type.PLAIN_TEXT)
        sentiment = self.client.analyze_entity_sentiment(document=document)

        return sentiment

    def is_police_entity(self, entity):
        possible_responses = [
            "police",
            "officer",
            "cop",
            "officers",
            "pigs",
            "policeofficer",
        ]
        if entity in self.police_words:
            return entity
        for mention in entity.mentions:
            if pre_process_text(mention.text.content) in self.police_words:
                return entity
            return False
{'fraction_non_alphanumeric': 0.08085728202630298, 'fraction_numerical': 0.0, 'mean_word_length': 2.2060889929742387, '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': '1978330', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1078, 'n_words': 243}