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
 
 
2ccab26
 
0b77e4b
 
9fde5ad
 
 
 
 
f7fa719
2ccab26
f7fa719
 
 
9fde5ad
 
 
 
 
 
 
e0d04d6
7406800
e0d04d6
 
 
 
 
 
f7fa719
83e1499
f7fa719
d3bc946
0b77e4b
83e1499
 
 
 
 
 
 
 
 
2ccab26
 
83e1499
 
 
 
 
 
 
0b77e4b
83e1499
 
 
 
 
 
 
 
 
 
 
 
0b77e4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
##packages code
from shapely.geometry import Point
import pandas as pd
from tqdm import tqdm
import streamlit as st
import numpy as np
import json, requests 
import pandas as pd
#from pandas.io.json import json_normalize

import matplotlib.pyplot as plt
import seaborn as sns
from math import radians, cos, sin, asin, sqrt

#import spacy
#!python -m spacy download en_core_web_lg


from sentence_transformers import SentenceTransformer, util

@st.cache_resource
def model_nlp():
    model = SentenceTransformer('all-MiniLM-L6-v2')
    return model

import unicodedata

from metaphone import doublemetaphone
from fuzzywuzzy import fuzz
from difflib import SequenceMatcher
import re

import geopandas as gpd
from geopandas import GeoDataFrame

path = "Climate_site/python_scripts/"


@st.cache_data  # 👈 Add the caching decorator
def load_data():
    url = path + "institutions.tsv"
    dic = pd.read_csv(url, delimiter = "\t" , index_col = 1).to_dict('index')
    return dic

dic_institutions = load_data()


#################### General Functions #############################

def URL(base_URL , entity_type , filters):
    url = base_URL + entity_type + filters 
    return url


def get_data(url):
    url = requests.get(url)
    text = url.text
    import json
    data = json.loads(text)
    return data

## encoding the abstract


def reconstruction_abstract(abstract_inverted_index):
    # return the abstract is the abstract exists in the database, else, return None
    
    if abstract_inverted_index != None:
        
        list_values = list(abstract_inverted_index.values())
        list_keys = list(abstract_inverted_index.keys())
        #from the words in the abstract (keys of abstract_inverted_index) and their position in the text (values of abstract_inverted_index), reconstruct the abstract
        
        size_abstract = max([ max(elem) for elem in abstract_inverted_index.values() ] )
        
        abstract = [""]*(size_abstract +1)
        
        for i in range(len(list_values)):
            for pos in list_values[i]:
                abstract[pos] = list_keys[i]
        
        return " ".join(list(abstract))
    
    else:
        return None
            
    
## calculate efficiently the dot product between two vectors

def norm(vector):
    return np.sqrt(sum(x * x for x in vector))    

def cosine_similarity2(vec_a, vec_b):
        norm_a = norm(vec_a)
        norm_b = norm(vec_b)
        dot = sum(a * b for a, b in zip(vec_a, vec_b))
        return dot / (norm_a * norm_b)




#################### Paper Functions #############################

def related_papers_own_research(  research_key_words , display  ):

    dic = {}

    max_count = 0
    base_URL_OA = f'https://api.openalex.org/'
    filter_works = f'works?'
    
        

    filter_openalex = f"search=" + research_key_words + "&per_page=200&mailto=emma_scharfmann@berkeley.edu"
        
        
    filter_openalex = filter_openalex.replace(" " , "%20")
    
    url = URL(base_URL_OA , filter_works, filter_openalex) 
    data = get_data(url)
    count = data["meta"]["count"] 



    if display == True:
        print(  data["meta"]["count"] , [elem[i] for i in range(1,len(elem))]  )
        print(url)
    
    for i in range(len(data["results"])):
        dic[ data["results"][i]["id"]] = {}
        dic[ data["results"][i]["id"]]["title"] = data["results"][i]["title"]
        dic[ data["results"][i]["id"]]["abstract"] = reconstruction_abstract(data["results"][i]["abstract_inverted_index"])
        dic[ data["results"][i]["id"]]["concepts"] = data["results"][i]["concepts"]
        dic[ data["results"][i]["id"]]["date"] = data["results"][i]["publication_date"]
        dic[ data["results"][i]["id"]]["authorships"] = data["results"][i]["authorships"]
        dic[ data["results"][i]["id"]]["cited_by_count"] = data["results"][i]["cited_by_count"]

        if len(data["results"][i]["authorships"]) > 0:
            if data["results"][i]["authorships"][0]["institutions"] != []:
                dic[ data["results"][i]["id"]]["countries"] = data["results"][i]["authorships"][0]["institutions"][0]["country_code"]
                dic[ data["results"][i]["id"]]["institutions"] = data["results"][i]["authorships"][0]["institutions"][0]["display_name"]
            else:
                dic[ data["results"][i]["id"]]["countries"] = ""
                dic[ data["results"][i]["id"]]["institutions"] = ""
            dic[ data["results"][i]["id"]]["authors"] = data["results"][i]["authorships"][0]["author"]["display_name"]

            for j in range(1 , len(data["results"][i]["authorships"])):
                if data["results"][i]["authorships"][j]["institutions"] != []:
                    dic[ data["results"][i]["id"]]["institutions"] += ", " + data["results"][i]["authorships"][j]["institutions"][0]["display_name"]
                    dic[ data["results"][i]["id"]]["countries"] = data["results"][i]["authorships"][j]["institutions"][0]["country_code"]
                dic[ data["results"][i]["id"]]["authors"] += ", " + data["results"][i]["authorships"][j]["author"]["display_name"]

    if display == True: 
        print(" ")
        
    return dic
        
    
        

## Ranking Papers

def ranking_own_research(research_key_words,  details  , display):
    
    model = model_nlp()
    
    dic_scores_papers = {}
    dic = related_papers_own_research( research_key_words , False)
    
    reference_text = details
        
    if display== True:
        print("Technology details: " , reference_text)
        print(" ")
    encoded_text = model.encode(reference_text, convert_to_tensor=False , show_progress_bar = False).tolist()
    

    
    for ids in list(dic.keys()):

        dic_scores_papers[ids] = {}
        
        if dic[ids]["title"] != None:
            encoded_title = model.encode(dic[ids]["title"], convert_to_tensor=False , show_progress_bar = False).tolist()
            score_title = cosine_similarity2(encoded_title, encoded_text)
        else:
            score_title = None

    

        if dic[ids]["abstract"] != None:
            encoded_abstract = model.encode(dic[ids]["abstract"], convert_to_tensor=False , show_progress_bar = False).tolist()
            score_abstract = cosine_similarity2(encoded_abstract, encoded_text)
        else:
            score_abstract = None


        #concepts = ""
        #for elem in dic[ids]["concepts"]:
        #    concepts += elem["display_name"] + " "
        #encoded_concepts = model.encode(concepts, convert_to_tensor=False).tolist()
        #score_concepts = cosine_similarity2(encoded_concepts, encoded_text)

        dic_scores_papers[ids]["title comparison"] = score_title
        dic_scores_papers[ids]["abstract comparison"] = score_abstract
        #dic_scores_papers[ids]["concepts comparision"] = score_concepts
        
        dic_scores_papers[ids]["title"] = dic[ids]["title"]
        dic_scores_papers[ids]["citations"] = dic[ids]["cited_by_count"]
        

        dic_scores_papers[ids]["date"] = dic[ids]["date"][:4]
        if "institutions" in dic[ids]:
            dic_scores_papers[ids]["institutions"] = dic[ids]["institutions"]
        #    dic_scores_papers[ids]["countries"] = dic[ids]["countries"]
            
        else:
            dic_scores_papers[ids]["institutions"] = None
        #    dic_scores_papers[ids]["countries"] = None
        
        if "authors" in dic[ids]:
            dic_scores_papers[ids]["number of co-authors"] = len(dic[ids]["authors"].split(","))
            dic_scores_papers[ids]["authors"] = dic[ids]["authors"]

    return dic , dic_scores_papers
    



    
    
    
def get_ranking_own_research(research_key_words,  details  , display , size):
    
    dic , dic_scores_papers = ranking_own_research(research_key_words,  details  , display)
    
    if dic_scores_papers == {}:
        return "No paper found"
    
    elif type(dic_scores_papers) == str:
        return dic_scores_papers
    
    else:
        return pd.DataFrame(dic_scores_papers).T.sort_values(by="abstract comparison" , ascending = False).head(size)
     
      
 
          
    


def extract_quantitative_data_paper(work_id):

    url = "https://api.openalex.org/works/" + str(work_id)
    url_google = "https://explore.openalex.org/works/" + str(work_id)
    
    data = get_data(url)
    date = data["publication_date"]
    title = data["title"]
    abstract = reconstruction_abstract(data["abstract_inverted_index"])
    concepts = ", ".join( [elem["display_name"] for elem in data["concepts"]] )
    authors = ", ".join( [elem["author"]["display_name"] for elem in data["authorships"]] )
    institutions = ", ".join( set([elem["institutions"][0]["display_name"] for elem in data["authorships"] if len(elem["institutions"]) > 0]) )
    return url_google , title , abstract , date , authors , institutions
            

    


## Mapping the authors

#merge the nobiliary particles with the last name
#ln_suff file can be modified if more or less nobiliary particles want to be suppressed

ln_suff = ['oster',
 'nordre',
 'vaster',
 'aust',
 'vesle',
 'da',
 'van t',
 'af',
 'al',
 'setya',
 'zu',
 'la',
 'na',
 'mic',
 'ofver',
 'el',
 'vetle',
 'van het',
 'dos',
 'ui',
 'vest',
 'ab',
 'vste',
 'nord',
 'van der',
 'bin',
 'ibn',
 'war',
 'fitz',
 'alam',
 'di',
 'erch',
 'fetch',
 'nga',
 'ka',
 'soder',
 'lille',
 'upp',
 'ua',
 'te',
 'ni',
 'bint',
 'von und zu',
 'vast',
 'vestre',
 'over',
 'syd',
 'mac',
 'nin',
 'nic',
 'putri',
 'bet',
 'verch',
 'norr',
 'bath',
 'della',
 'van',
 'ben',
 'du',
 'stor',
 'das',
 'neder',
 'abu',
 'degli',
 'vre',
 'ait',
 'ny',
 'opp',
 'pour',
 'kil',
 'der',
 'oz',
 'von',
 'at',
 'nedre',
 'van den',
 'setia',
 'ap',
 'gil',
 'myljom',
 'van de',
 'stre',
 'dele',
 'mck',
 'de',
 'mellom',
 'mhic',
 'binti',
 'ath',
 'binte',
 'snder',
 'sre',
 'ned',
 'ter',
 'bar',
 'le',
 'mala',
 'ost',
 'syndre',
 'sr',
 'bat',
 'sndre',
 'austre',
 'putra',
 'putera',
 'av',
 'lu',
 'vetch',
 'ver',
 'puteri',
 'mc',
 'tre',
 'st']

#suppress all the unwanted suffixes from a string. 
#name_del file can be modified if more or less suffixes want to be suppressed 

name_del = ['2nd', '3rd', 'Jr', 'Jr.', 'Junior', 'Sr', 'Sr.', 'Senior']


def name_delete(string):
    for elmt in name_del:
        if f" {elmt}" in string:
            return string.replace(f" {elmt}","")
    return string

def ln_suff_merge(string):
    for suff in ln_suff:
        if f" {suff} " in string or string.startswith(f"{suff} "):
            return string.replace(f"{suff} ",suff.replace(" ",""))
    return string

#normalize a string dat that represents often a name. 

def normalize(data):
    normal = unicodedata.normalize('NFKD', data).encode('ASCII', 'ignore')
    val = normal.decode("utf-8")
    # delete unwanted elmt
    val = name_delete(val)
    # lower full name in upper
    val = re.sub(r"[A-Z]{3,}", lambda x: x.group().lower(), val)
    # add space in front of upper case
    val = re.sub(r"(\w)([A-Z])", r"\1 \2", val)
    # Lower case
    val = val.lower()
    # remove special characters
    val = re.sub('[^A-Za-z0-9 ]+', ' ', val)
    # remove multiple spaces
    val = re.sub(' +', ' ', val)
    # remove trailing spaces
    val = val.strip()
    # suffix merge
    val = ln_suff_merge(val)

    return val


def main_authors( research_key_words  , details ,   size):
        
    dic_papers , dic_papers_ranked = ranking_own_research(  research_key_words  , details ,  False  )
    
    dic_papers_co_authors = {}
        
    for paper in list(dic_papers_ranked.keys())[:size]:

        for k in range(len(dic_papers[paper]["authorships"])):
            coauthor_id = dic_papers[paper]["authorships"][k]["author"]["id"]
            
            
            author_name = dic_papers[paper]["authorships"][k]["author"]["display_name"]
            author_name_norm = normalize(dic_papers[paper]["authorships"][k]["author"]["display_name"]).split()
            if len (author_name_norm) > 0:
                
                author_name_norm = author_name_norm[0] + " " + author_name_norm[-1]

                if author_name_norm not in dic_papers_co_authors:
                    dic_papers_co_authors[author_name_norm] = {}
                    dic_papers_co_authors[author_name_norm]["Author's name(s)"] = author_name
                    if coauthor_id != None:
                        dic_papers_co_authors[author_name_norm]["Author's id(s)"] = coauthor_id[21:]
                    else:
                        dic_papers_co_authors[author_name_norm]["Author's id(s)"] = ""
                    dic_papers_co_authors[author_name_norm]["Number of occurence within the " + str(size) + " most related papers"] = 1
                    dic_papers_co_authors[author_name_norm]["Number of related citations"] = dic_papers[paper]["cited_by_count"]
                else:
                    dic_papers_co_authors[author_name_norm]["Number of occurence within the " + str(size) + " most related papers"] += 1
                    if author_name not in dic_papers_co_authors[author_name_norm]["Author's name(s)"]:
                        dic_papers_co_authors[author_name_norm]["Author's name(s)"] += ", " + author_name
                    if coauthor_id != None and coauthor_id[21:] not in dic_papers_co_authors[author_name_norm]["Author's id(s)"]:                                                              
                        dic_papers_co_authors[author_name_norm]["Author's id(s)"] += ", " + coauthor_id[21:]
                        dic_papers_co_authors[author_name_norm]["Number of related citations"] += dic_papers[paper]["cited_by_count"]



    if dic_papers_co_authors != {}:
        
        for author_name_norm in list(dic_papers_co_authors.keys()):
            list_ids = dic_papers_co_authors[author_name_norm]["Author's id(s)"].split(", ")
            work_count = 0
            cited_by_count = 0
            institutions = ''
            institution_id = None
            
            for elem in list_ids:
                if len(elem) > 3:

                    try:
                        data = get_data("https://api.openalex.org/people/" + elem)
                        work_count += data["works_count"]
                        cited_by_count += data["cited_by_count"]
                        
                        
                        if data["last_known_institution"] != None and data["last_known_institution"]["id"] != None:
                            institution_id = data["last_known_institution"]["id"][21:]
                        
                        if data["last_known_institution"] != None and data["last_known_institution"]["display_name"] != None:
                            if institutions == '':
                                institutions += data["last_known_institution"]["display_name"]
                            else:
                                institutions += ", " + data["last_known_institution"]["display_name"]
                    except:
                        pass

            if work_count == 0 or work_count > 10000:
                dic_papers_co_authors.pop(author_name_norm)
            else:
                dic_papers_co_authors[author_name_norm]["Last Known Institution"] = institutions
                dic_papers_co_authors[author_name_norm]["Number of works"] = work_count
                dic_papers_co_authors[author_name_norm]["Number of citations"] = cited_by_count
                dic_papers_co_authors[author_name_norm]["Institution_id"] = institution_id

        

        dic_papers_co_authors = {k: v for k, v in sorted(dic_papers_co_authors.items(), key=lambda item: item[1]["Number of occurence within the " + str(size) + " most related papers"] , reverse = True)[:size]}


        dic_papers_map = {}
        count = 0
        
    
        for author_name_norm in dic_papers_co_authors:
        
            institution_id = dic_papers_co_authors[author_name_norm]["Institution_id"]
            if institution_id in dic_institutions:
                data = dic_institutions[institution_id]
                
                dic_papers_map[count] = {}
                dic_papers_map[count]["longitude"] = data["longitude"]
                dic_papers_map[count]["latitude"] = data["latitude"]
                dic_papers_map[count]["author"] = dic_papers_co_authors[author_name_norm]["Author's name(s)"]
                dic_papers_map[count]["institution"] = dic_papers_co_authors[author_name_norm]["Last Known Institution"]
                        
                count += 1
            


                
        map_df = pd.DataFrame(dic_papers_map).T

        map_df["longitude"]=map_df['longitude'].astype(float)
        map_df['latitude']=map_df['latitude'].astype(float)
        map_df = map_df[map_df["latitude"].notnull()]


        
        return pd.DataFrame(dic_papers_co_authors, index = [ "Author's name(s)" , "Author's id(s)" ,  "Number of occurence within the " + str(200) + " most related papers" , "Last Known Institution" , "Number of works" , "Number of citations" ,  "Number of related citations"]).T.style.hide(axis="index") , map_df

    else:
        return ("Select another category")


def map_authors(research_key_words , details , size):

    dic_papers , dic_papers_ranked = ranking_own_research(  research_key_words  , details ,  False  )

    
    dic_papers_co_authors = {}
    count = 0
    

    for paper in list(dic_papers_ranked.keys())[:size]:
        
        for k in range(len(dic_papers[paper]["authorships"])):
            
            if dic_papers[paper]["authorships"][k]["institutions"] != [] and "id" in dic_papers[paper]["authorships"][k]["institutions"][0] and dic_papers[paper]["authorships"][k]["institutions"][0]["id"] != None:
                institution_id = dic_papers[paper]["authorships"][k]["institutions"][0]["id"][21:]
                if institution_id in dic_institutions:
                    data = dic_institutions[institution_id]

                    dic_papers_co_authors[count] = {}
                    dic_papers_co_authors[count]["longitude"] = data["longitude"]
                    dic_papers_co_authors[count]["latitude"] = data["latitude"]
                    dic_papers_co_authors[count]["abstract comparison"] = dic_papers_ranked[paper]["abstract comparison"]
                    dic_papers_co_authors[count]["author"] = dic_papers[paper]["authorships"][k]["author"]["display_name"]
                    dic_papers_co_authors[count]["institution"] = dic_papers[paper]["authorships"][k]["institutions"][0]["display_name"]
                    dic_papers_co_authors[count]["date"] = dic_papers[paper]["date"]
                    
                    count += 1
                    
    map_df = pd.DataFrame(dic_papers_co_authors).T
                    
    if dic_papers_co_authors == {}:
        return map_df
    

    
    
    map_df["longitude"]=map_df['longitude'].astype(float)
    map_df['latitude']=map_df['latitude'].astype(float)
    map_df = map_df[map_df["latitude"].notnull()]

    return map_df