Beispiel #1
0
 def convert(
     self,
     analyzer_response: TextPayload,
     base_payload: Optional[Dict[str, Any]] = None,
     **kwargs,
 ) -> Dict[str, Any]:
     base_payload = base_payload or {}
     merged_dict = {**base_payload, **analyzer_response.to_dict()}
     return flatten_dict(merged_dict)
Beispiel #2
0
    def convert(
        self,
        analyzer_response: AnalyzerResponse,
        base_payload: Optional[Dict[str, Any]] = None,
        **kwargs,
    ) -> Dict[str, Any]:
        request_payload = base_payload or {}

        if analyzer_response.source_name != "Twitter":
            return {**request_payload, **analyzer_response.to_dict()}

        source_information = kwargs["source_information"]

        user_url = ""
        positive = 0.0
        negative = 0.0
        text = ""
        tweet_id = None
        created_at_str = None
        classification_list = []

        flat_dict = flatten_dict(analyzer_response.to_dict())
        for k, v in flat_dict.items():
            if "username" in k:
                user_url = TWITTER_URL_PREFIX + v
            elif "text" in k:
                text = str(v).replace("\n", " ")
            elif "positive" in k:
                positive = float(v)
            elif "negative" in k:
                negative = float(v)
            elif "meta_id" in k:
                tweet_id = v
            elif "created_at" in k:
                created_at_str = v
            elif "segmented_data" in k and len(classification_list) < 2:
                classification_list.append(k.rsplit("_", 1)[1])

        if created_at_str:
            created_at = parser.isoparse(created_at_str)
            created_at_str = (
                created_at.replace(tzinfo=timezone.utc)
                .astimezone(tz=IST_TZ)
                .strftime("%Y-%m-%d %H:%M:%S")
            )

        tweet_url = user_url + "/status/" + tweet_id
        # Sentiment rules
        if negative > 8.0:
            sentiment = "Strong Negative"
        elif 0.3 < negative <= 8.0:
            sentiment = "Negative"
        elif positive >= 0.8:
            sentiment = "Strong Positive"
        elif 0.4 < positive < 0.8:
            sentiment = "Positive"
        else:
            sentiment = "Neutral"

        enquiry = {
            "Source": source_information,
            "FeedbackBy": user_url,
            "Sentiment": sentiment,
            "TweetUrl": tweet_url,
            "FormattedText": text,
            "PredictedCategories": ",".join(classification_list),
        }

        if created_at_str:
            enquiry["ReportedAt"] = created_at_str

        kv_str_list = [k + ": " + str(v) for k, v in enquiry.items()]
        request_payload["enquiryMessage"] = "\n".join(kv_str_list)
        return request_payload
Beispiel #3
0
    def convert(
        self,
        analyzer_response: TextPayload,
        base_payload: Optional[Dict[str, Any]] = None,
        **kwargs,
    ) -> Dict[str, Any]:
        request_payload = base_payload or {}
        use_enquiry_api = kwargs.get("use_enquiry_api", False)

        if analyzer_response.source_name != "Twitter":
            return {**request_payload, **analyzer_response.to_dict()}

        source_information = kwargs["source_information"]
        partner_id = kwargs["partner_id"]

        user_url = ""
        positive = 0.0
        negative = 0.0
        text = ""
        tweet_id = None
        created_at_str = None
        classification_list: List[str] = []

        flat_dict = flatten_dict(analyzer_response.to_dict())
        for k, v in flat_dict.items():
            if "username" in k:
                user_url = TWITTER_URL_PREFIX + v
            elif "text" in k:
                text = str(v).replace("\n", " ")
            elif "positive" in k:
                positive = float(v)
            elif "negative" in k:
                negative = float(v)
            elif "meta_id" in k:
                tweet_id = v
            elif "created_at" in k:
                created_at_str = v
            elif "segmented_data" in k and len(classification_list) < 2:
                classification_list.append(k.rsplit("_", 1)[1])

        created_at_str_parsed: Optional[str] = None
        if created_at_str:
            created_at = parser.isoparse(created_at_str)
            created_at_str_parsed = (created_at.replace(
                tzinfo=timezone.utc).astimezone(
                    tz=IST_TZ).strftime("%Y-%m-%d %H:%M:%S"))

        tweet_url = f"{user_url}/status/{tweet_id}"
        # Sentiment rules
        if negative > 8.0:
            sentiment = "Strong Negative"
        elif 0.3 < negative <= 8.0:
            sentiment = "Negative"
        elif positive >= 0.8:
            sentiment = "Strong Positive"
        elif 0.4 < positive < 0.8:
            sentiment = "Positive"
        else:
            sentiment = "Neutral"

        if use_enquiry_api:
            enquiry = {
                "Source": source_information,
                "FeedbackBy": user_url,
                "Sentiment": sentiment,
                "TweetUrl": tweet_url,
                "FormattedText": text,
                "PredictedCategories": ",".join(classification_list),
            }

            if created_at_str_parsed is not None:
                enquiry["ReportedAt"] = created_at_str_parsed

            kv_str_list = [k + ": " + str(v) for k, v in enquiry.items()]
            request_payload["enquiryMessage"] = "\n".join(kv_str_list)
        else:
            message = {
                "message":
                text,
                "partnerId":
                partner_id,
                "query":
                source_information,
                "source":
                analyzer_response.source_name,
                "url":
                tweet_url,
                "userProfile":
                user_url,
                "sentiment":
                sentiment,
                "predictedCategories":
                ",".join(classification_list),
                "metadata":
                str(
                    json.dumps(analyzer_response.segmented_data,
                               ensure_ascii=False)),
                "originatedAt":
                created_at_str,
            }
            request_payload["messageDetail"] = str(
                json.dumps(message, ensure_ascii=False))

        return request_payload