Files
solveig_master/sustainability_word_list.ipynb
2023-04-13 18:20:32 +00:00

32 KiB
Raw Permalink Blame History

In [1]:
# load list of words related to sustainability
path = 'sustainability_words.txt'
word_list = list()
with open(path, 'r') as f:
    for line in f.readlines():
        word_list.append(line.replace('\n', ''))

word_list.sort()
print(word_list)
['CO2', 'CSR', 'ESG', 'SDG', 'bio', 'carb', 'challeng', 'chang', 'clean', 'climate', 'eco', 'emissions', 'environment', 'future', 'garbage', 'green', 'methanol', 'mission', 'neutral', 'ocean', 'planet', 'plastic', 'recycling', 'reduc', 'responsib', 'sulphur', 'sustainab', 'zero']
In [2]:
# load word counts from tweets
import pandas as pd

word_df = pd.read_csv('word_count.csv', index_col=0)

word_df
Out [2]:
word counts ratio
0 maersk 3207 0.04001
1 is 1025 0.01279
2 more 989 0.01234
3 here 825 0.01029
4 trade 642 0.00801
... ... ... ...
10920 ritzau 1 0.00001
10921 baan 1 0.00001
10922 indiabased 1 0.00001
10923 occurs 1 0.00001
10924 multicarrier 1 0.00001

10925 rows × 3 columns

In [3]:
# load ignore list
ignore_list = list()
path = 'ignore_words.txt'
with open(path, 'r') as f:
    for line in f.readlines():
        ignore_list.append(line.replace('\n', ''))
        
ignore_list.sort()
print(ignore_list)
['become', 'becomes', 'becoming', 'blueeconomy', 'changed', 'chinaeconomy', 'deconsolidation', 'decorated', 'decoupling', 'ecommerce', 'ecommercelogistics', 'economia', 'economic', 'economically', 'economicgrowth', 'economictimes', 'economies', 'economist', 'economistimpact', 'economy', 'environments', 'fairfuture4seafarers', 'mclean', 'portrecord', 'recognise', 'recognised', 'recognising', 'recognition', 'recognized', 'record', 'recorded', 'records', 'recovery', 'second', 'secondbusiest', 'secondgeneration', 'secondlargest', 'seconds', 'shetradesglobal', 'socioeconomic', 'telecoms', 'theeconomist', 'transoceanic', 'unchanged', 'worldrecord']
In [4]:
import numpy as np

match_word_list = list()
mean_idx_list = list()
sigma_idx_list = list()
for i, word in enumerate(word_list):
    # find index of matching words
    contains_word_mask = word_df['word'].str.contains(word.lower())==True
    counts_above_limit_mask = word_df['counts'].gt(1)
    match_mask = contains_word_mask & counts_above_limit_mask
    match_idx_list = word_df[match_mask].index.tolist()
    # remove indices of unwanted words
    remove_idx_list = list()
    for idx in match_idx_list:
        match_word = word_df.loc[idx, 'word']
        if match_word in ignore_list:
            remove_idx_list.append(idx)
    for idx in remove_idx_list:
        match_idx_list.remove(idx)
    # store matched words
    match_word_list.append(list())
    for idx in match_idx_list:
        match_word = word_df.loc[idx, 'word']
        match_word_list[i].append(match_word)
    # calculate mean and std of word ranking from located indices
    mu = np.array(match_idx_list).mean().round()
    mean_idx_list.append(mu)
    sigma = np.array(match_idx_list).std().round()
    sigma_idx_list.append(sigma)

# show example of results
idx = 0
print(word_list[idx])
print(match_word_list[idx])
print(mean_idx_list[idx])
print(sigma_idx_list[idx])
CO2
['co2', 'co2neutral', 'co2emission']
3390.0
2275.0
In [5]:
df = pd.DataFrame({
    'root': word_list,
    'mean_ranking': mean_idx_list,
    'std_ranking': sigma_idx_list,
    'word_matches': match_word_list
})

df
Out [5]:
root mean_ranking std_ranking word_matches
0 CO2 3390.0 2275.0 [co2, co2neutral, co2emission]
1 CSR 1371.0 0.0 [csr]
2 ESG 636.0 0.0 [esg]
3 SDG 423.0 0.0 [sdgs]
4 bio 3687.0 1274.0 [biofuel, biofuels, biohuts, biodiversity]
5 carb 1952.0 1526.0 [decarbonisation, carbon, decarbonization, car...
6 challeng 420.0 264.0 [challenges, challenge, challenging]
7 chang 2148.0 1667.0 [change, changing, climatechange, changes, exc...
8 clean 1695.0 837.0 [theoceancleanup, cleanup, clean, cleanseas, o...
9 climate 3046.0 2095.0 [climateaction, climate, climatechange, climat...
10 eco 3074.0 1269.0 [ecosystem, eco, maerskecodelivery, ecofriendl...
11 emissions 2925.0 2046.0 [emissions, carbonemissions, zeroemissions]
12 environment 2391.0 1574.0 [environment, environmental, unenvironment, en...
13 future 1770.0 1684.0 [future, futureproofing]
14 garbage 1808.0 612.0 [garbage, greatpacificgarbagepatch]
15 green 2393.0 1158.0 [green, greenfuels, greener, greenfuel, greent...
16 methanol 1700.0 1044.0 [methanol, emethanol]
17 mission 3247.0 1950.0 [emissions, mission, eucommission, carbonemiss...
18 neutral 2382.0 1798.0 [neutral, carbonneutral, neutrality, co2neutral]
19 ocean 2169.0 1629.0 [ocean, theoceancleanup, oceans, oceanplastic,...
20 planet 1295.0 0.0 [planet]
21 plastic 2320.0 1490.0 [plastic, oceanplastic, plasticwaste, plasticp...
22 recycling 860.0 154.0 [recycling, shiprecycling]
23 reduc 1242.0 965.0 [reduce, reducing, reduced, reduction, reducti...
24 responsib 1889.0 1135.0 [responsible, responsibility, responsibly, res...
25 sulphur 2828.0 1772.0 [sulphur, lowsulphur]
26 sustainab 1527.0 1873.0 [sustainability, sustainable, sustainableshipp...
27 zero 2977.0 1646.0 [zero, netzero, zerocarbon, zerocarbonship, ze...
In [6]:
# sort values by ranking
df = df.sort_values(by='mean_ranking').reset_index(drop=True)
df
Out [6]:
root mean_ranking std_ranking word_matches
0 challeng 420.0 264.0 [challenges, challenge, challenging]
1 SDG 423.0 0.0 [sdgs]
2 ESG 636.0 0.0 [esg]
3 recycling 860.0 154.0 [recycling, shiprecycling]
4 reduc 1242.0 965.0 [reduce, reducing, reduced, reduction, reducti...
5 planet 1295.0 0.0 [planet]
6 CSR 1371.0 0.0 [csr]
7 sustainab 1527.0 1873.0 [sustainability, sustainable, sustainableshipp...
8 clean 1695.0 837.0 [theoceancleanup, cleanup, clean, cleanseas, o...
9 methanol 1700.0 1044.0 [methanol, emethanol]
10 future 1770.0 1684.0 [future, futureproofing]
11 garbage 1808.0 612.0 [garbage, greatpacificgarbagepatch]
12 responsib 1889.0 1135.0 [responsible, responsibility, responsibly, res...
13 carb 1952.0 1526.0 [decarbonisation, carbon, decarbonization, car...
14 chang 2148.0 1667.0 [change, changing, climatechange, changes, exc...
15 ocean 2169.0 1629.0 [ocean, theoceancleanup, oceans, oceanplastic,...
16 plastic 2320.0 1490.0 [plastic, oceanplastic, plasticwaste, plasticp...
17 neutral 2382.0 1798.0 [neutral, carbonneutral, neutrality, co2neutral]
18 environment 2391.0 1574.0 [environment, environmental, unenvironment, en...
19 green 2393.0 1158.0 [green, greenfuels, greener, greenfuel, greent...
20 sulphur 2828.0 1772.0 [sulphur, lowsulphur]
21 emissions 2925.0 2046.0 [emissions, carbonemissions, zeroemissions]
22 zero 2977.0 1646.0 [zero, netzero, zerocarbon, zerocarbonship, ze...
23 climate 3046.0 2095.0 [climateaction, climate, climatechange, climat...
24 eco 3074.0 1269.0 [ecosystem, eco, maerskecodelivery, ecofriendl...
25 mission 3247.0 1950.0 [emissions, mission, eucommission, carbonemiss...
26 CO2 3390.0 2275.0 [co2, co2neutral, co2emission]
27 bio 3687.0 1274.0 [biofuel, biofuels, biohuts, biodiversity]
In [7]:
# save results
df['mean_ranking'] = df['mean_ranking'].astype(int)
df['std_ranking'] = df['std_ranking'].astype(int)
df.to_csv('word_root_ranking.csv')
In [ ]: