File size: 12,428 Bytes
b0037ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de0e247
b0037ca
 
 
 
 
 
 
 
 
 
ac1c090
 
 
 
 
 
b0037ca
0ac0e1a
b0037ca
5c64186
b0037ca
 
 
 
5c64186
b0037ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0721e7
b0037ca
37d49c8
86a62ab
37d49c8
86a62ab
c16c5c3
37d49c8
 
 
 
 
86a62ab
81272dc
37d49c8
 
de0e247
d0721e7
37d49c8
 
b0037ca
86a62ab
37d49c8
d0721e7
 
 
 
 
 
 
 
5a2b249
 
 
 
ac1c090
86a62ab
 
ac1c090
86a62ab
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import streamlit as st
import itertools
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import pandas as pd
import numpy as np
from collections import defaultdict

# text = """Economy
# Labour Party: Aim to deliver economic stability through growth and tough spending rules? (The Speaker)?.
# Conservative Party: Focus on economic growth by reducing debt and creating jobs? (theweek)?? (The Speaker)?.
# Liberal Democrats: Promote a fair and innovative economy with opportunities for all? (The Speaker)?.
# Scottish National Party (SNP): Advocate for full tax devolution to boost the Scottish economy? (theweek)?.
# Green Party: Invest in a green economic transformation to create sustainable jobs? (The Speaker)?.
# Reform UK: Aim to stimulate growth by reducing taxes and government spending? (theweek)?? (The Speaker)?.
# Taxation
# Labour Party: Propose to raise funds by taxing private schools, clamping down on tax underpayment, and a windfall tax on oil and gas companies? (theweek)?.
# Conservative Party: Plan to cut National Insurance and maintain lower personal taxes? (theweek)?.
# Liberal Democrats: Advocate for fair taxation and investments in public services? (The Speaker)?.
# Scottish National Party (SNP): Support windfall taxes on businesses and full tax power devolution? (theweek)?.
# Green Party: Propose new taxes on assets worth over £10 million and carbon tax? (The Speaker)?.
# Reform UK: Plan to raise the income tax threshold, scrap inheritance tax on smaller estates, and cut VAT on energy bills? (theweek)?? (The Speaker)?.
# Debt
# Labour Party: Aim to keep national debt under control through economic growth? (The Speaker)?.
# Conservative Party: Focus on reducing national debt as a key priority? (The Speaker)?.
# Liberal Democrats: Promote a sustainable approach to managing national debt? (The Speaker)?.
# Scottish National Party (SNP): Push for additional funding and economic autonomy to manage debt better? (theweek)?.
# Green Party: Invest heavily in green initiatives while aiming to manage debt sustainably? (The Speaker)?.
# Reform UK: Plan to reduce government spending to manage and reduce debt? (theweek)?.
# Inflation
# Labour Party: Aim to keep inflation low through economic stability measures? (The Speaker)?.
# Conservative Party: Prioritize halving inflation as a main economic goal? (The Speaker)?.
# Liberal Democrats: Focus on maintaining low inflation through fair economic policies? (The Speaker)?.
# Scottish National Party (SNP): Call for more funding to address inflationary pressures? (theweek)?.
# Green Party: Promote economic policies that balance environmental investment with controlling inflation? (The Speaker)?.
# Reform UK: Aim to lower inflation by cutting taxes and reducing energy costs? (theweek)?.
# Healthcare
# Labour Party: Plan to reduce NHS waiting times and invest in health infrastructure? (The Speaker)?.
# Conservative Party: Focus on cutting NHS waiting lists and improving healthcare services? (The Speaker)?.
# Liberal Democrats: Promise to improve access to GPs and emergency dental care? (theweek)?? (The Speaker)?.
# Scottish National Party (SNP): Demand increased NHS funding and better healthcare services? (theweek)?.
# Green Party: Propose major NHS investment and immediate pay rises for NHS staff? (The Speaker)?.
# Reform UK: Aim to achieve zero waiting lists and exempt healthcare workers from basic income tax? (theweek)?.
# Energy
# Labour Party: Establish Great British Energy, a new publicly-owned clean power company? (The Speaker)?.
# Conservative Party: Continue investing in energy security and diversification? (theweek)?.
# Liberal Democrats: Invest in renewable energy to generate 80% of electricity from renewables by the next decade? (The Speaker)?.
# Scottish National Party (SNP): Support re-entry into the EU single market to boost energy trade? (theweek)?.
# Green Party: Push for a green energy transformation, nationalizing major energy companies? (The Speaker)?.
# Reform UK: Increase drilling for gas and oil, scrapping net-zero plans to lower energy bills? (theweek)?.
# Environment
# Labour Party: Aim to make Britain a clean energy superpower? (The Speaker)?.
# Conservative Party: Continue current environmental policies with a focus on balanced growth? (theweek)?.
# Liberal Democrats: Promote a flourishing environment with fair access to nature? (The Speaker)?.
# Scottish National Party (SNP): Advocate for stronger environmental protections and policies? (theweek)?.
# Green Party: Focus on extensive environmental legislation and investment in green infrastructure? (The Speaker)?.
# Reform UK: Oppose current green policies and focus on traditional energy sources? (theweek)?.
# Security
# Labour Party: Strengthen border security and crack down on antisocial behavior? (The Speaker)?.
# Conservative Party: Implement mandatory national service and continue the Rwanda immigration scheme? (The Speaker)?.
# Liberal Democrats: Ensure fair access to security and justice for all? (The Speaker)?.
# Scottish National Party (SNP): Focus on unifying Scotland's security policies and practices? (The Speaker)?.
# Green Party: Promote community-based security measures and social justice? (The Speaker)?.
# Reform UK: Establish a new department for immigration and enhance border security? (theweek)?.
# Education
# Labour Party: Recruit 6,500 new teachers and improve educational infrastructure? (The Speaker)?.
# Conservative Party: Replace A-Levels with a new ‘Advanced British Standard’ and ensure continuous math education until age 18? (theweek)?? (The Speaker)?.
# Liberal Democrats: Increase school and college funding per pupil above the rate of inflation and lower the voting age to 16? (theweek)?? (The Speaker)?.
# Scottish National Party (SNP): Focus on improving access to quality education and advocating for full devolution of educational powers? (theweek)?.
# Green Party: Support comprehensive educational reforms including increased funding and nationalizing education services? (The Speaker)?.
# Reform UK: Reform educational standards, introduce patriotic curriculum, and eliminate "woke" ideologies from schools? (theweek)?? (The Speaker)?.
# Pensions
# Labour Party: No specific changes to pensions mentioned in the available manifestos? (The Speaker)?.
# Conservative Party: Maintain the triple lock system for pensions, ensuring they rise with inflation, wage growth, or 2.5%, whichever is highest? (theweek)?? (The Speaker)?.
# Liberal Democrats: No specific changes to pensions mentioned in the available manifestos? (The Speaker)?.
# Scottish National Party (SNP): Demand full devolution of pension powers to Scotland? (theweek)?.
# Green Party: No specific changes to pensions mentioned in the available manifestos? (The Speaker)?.
# Reform UK: No specific changes to pensions mentioned in the available manifestos? (theweek)?.
# Housing
# Labour Party: Commit to building more affordable housing and upgrading existing homes? (The Speaker)?.
# Conservative Party: Build 1.6 million new homes and support first-time buyers with government-backed mortgages? (theweek)?? (The Speaker)?.
# Liberal Democrats: Build 380,000 new homes annually, including 150,000 social housing units? (theweek)?? (The Speaker)?.
# Scottish National Party (SNP): Focus on increasing spending to provide more social housing in Scotland? (theweek)?.
# Green Party: Provide 150,000 new social homes every year and improve home insulation? (The Speaker)?.
# Reform UK: No specific housing policies mentioned in the available manifestos? (theweek)?? (The Speaker)?.
# Immigration
# Labour Party: Strengthen border security and crack down on illegal immigration? (The Speaker)?.
# Conservative Party: Continue with the Rwanda immigration scheme and introduce mandatory national service? (theweek)?? (The Speaker)?.
# Liberal Democrats: Provide safe and legal routes for asylum seekers and reform the immigration system to be merit-based? (theweek)?? (The Speaker)?.
# Scottish National Party (SNP): Advocate for humane immigration policies and support rejoining the EU single market? (theweek)?.
# Green Party: Support fair immigration policies and provide better support for asylum seekers? (The Speaker)?.
# Reform UK: Implement a four-point plan to curb illegal immigration and leave the European Convention on Human Rights? (theweek)?? (The Speaker)?."""



# data = []

# for line in text.split('\n'):
#     new_theme = line.strip().split()
#     if len(new_theme) == 1:
#         theme = new_theme[0]
#         continue
#     party, position = line.strip().split(': ')
#     position = position.split('?')[0]
#     data.append([theme,party,position])
# data = pd.DataFrame(data,columns='theme party position'.split()).set_index(['theme','party'])['position'].unstack()
# data.to_parquet('./data.parquet')
data = pd.read_parquet('./data.parquet')

# # Load a pre-trained model
# model = SentenceTransformer('all-MiniLM-L6-v2')

# # Define your sentences
# sentences = data.stack().to_list()

# # Generate embeddings
# embeddings = model.encode(sentences)

# embeddings = pd.DataFrame([[embedding] for embedding in embeddings],index=data.stack().index,columns=['embedding'])['embedding'].unstack()
# embeddings.to_parquet('./embeddings.parquet')
embeddings = pd.read_parquet('./embeddings.parquet')

# similarities = []
# for theme in embeddings.index:
#     print(theme)
#     for p1,p2 in itertools.combinations(embeddings.columns,2):
#         similarity = cosine_similarity([embeddings.loc[theme,p1]], [embeddings.loc[theme,p2]])[0][0]
#         similarities.append([theme,p1,p2,similarity])
#         similarities.append([theme,p2,p1,similarity])
    

# similarities = pd.DataFrame(similarities,columns = 'theme party1 party2 similarity'.split()).set_index('theme party1 party2'.split())['similarity'].reset_index()
# similarities.to_parquet('./similarities.parquet')
# similarities = pd.read_parquet('./similarities.parquet')
                    
# similarities.loc[('Debt',slice(None),slice(None))].reset_index()





st.title("Do you know your political parties?")


st.write(' - We gathered the political parties manisfestos from ChatGPT')
st.write(' - Create an embedding of their pledge on different matters')
st.write(' - Visualise how different they truly are on a PCA projection')
st.write('Soon extended to US parties/candidates, to make it more general')
st.divider()

parties = st.multiselect(
    "Which parties do you want to compare?",
    list(data.columns),
    list(np.random.choice(data.columns,4,replace=False)))

themes = st.multiselect(
    "Which themes matter to you?",
    list(data.index),
    list(np.random.choice(data.index,4,replace=False)))



subset = embeddings.loc[themes,parties]
X = np.array([array for array in subset.T.apply(lambda l: np.concatenate(l).ravel(), axis=1)])

from sklearn.manifold import Isomap
from sklearn.decomposition import PCA

model = PCA(n_components=2)
projection = model.fit_transform(X)


import plotly.express as px


projection = pd.DataFrame(projection,columns=['x','y'])
projection['party'] = parties
projection['color'] = parties
ps = []
pss = dict()
for party,pledges in data.loc[themes,parties].items():
    # st.subheader()
    pss[f'{party} pledges :'] = []
    
    m = f'{party} pledges :'
    for theme, t in pledges.items():
        if not t.startswith('No specific'):
            m += f'<br>  - {theme}: {t}'
            # st.write(f'  - {theme}: {t}')
        pss[f'{party} pledges :'].append(f'  - {theme}: {t}')
    ps.append(m)
projection['manifesto'] = ps

fig = px.scatter(projection,x='x', y='y', text='party', color='party', hover_data=["manifesto"])
fig.update_traces(marker=dict(size=30,
                              line=dict(width=5,
                                        color='DarkSlateGrey')),
                  selector=dict(mode='markers')
                  )

fig.update_layout(legend=dict(
    orientation="h",
    yanchor="bottom",
    y=1.02,
    xanchor="right",
    x=1
))


fig.update_xaxes(visible=False, showticklabels=False)
fig.update_yaxes(visible=False, showticklabels=False)
st.divider()
st.subheader('This is how similar the parties are, based on your interests')
st.plotly_chart(fig, config={'displayModeBar': False})
st.divider()
for t,ts in pss.items():
    st.subheader(t)
    for l in ts:
        st.write(l)