Script 1019: Tagging dimension Script Campaigns Intent

Purpose

Python script to extract intent from campaign names and update the intent column in the output dataframe.

To Elaborate

The Python script solves the problem of extracting intent from campaign names and updating the intent column in the output dataframe. The script defines a list of intent keywords and uses a function to extract the intent from each campaign name. It then updates the intent column in the output dataframe with the extracted intent. Finally, it drops any rows with missing intent values from the output dataframe.

Walking Through the Code

  1. The script defines the configurable parameters, including the intent keywords and column names.
  2. The script defines a function called “extract_intent” that takes a campaign name as input and returns the extracted intent.
  3. The script creates a copy of the input dataframe as the output dataframe.
  4. The script loops through all rows in the input dataframe.
  5. For each row, it extracts the campaign name and calls the “extract_intent” function to get the intent.
  6. It prints the campaign name and intent.
  7. It updates the intent column in the output dataframe with the extracted intent.
  8. After the loop, it drops any rows with missing intent values from the output dataframe.
  9. If the output dataframe is not empty, it prints the tableized output dataframe. Otherwise, it prints “Empty outputDf”.

Vitals

  • Script ID : 1019
  • Client ID / Customer ID: 1306927457 / 60270313
  • Action Type: Bulk Upload
  • Item Changed: Campaign
  • Output Columns: Account, Campaign, Intent
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Autumn Archibald (aarchibald@marinsoftware.com)
  • Created by Autumn Archibald on 2024-04-29 19:27
  • Last Updated by Autumn Archibald on 2024-04-29 19:28
> See it in Action

Python Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Define the configurable parameters for the script
INTENT_KEYWORDS = ['tofu', 'mofu', 'bofu']
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_INTENT = 'Intent'

# Function to extract intent from campaign name
def extract_intent(campaign_name):
    campaign_lower = campaign_name.lower()
    for intent in INTENT_KEYWORDS:
        if intent in campaign_lower:
            return intent
    return ''

# Copy input rows to output
outputDf = inputDf.copy()

# Loop through all rows
for index, row in inputDf.iterrows():
    campaign_name = row[RPT_COL_CAMPAIGN]

    # Extract intent from campaign name
    intent = extract_intent(campaign_name)
    print("Campaign [%s] => Intent [%s]" % (campaign_name, intent))

    # Update intent column
    outputDf.at[index, BULK_COL_INTENT] = intent

# Drop any rows with missing intent values
outputDf = outputDf.dropna(subset=[BULK_COL_INTENT])

if not outputDf.empty:
    print("outputDf", tableize(outputDf))
else:
    print("Empty outputDf")

Post generated on 2024-05-15 07:44:05 GMT

comments powered by Disqus