-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloudPapers.py
executable file
·2088 lines (1686 loc) · 76.5 KB
/
cloudPapers.py
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from tkinter import filedialog
import tkinter.font as tkfont
from pickle import load as pickle_load
from pickle import dump as pickle_dump
from subprocess import call as subp_call
from subprocess import Popen as subp_popen
import re
import datetime
import sys, os
import ntpath
try:
# python 2
from urllib2 import Request, urlopen, quote
except ImportError:
# python 3
from urllib.request import Request, urlopen, quote
try:
# python 2
from htmlentitydefs import name2codepoint
except ImportError:
# python 3
from html.entities import name2codepoint
# request google scholar for bibtex
GOOGLE_SCHOLAR_URL = "https://scholar.google.com"
HEADERS = {'User-Agent': 'Mozilla/5.0'}
# to support relative path across linux, mac and windows
application_path = os.path.dirname(os.path.abspath(__file__))
if getattr(sys, 'frozen', False):
# If the application is run as a bundle, the pyInstaller bootloader
# extends the sys module by a flag frozen=True and sets the app
# path into variable _MEIPASS'.
application_path = os.path.dirname(sys.executable)
# configure
lib_file = os.path.join(application_path, "papers.dat")
toread_file = os.path.join(application_path, "toread.txt")
unread_file = os.path.join(application_path, "unread.txt")
conference_file = os.path.join(application_path, "conference.dat")
DEFAULT_YEAR = 1900
MAX_RATING = 5
OTHERS_CONFERENCE = 'others'
# Build a list of tuples for each file type the file dialog should display
my_filetypes = [('all files', '.*'), ('pdf files', '.pdf'), ('Word files', '.doc'), ('Word files', '.docx'), ('text files', '.txt')]
filetypes = tuple([ftype[1] for ftype in my_filetypes[1:]])
class Category:
def __init__(self, label):
self.label = label
self.papers = set() # paper ids
# category_str: gui_input, multiple category separated by ';'
@classmethod
def parse(cls, category_str):
items = category_str.split(';')
category = []
for item in items:
item = item.strip()
if len(item) < 1 : continue
category.append(item)
return category
@classmethod
def guiString(cls, categories):
return ';'.join([c.label for c in categories])
def __repr__(self):
return self.label
author_format_re = re.compile(r'^(.+?)[, ](.+?);(.*)')
author_format1_re = re.compile(r'^(.+?)[, ](.+?) and (.*)')
class Author(Category):
def __init__(self, label):
self.last_name, self.first_name = self.nameParse(label)
self.label = self.getFullname(self.first_name, self.last_name)
self.papers = set()
@classmethod
def getFullname(cls, first_name, last_name):
if len(last_name) > 0 and len(first_name) > 0:
return last_name + ', ' + first_name
elif len(last_name) > 0 :
return last_name
else: return ""
@classmethod
def nameParse(cls, full_name):
reverse = False if ',' in full_name else True
full_name = full_name.strip()
last_name = ""
first_name = ""
if not reverse:
splitted_names = full_name.split(',', 2)
if len(splitted_names) == 1 :
last_name = full_name
else:
last_name = splitted_names[0].strip()
first_name = splitted_names[1].strip()
else:
splitted_names = full_name.rsplit(' ', 1)
if len(splitted_names) == 1 :
last_name = full_name
else:
last_name = splitted_names[1].strip()
first_name = splitted_names[0].strip()
return last_name, first_name
@classmethod
def parseFormat1(cls, author_str):
items = author_str.split(' and ')
authors = []
for item in items:
item = item.strip()
if len(item) < 1 : continue
authors.append(item)
return authors
@classmethod
def parseAuthorString(cls, author_str):
m = author_format_re.match(author_str)
m1 = author_format1_re.match(author_str)
if m :
items = cls.parse(author_str)
elif m1:
items = cls.parseFormat1(author_str)
else:
items = [author_str]
return items
@classmethod
def authorParse(cls, author_str):
items = cls.parseAuthorString(author_str)
authors = []
for item in items:
authors.append(Author(item))
return authors
@classmethod
def bibString(cls, authors):
return ' and '.join([a.label for a in authors])
@classmethod
def guiString(cls, authors):
return ';'.join([a.label for a in authors])
class Project(Category):
@classmethod
def projectParse(cls, project_str):
items = cls.parse(project_str)
projects = []
for item in items:
projects.append(Project(item))
return projects
class Tag(Category):
@classmethod
def tagParse(cls, tag_str):
items = cls.parse(tag_str)
tags = []
for item in items:
tags.append(Tag(item))
return tags
class Conference:
def __init__(self, label):
self.label = label
self.index = 0
self.papers = set()
@staticmethod
def loadConference(file_name):
c_map = {}
if os.path.isfile(file_name):
with open(file_name) as fin:
for line in fin.readlines():
line = line.strip().lower()
items = re.split('\t| ', line)
if len(items) != 2: continue
c_map[items[0]] = items[1]
return c_map
def __repr__(self):
return self.label
class Dataset(Category):
@classmethod
def datasetParse(cls, dataset_str):
items = cls.parse(dataset_str)
datasets = []
for item in items:
datasets.append(Dataset(item))
return datasets
first_word_re = re.compile(r'^[a-zA-Z]+')
class Bib:
def __init__(self):
self._title = ""
self._author = []
self._conference = None
self._year = DEFAULT_YEAR
self._first_title_word = ""
self._first_author_name = ""
self.bibtex = ""
self.type = 0 # 0: conference, 1: jornal
@property
def title(self):
return self._title
@title.setter
def title(self, value):
self._title = value.lower()
m = first_word_re.search(value)
if m : self._first_title_word = m.group()
@property
def author(self):
return self._author
@author.setter
def author(self, value):
self._author = []
self._first_author_name = ""
if isinstance(value, str) and len(value) > 0:
value = Author.authorParse(value.lower())
if isinstance(value, list) and len(value) >= 1 :
format_correct = True
for v in value:
if not isinstance(v, Author) :
format_correct = False
break
if format_correct:
self._author = value
self._first_author_name = value[0].last_name
@property
def conference(self):
return self._conference
@conference.setter
def conference(self, value):
self._conference = None
if isinstance(value, str) and len(value) > 0:
value = Conference(value.lower())
if isinstance(value, Conference) :
self._conference = value
@property
def year(self):
return self._year
@year.setter
def year(self, value):
self._year = DEFAULT_YEAR
if isinstance(value, str) and len(value) > 0:
value = int(value)
if isinstance(value, int) and value >= DEFAULT_YEAR and value <= datetime.datetime.now().year :
self._year = value
def __repr__(self):
tmp_cite = self._first_author_name + str(self.year)+ self._first_title_word
tmp_c_str = "" if self.conference is None else self.conference.label
if self.type == 1:
return "@article{{{},\n title={{{}}},\n author={{{}}},\n journal={{{}}},\n year={{{}}}\n}}".format(tmp_cite, self.title, Author.bibString(self.author), tmp_c_str, str(self.year))
else:
return "@inproceedings{{{},\n title={{{}}},\n author={{{}}},\n booktitle={{{}}},\n year={{{}}}\n}}".format(tmp_cite, self.title, Author.bibString(self.author), tmp_c_str, str(self.year))
def shortString(self):
return " ".join([self.title, ' '.join([a.label for a in self.author]), str(self.year) if self.year!=DEFAULT_YEAR else ''])
type_re = re.compile(r'^@inproceedings(.*)')
title_re = re.compile(r'(?<=[^a-z]title\={).+?(?=})')
author_re = re.compile(r'(?<=[^a-z]author\={).+?(?=})')
conference_re = re.compile(r'(?<=[^a-z]booktitle\={).+?(?=})|(?<=[^a-z]journal\={).+?(?=})')
year_re = re.compile(r'(?<=[^a-z]year\={).+?(?=})')
gsbib_re = re.compile(r'<a href="https://scholar.googleusercontent.com(/scholar\.bib\?[^"]*)')
class bibParser:
@classmethod
def parse(cls, bib_str, lib=None):
b = Bib()
b.bibtex = bib_str
b.type = cls.typeParser(bib_str)
b.title = cls.titleParser(bib_str)
b.author = cls.authorParser(bib_str, lib=lib)
b.conference = cls.conferenceParser(bib_str, lib=lib)
b.year = cls.yearParser(bib_str)
return b
@classmethod
def typeParser(cls, bib_str):
m = type_re.match(bib_str)
return 0 if m else 1
@classmethod
def titleParser(cls, bib_str):
m = title_re.search(bib_str)
return m.group() if m else ""
@classmethod
def authorParser(cls, bib_str, lib=None):
m = author_re.search(bib_str)
a_str = m.group() if m else ""
if lib is not None:
authors = lib.parseAuthors(a_str)
return authors
return a_str
@classmethod
def conferenceParser(cls, bib_str, lib=None):
m = conference_re.search(bib_str)
c_str = m.group() if m else ""
if lib is not None and len(c_str) > 0:
conference = lib.parseConference(c_str)
return conference
return c_str
@classmethod
def yearParser(cls, bib_str):
m = year_re.search(bib_str)
return m.group() if m else ""
# google scholar query
# todo: download pdf
@classmethod
def query(cls, searchstr):
"""Query google scholar.
This method queries google scholar and returns a list of citations.
Parameters
----------
searchstr : str
the query
Returns
-------
result : list of strings
the list with citations
"""
searchstr = '/scholar?q='+quote(searchstr)
url = GOOGLE_SCHOLAR_URL + searchstr
header = HEADERS
header['Cookie'] = "GSP=CF=4"
request = Request(url, headers=header)
response = urlopen(request)
html = response.read()
html = html.decode('utf8')
# grab the links
tmp = cls.get_links(html)
# follow the bibtex links to get the bibtex entries
result = list()
for link in tmp:
url = GOOGLE_SCHOLAR_URL+link
request = Request(url, headers=header)
response = urlopen(request)
bib = response.read()
bib = bib.decode('utf8')
result.append(bib)
return result
@classmethod
def get_links(cls, html):
"""Return a list of reference links from the html.
Parameters
----------
html : str
outformat : int
the output format of the citations
Returns
-------
List[str]
the links to the references
"""
reflist = gsbib_re.findall(html)
# escape html entities
reflist = [re.sub('&(%s);' % '|'.join(name2codepoint), lambda m:
chr(name2codepoint[m.group(1)]), s) for s in reflist]
return reflist
class Paper(object):
def __init__(self):
# required information
self.id = -1
self.bib = Bib()
self._path = "" # relative path to support cloud storage
# optional information
self._dataset = []
self._tag = []
self._project = []
self.comment = ""
self.hasGithub = False
self.hasRead = False
self._rating = 0
self._need_revise = False
@property
def bibtex(self):
return self.bib.bibtex
@bibtex.setter
def bibtex(self, value):
self.bib.bibtex = value
@property
def papertype(self):
return self.bib.type
@papertype.setter
def papertype(self, value):
self.bib.type = value
@property
def title(self):
return self.bib.title
@title.setter
def title(self, value):
self.bib.title = value
@property
def author(self):
return Author.guiString(self.bib.author)
@author.setter
def author(self, value):
self.bib.author = value
@property
def conference(self):
if self.bib.conference is not None:
return self.bib.conference.label
else:
return ""
@conference.setter
def conference(self, value):
self.bib.conference = value
@property
def year(self):
return str(self.bib.year)
@year.setter
def year(self, value):
self.bib.year = value
@property
def rating(self):
return str(self._rating)
@rating.setter
def rating(self, value):
self._rating = 0
if isinstance(value, str) : value = int(value)
if isinstance(value, int) and value >= 0 and value <= MAX_RATING:
self._rating = value
@property
def path(self):
return self._path
@path.setter
def path(self, value):
self._path = ""
normed_value = os.path.normpath(value)
filename = ntpath.basename(normed_value)
if filename.endswith(filetypes) and os.path.isfile(os.path.join(application_path, normed_value)):
self._path = normed_value
@property
def full_path(self):
return os.path.join(application_path, self._path)
@property
def dataset(self):
return Dataset.guiString(self._dataset)
@dataset.setter
def dataset(self, value):
self._dataset = []
if isinstance(value, str) and len(value) > 0:
value = Dataset.datasetParse(value)
if isinstance(value, list) and len(value) >= 1 :
format_correct = True
for v in value:
if not isinstance(v, Dataset) :
format_correct = False
break
if format_correct:
self._dataset = value
@property
def tag(self):
return Tag.guiString(self._tag)
@tag.setter
def tag(self, value):
self._tag = []
if isinstance(value, str) and len(value) > 0:
value = Tag.tagParse(value)
if isinstance(value, list) and len(value) >= 1 :
format_correct = True
for v in value:
if not isinstance(v, Tag) :
format_correct = False
break
if format_correct:
self._tag = value
@property
def project(self):
return Project.guiString(self._project)
@project.setter
def project(self, value):
self._project = []
if isinstance(value, str) and len(value) > 0:
value = Project.projectParse(value)
if isinstance(value, list) and len(value) >= 1 :
format_correct = True
for v in value:
if not isinstance(v, Project) :
format_correct = False
break
if format_correct:
self._project = value
def __repr__(self):
return "title: {}\nauthor: {}\nconference: {}\nyear: {}\npath: {}\ntags: {}\ndataset: {}\nproject: {}\ncomment: {}\n{}\n".format(self.title, self.author, self.conference, self.year, self.full_path, self.tag, self.dataset, self.project, self.comment, 'Has released codes!' if self.hasGithub else 'No released codes!')
def checkState(self):
state = 0
if self._path == "" :
state = 1
elif self.title == "" or self.author == "" or self.conference == "" or self.year == str(DEFAULT_YEAR) :
state = 2
return state
class Library:
def __init__(self):
self._years = {} # {year:set(paper_id, ...), ...}
self._authors = {} # author_label: Author()
self._conferences = {OTHERS_CONFERENCE:Conference(OTHERS_CONFERENCE)} # conference_label: Conference()
self._datasets = {} # dataset_label: Category()
self._tags = {} # tag_label: Category()
self._projects = {} # project_label: Category()
self._ratings = {} # rating: set(paper_id, ...)
self._papers = {} # paper_id: Paper()
self._conference_alias = {OTHERS_CONFERENCE:OTHERS_CONFERENCE}
self.paper_id_pool = set()
self.max_paper_id = len(self._papers) - 1
@property
def papers(self):
return self._papers
@property
def authors(self):
return self._authors
@property
def conferences(self):
return self._conferences
@property
def years(self):
return self._years
@property
def datasets(self):
return self._datasets
@property
def tags(self):
return self._tags
@property
def projects(self):
return self._projects
@property
def ratings(self):
return self._ratings
def parseConference(self, c_str):
re_c = None
if len(c_str) > 0:
c_list = self.findConference(c_str.lower())
re_c = c_list[0]
# todo: compute similarity and pick up the similarer one
for c in c_list:
if c_str == c.label:
re_c = c
return re_c
def parseAuthors(self, a_str):
authors = []
items = Author.parseAuthorString(a_str.lower())
for item in items:
last_name, first_name = Author.nameParse(item)
full_name = Author.getFullname(first_name, last_name)
a_list = self.findAuthor(full_name)
if len(a_list) > 0:
authors.append(a_list[0])
else:
authors.append(Author(full_name))
return authors
def parseTags(self, t_str):
tags = []
items = Tag.parse(t_str.lower())
for item in items:
t_list = self.findTag(item)
if len(t_list) > 0:
tags.append(t_list[0])
else:
tags.append(Tag(item))
return tags
def parseDatasets(self, d_str):
datasets = []
items = Dataset.parse(d_str.lower())
for item in items:
d_list = self.findDataset(item)
if len(d_list) > 0:
datasets.append(d_list[0])
else:
datasets.append(Dataset(item))
return datasets
def parseProjects(self, p_str):
projects = []
items = Project.parse(p_str.lower())
for item in items:
p_list = self.findProject(item)
if len(p_list) > 0:
projects.append(p_list[0])
else:
projects.append(Project(item))
return projects
def removePaper(self, paper_id):
if paper_id in self.papers:
del_paper = self.papers[paper_id]
if del_paper.bib.year in self.years:
self.years[del_paper.bib.year].remove(paper_id)
if len(self.years[del_paper.bib.year]) == 0:
del self.years[del_paper.bib.year]
if del_paper.bib.conference is not None:
del_paper.bib.conference.papers.remove(paper_id)
for a in del_paper.bib.author:
a.papers.remove(paper_id)
if len(a.papers) == 0:
del self.authors[a.label]
for t in del_paper._tag:
t.papers.remove(paper_id)
if len(t.papers) == 0:
del self.tags[t.label]
for d in del_paper._dataset:
d.papers.remove(paper_id)
if len(d.papers) == 0:
del self.datasets[d.label]
for p in del_paper._project:
p.papers.remove(paper_id)
if len(p.papers) == 0:
del self.projects[p.label]
if del_paper._rating in self.ratings:
self.ratings[del_paper._rating].remove(paper_id)
if len(self.ratings[del_paper._rating]) == 0:
del self.ratings[del_paper._rating]
del self._papers[paper_id]
self.paper_id_pool.add(paper_id)
# paper: Paper()
def addPaper(self, paper):
paper_id = self.generatePaperId()
paper.id = paper_id
self.addPaperYear(paper_id, paper.bib.year)
self.addPaperRating(paper_id, paper._rating)
if paper.bib.conference is not None:
paper.bib.conference.papers.add(paper_id)
self.addPaperCategory(paper_id, paper.bib.author, self.authors)
self.addPaperCategory(paper_id, paper._tag, self.tags)
self.addPaperCategory(paper_id, paper._dataset, self.datasets)
self.addPaperCategory(paper_id, paper._project, self.projects)
self._papers[paper_id] = paper
return paper_id
def addPaperYear(self, paper_id, year):
if year > DEFAULT_YEAR:
tmp_paper_set = self._years.get(year, set())
tmp_paper_set.add(paper_id)
self._years[year] = tmp_paper_set
def addPaperRating(self, paper_id, rating):
if rating > 0 :
tmp_paper_set = self._ratings.get(rating, set())
tmp_paper_set.add(paper_id)
self._ratings[rating] = tmp_paper_set
def addPaperCategory(self, paper_id, categories, target_categories):
for c in categories:
if len(c.papers) == 0:
target_categories[c.label] = c
c.papers.add(paper_id)
def revisePaperBib(self, paper_id, bib):
hasRevised = False
target_paper = self.papers[paper_id]
if target_paper.bibtex != bib.bibtex:
target_paper.bib.bibtex = bib.bibtex
hasRevised = True
if target_paper.papertype != bib.type:
target_paper.bib.type = bib.type
hasRevised = True
if target_paper.title != bib.title:
target_paper.bib.title = bib.title
hasRevised = True
if int(target_paper.year) != bib.year:
if target_paper.bib.year in self.years:
self.years[target_paper.bib.year].remove(paper_id)
if len(self.years[target_paper.bib.year]) == 0 :
del self.years[target_paper.bib.year]
self.addPaperYear(paper_id, bib.year)
target_paper.bib.year = bib.year
hasRevised = True
if bib.conference is not None and target_paper.conference != bib.conference.label:
if target_paper.bib.conference is not None:
target_paper.bib.conference.papers.remove(paper_id)
target_paper.bib.conference = bib.conference
bib.conference.papers.add(paper_id)
hasRevised = True
if target_paper.author != Author.guiString(bib.author):
target_paper.bib.author = self.revisePaperCategory(paper_id, bib.author, target_paper.bib.author, self.authors)
hasRevised = True
return hasRevised
def revisePaper(self, paper_id, paper):
hasRevised = False
target_paper = self.papers[paper_id]
if target_paper.path != paper.path :
target_paper.path = paper.path
hasRevised = True
hasRevised = hasRevised | self.revisePaperBib(paper_id, paper.bib)
if target_paper.tag != paper.tag:
target_paper._tag = self.revisePaperCategory(paper_id, paper._tag, target_paper._tag, self.tags)
hasRevised = True
if target_paper.dataset != paper.dataset:
target_paper._dataset = self.revisePaperCategory(paper_id, paper._dataset, target_paper._dataset, self.datasets)
hasRevised = True
if target_paper.project != paper.project:
target_paper._project = self.revisePaperCategory(paper_id, paper._project, target_paper._project, self.projects)
hasRevised = True
if target_paper.comment != paper.comment:
target_paper.comment = paper.comment
hasRevised = True
if target_paper.hasRead != paper.hasRead:
target_paper.hasRead = paper.hasRead
hasRevised = True
if target_paper.hasGithub != paper.hasGithub:
target_paper.hasGithub = paper.hasGithub
hasRevised = True
if target_paper.rating != paper.rating:
if target_paper._rating in self.ratings:
self.ratings[target_paper._rating].remove(paper_id)
if len(self.ratings[target_paper._rating]) == 0:
del self.ratings[target_paper._rating]
self.addPaperRating(paper_id, paper._rating)
target_paper._rating = paper._rating
hasRevised = True
return hasRevised
def revisePaperCategory(self, paper_id, source_category, target_category, categories):
for c in source_category:
c.papers.add(paper_id)
if c.label not in categories:
categories[c.label] = c
for c in target_category:
if c not in source_category:
c.papers.remove(paper_id)
if len(c.papers) == 0:
del categories[c.label]
return source_category
def setOtherConference(self, paper_id, paper):
paper.bib._conference = self._conferences[OTHERS_CONFERENCE]
self._conferences[OTHERS_CONFERENCE].papers.add(paper_id)
def generatePaperId(self):
if len(self.paper_id_pool) < 1:
self.extendPaperIdPool()
tmp_id = self.paper_id_pool.pop()
return tmp_id
def extendPaperIdPool(self):
tmp_id = self.max_paper_id + 1
while tmp_id in self.paper_id_pool:
tmp_id += 1
self.paper_id_pool.add(tmp_id)
self.max_paper_id = tmp_id
def similarity(self, input_str, target_str, support_fuzzy=False):
str_a = input_str.lower()
str_b = target_str.lower()
if not support_fuzzy:
return str_a == str_b
if str_a in str_b or str_b in str_a :
return True
tokens_a = re.split(r'[\s,_-]', str_a)
for token in tokens_a:
if token in str_b:
return True
return False
def searchDuplicatePaper(self, paper):
for pi in self.papers:
pi_path = self.papers[pi].path
if paper.path == pi_path or paper.title == self.papers[pi].title or ntpath.basename(paper.path) == ntpath.basename(pi_path):
return pi
return -1
# todo: better fuzzy comment
def findPaper(self, paper, target_paper_ids=None, support_fuzzy=False, fuzzy_window=0):
papers_list = []
if len(paper.title) > 0:
title_papers = self.findTitle(paper.title, target_paper_ids=target_paper_ids, support_fuzzy=support_fuzzy)
papers_list.append(title_papers)
if paper.conference != "" and paper.conference != OTHERS_CONFERENCE :
conferences = self.findConference(paper.conference, support_fuzzy=support_fuzzy)
conference_papers = self.combineListFindResults([c.papers for c in conferences])
papers_list.append(conference_papers)
if paper.bib.year > DEFAULT_YEAR:
year_papers = self.findYear(paper.year, fuzzy_window=fuzzy_window)
papers_list.append(year_papers)
if len(paper.author) > 0:
authors = []
for a in paper.bib.author:
authors.extend(self.findAuthor(a.label, support_fuzzy=support_fuzzy))
author_papers = self.combineListFindResults([a.papers for a in authors])
papers_list.append(author_papers)
if len(paper.tag) > 0:
tags = []
for t in paper._tag:
tags.extend(self.findTag(t.label, support_fuzzy=support_fuzzy))
tag_papers = self.combineListFindResults([t.papers for t in tags])
papers_list.append(tag_papers)
if len(paper.dataset) > 0:
datasets = []
for d in paper._dataset:
datasets.extend(self.findDataset(d.label, support_fuzzy=support_fuzzy))
dataset_papers = self.combineListFindResults([d.papers for d in datasets])
papers_list.append(dataset_papers)
if len(paper.project) > 0:
projects = []
for p in paper._project:
projects.extend(self.findProject(p.label, support_fuzzy=support_fuzzy))
project_papers = self.combineListFindResults([p.papers for p in projects])
papers_list.append(project_papers)
tmp_papers = self.combineListFindResults(papers_list, True)
if target_paper_ids is not None:
tmp_papers =[pi for pi in tmp_papers if pi in target_paper_ids]
return list(tmp_papers)
def combineListFindResults(self, papers_list, isAnd=True):
re_papers = set()
if len(papers_list) > 0 :
if isAnd:
re_papers = papers_list[0].intersection(*papers_list[1:])
else:
re_papers = papers_list[0].union(*papers_list[1:])
return re_papers
def findYear(self, year, fuzzy_window=0):
papers = set()
year = int(year)
if year in self.years:
papers |= self.years[year]
if fuzzy_window > 0:
for i in range(fuzzy_window):
if year+1+i in self.years:
papers |= self.years[year+1+i]
if year-i-1 in self.years:
papers |= self.years[year-i-1]
return papers
def findRating(self, rating):
papers = set()
rating = int(rating)
if rating in self.ratings:
papers |= self.ratings[rating]
return papers
def findUnread(self):
papers = [pi for pi in self.papers if not self.papers[pi].hasRead]
return set(papers)
def findGithub(self):
papers = [pi for pi in self.papers if self.papers[pi].hasGithub]
return set(papers)
def findToRevise(self):
papers = [pi for pi in self.papers if self.papers[pi]._need_revise]
return set(papers)
def findTitle(self, t_str, target_paper_ids=None, support_fuzzy=False):
papers = set()
if target_paper_ids is None:
target_paper_ids = self._papers
for pi in target_paper_ids:
if self.similarity(t_str, self._papers[pi].title, support_fuzzy=support_fuzzy):
papers.add(pi)
return papers
def getConferenceName(self, c_str):
if len(c_str) > 0:
return self._conference_alias[c_str] if c_str in self._conference_alias else OTHERS_CONFERENCE
else: return c_str
def findConference(self, c_str, support_fuzzy=False):
conferences = []
if c_str != OTHERS_CONFERENCE and len(c_str) > 0:
for c_name in self._conference_alias:
if c_str == c_name or c_name in c_str :
conferences.append(self.conferences[self._conference_alias[c_name]])
elif support_fuzzy and self.similarity(c_str, c_name, support_fuzzy=support_fuzzy):
conferences.append(self.conferences[self._conference_alias[c_name]])
return conferences if len(conferences) > 0 else [self._conferences[OTHERS_CONFERENCE]]
elif c_str == OTHERS_CONFERENCE: