File size: 6,270 Bytes
79e1719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6811882
79e1719
 
6811882
79e1719
 
6811882
79e1719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import pandas as pd
import os
import nltk
from nltk.corpus import stopwords
import plotly.express as px
from collections import Counter
import re
import matplotlib.pyplot as plt
from wordcloud import WordCloud

place_mapping = {
    'united states': 'United States',
    'u.s.': 'United States',
    'US': 'United States',
    'america': 'United States',
    'north america': 'North America',
    'usa': 'United States',
    'south america': 'South America',
    'american': 'United States',
    'europe': 'Europe',
    'eu': 'Europe',
    'china': 'China',
    'chinese': 'China',
    'russia': 'Russia',
    'arab': 'Arab Countries',
    'middle east': 'Middle East',
    'asia': 'Asia',
    'asian': 'Asia',
    'spain': 'Spain',
    'germany': 'Germany',
    'france': 'France',
    'uk': 'United Kingdom',
    'britain': 'United Kingdom',
    'canada': 'Canada',
    'mexico': 'Mexico',
    'brazil': 'Brazil',
    'venezuela': 'Venezuela',
    'angola': 'Angola',
    'nigeria': 'Nigeria',
    'libya': 'Libya',
    'iraq': 'Iraq',
    'iran': 'Iran',
    'kuwait': 'Kuwait',
    'qatar': 'Qatar',
    'saudi arabia': 'Saudi Arabia',
    'gcc': 'Gulf Cooperation Council',
    'asia-pacific': 'Asia',
    'southeast asia': 'Asia',
    'latin america': 'Latin America',
    'caribbean': 'Caribbean',
}

region_mapping = {
    'North America': ['United States', 'Canada', 'Mexico'],
    'South America': ['Brazil', 'Venezuela'],
    'Europe': ['United Kingdom', 'Germany', 'France', 'Spain', 'Russia'],
    'Asia': ['China', 'India', 'Japan', 'South Korea'],
    'Middle East': ['Saudi Arabia', 'Iran', 'Iraq', 'Qatar', 'Kuwait'],
    'Africa': ['Nigeria', 'Libya', 'Angola'],
    # Add more regions as necessary
}


nomenclature_mapping = {
    'petroleum': 'Petroleum',
    'energy': 'Energy',
    'fuel oil': 'Fuel Oil',
    'shale': 'Shale',
    'offshore': 'Offshore',
    'upstream': 'Upstream',
    'hsfo': 'HSFO',
    'downstream': 'Downstream',
    'crude oil': 'Crude Oil',
    'crude' : 'Crude Oil',
    'refinery': 'Refinery',
    'oil field': 'Oil Field',
    'drilling': 'Drilling',
    'gas': 'Gas',
    'liquefied natural gas': 'LNG',
    'natural gas': 'NG',
    'oil': 'Crude Oil',
}

company_mapping = {
    'exxonmobil': 'ExxonMobil',
    'exxon': 'ExxonMobil',
    'chevron': 'Chevron',
    'bp': 'BP',
    'british petroleum': 'BP',
    'shell': 'Shell',
    'total energies': 'TotalEnergies',
    'conoco': 'ConocoPhillips',
    'halliburton': 'Halliburton',
    'slb': 'SLB',
    'schlumberger': 'SLB',
    'devon': 'Devon Energy',
    'occidental': 'Occidental Petroleum',
    'marathon': 'Marathon Oil',
    'valero': 'Valero Energy',
    'aramco': 'Aramco',
}

nltk.download('stopwords')

stop_words = set(stopwords.words('english'))


# Function to clean, tokenize, and remove stopwords
def tokenize(text):

    text = re.sub(r'[^\w\s]', '', text.lower())
    words = text.split()

    mapped_words = []
    for word in words:
        mapped_word = place_mapping.get(word, 
                        nomenclature_mapping.get(word, 
                        company_mapping.get(word, word)))
        mapped_words.append(mapped_word)

    filtered_words = [word for word in mapped_words if word not in stop_words]
    return filtered_words


# Function to apply filtering and plotting based on search input
def generateChartBar(data, search_word, body=False):

    # filtered_df = data[data['headline'].str.contains(search_word, case=False) | data['body'].str.contains(search_word, case=False)]
    
    all_words = []
    data['headline'].apply(lambda x: all_words.extend(tokenize(x)))

    if body:
        data['body'].apply(lambda x: all_words.extend(tokenize(x)))
    

    word_counts = Counter(all_words)
    top_10_words = word_counts.most_common(20)
    top_10_df = pd.DataFrame(top_10_words, columns=['word', 'frequency'])

    fig = px.bar(top_10_df, x='word', y='frequency', title=f'Top 20 Most Common Words (Excluding Stopwords) for "{search_word}"',
                 labels={'word': 'Word', 'frequency': 'Frequency'}, 
                 text='frequency')
    
    return fig

# Function to filter based on the whole word/phrase and region
def filterPlace(data, search_place):
    # Check if the search_place is a region
    if search_place in region_mapping:
        # Get all countries in the region
        countries_in_region = region_mapping[search_place]
        # Map countries to their place_mapping synonyms
        synonyms_pattern = '|'.join(
            r'\b{}\b'.format(re.escape(key))
            for country in countries_in_region
            for key in place_mapping
            if place_mapping[key] == country
        )
    else:
        # If a country is selected, get its standard place and synonyms
        standard_place = place_mapping.get(search_place.lower(), search_place)
        synonyms_pattern = '|'.join(
            r'\b{}\b'.format(re.escape(key)) 
            for key in place_mapping 
            if place_mapping[key] == standard_place
        )
    
    # Filter the DataFrame for headlines or body containing the whole word/phrase
    filtered_df = data[
        data['headline'].str.contains(synonyms_pattern, case=False, na=False) | 
        data['body'].str.contains(synonyms_pattern, case=False, na=False)
    ]
    
    if filtered_df.empty:
        print(f'No data found for {search_place}. Please try a different location or region.')
        return None
    
    return filtered_df

# Function to filter DataFrame and generate a word cloud
def generateWordCloud(data):

    # standard_place = place_mapping.get(search_place.lower(), search_place)
    # synonyms_pattern = '|'.join(re.escape(key) for key in place_mapping if place_mapping[key] == standard_place)
    
    # filtered_df = data[data['headline'].str.contains(synonyms_pattern, case=False, na=False) | 
    #                  data['body'].str.contains(synonyms_pattern, case=False, na=False)]
    
    # if filtered_df.empty:
    #     print(f'No data found for {search_place}. Please try a different location.')
    #     return
    
    text = ' '.join(data['headline'].tolist() + data['body'].tolist())
    wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
    
 
    return wordcloud