-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch
More file actions
executable file
·229 lines (212 loc) · 7.27 KB
/
search
File metadata and controls
executable file
·229 lines (212 loc) · 7.27 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,os
import getopt
import re
import copy
# BETTER: construct indices for tags to speed up searching
# TODO: papers after a given year, or a year interval; fuzzy matching? split into words and not substring matching?
# BETTER: compute the similarity of two strings instead of finding substr, for example, San Zhang and Zhang San should be the same
# file="note/literature.list"
def getFiles(dir, suffix):
results = []
for root, directory, files in os.walk(dir):
# print root,directory,files
for filename in files:
name, suf = os.path.splitext(filename)
# print name,suf
if suf == suffix:
# print os.path.join(root,filename)
results.append(os.path.join(root, filename))
return results
def removeWhiteSpace(strlist):
new_value_list = []
for i in strlist:
new_value_list.append(i.strip())
return new_value_list
items = ["file", "id", "title", "author", "journal", "year", "tags", "star", "problem", "interest", "hardness", "idea", "future", "comment", "other"]
class Paper(object):
def __init__(self, content={}):
self.content = content
def add(self, key, value):
if key == 'author' or key == 'tags':
value_list = value.split(',')
# print value_list
self.content[key] = removeWhiteSpace(value_list)
else:
self.content[key] = value.strip()
def output(self):
# BETTER: set a display order for different items
# for key in self.content:
for key in items:
print(key, "=>", self.content[key])
print("---")
def test(self, num=1):
return num * 2
def copy(self):
# NOTICE: copy.copy(self) is not enough, self.content is also a reference!
# Another choice is to use copy.deepcopy()
ret = Paper()
ret.content = copy.copy(self.content)
return ret
def compare_paper(a, b):
return len(a.content["star"]) < len(b.content["star"])
def readPaperList(papers, f):
# papers = []
with open(f, "r") as file_object:
# contents = file_object.read()
# print contents
# for line in file_object:
# print line
lines = file_object.readlines()
# print lines
curr = None
for line in lines:
# print line
#lstrip or rstrip: remove Whitespace by default, like space, \r, \n, \t, \f
# print line.rstrip()
line = line.strip()
if line == '':
continue
elif line[0:3] == '---':
if curr != None:
# curr.output()
# NOTICE: Python passes reference rather than value by default
papers.append(curr.copy())
curr = None
curr = Paper()
curr.add('file', f)
else:
str = line.split('=')
key = str[0].strip()
value = str[1].strip()
curr.add(key, value)
# print curr.content
# print "to check"
# for p in papers:
# p.output()
return
# return papers
def usage():
print(
"""
Usage:sys.args[0] [option]
-h or --help: shows the manual info
-g or --tags: the tags of paper, multiple tags divided by comma
-a or --author: the authors of paper, multiple authors divided by comma
-t or --title: the title of paper
-y or --year: the publication year of paper
-j or --journal: the conference/journal of paper
-s or --star: the score of paper, 1~5
-i or --id: the unique ID of this paper
-v or --version:shows the version of this software
"""
)
def regexMatch(limit, value):
# matchObj = re.search( r'dogs', value, re.M|re.I)
# re.search returns the first matching, while re.find returns all
matchObj = re.search( limit, value, re.M|re.I)
# print value
# print matchObj
if matchObj == None:
return False
else:
return True
def check(paper, limits):
for l in limits:
v = limits[l]
if l == 'author' or l == 'tags':
# check each item
for ev in v:
flag = False
for it in paper.content[l]:
if regexMatch(ev, it) == True:
flag = True
break
if flag == False:
return False
elif l == 'id':
if v != paper.content[l]:
return False
else:
if regexMatch(v, paper.content[l]) == False:
return False
return True
def starString(arg):
num = int(arg)
ret = ""
for i in range(num):
ret = ret + r'\*';
# print ret
return ret
if __name__ == '__main__':
files = getFiles('data', '.list')
papers = []
for f in files:
# print f
readPaperList(papers, f)
# for p in papers:
# p.output()
if len(sys.argv) == 1:
usage()
sys.exit()
# NOTICE: sort output list by star by default
sort_key = 'star'
# REFER: https://www.cnblogs.com/madsnotes/articles/5687079.html
#TODO+DEBUG: --version is not supported now
try:
opts, args = getopt.getopt(sys.argv[1:], "h:v:t:g:a:y:j:s:i:o", ["help", "version","title=", "tags=", "author=", "year=", "journal=", "star=", "id=", "order="])
except getopt.GetoptError:
print("argv error,please input")
# print opts
# print args
limits = {}
for cmd, arg in opts:
if cmd in ("-h", "--help"):
print("help info")
sys.exit()
elif cmd in ("-y", "--year"):
limits["year"] = arg.strip()
elif cmd in ("-j", "--journal"):
limits["journal"] = arg.strip()
elif cmd in ("-t", "--title"):
limits["title"] = arg.strip()
elif cmd in ("-a", "--author"):
limits["author"] = removeWhiteSpace(arg.split(','))
elif cmd in ("-g", "--tags"):
limits["tags"] = removeWhiteSpace(arg.split(','))
elif cmd in ("-s", "--star"):
limits["star"] = starString(arg.strip())
# print limits["star"]
elif cmd in ("-i", "--id"):
limits["id"] = arg.strip()
elif cmd in ("-v", "--version"):
print("%s version 1.0" % sys.argv[0])
elif cmd in ("-o", "--order"):
# print "order"
# print arg
# sort by a given property(key)
sort_key = arg.strip()
# BETTER+TODO: support abbreviation and reverse order
else:
print("command not supported")
# print limits
results = []
# print "paper num: ", len(papers)
for p in papers:
# p.output()
if check(p, limits) == True:
results.append(copy.copy(p))
# print "results:"
# print results
# sort on stars number, papers with higher stars should be placed on top
# sorted_results = sorted(results, cmp=compare_paper, reverse=True)
# print sort_key
sorted_results = sorted(results, key=lambda paper:len(paper.content[sort_key]), reverse=True)
# print "sorted_results:"
# print sorted_results
print("---")
for r in sorted_results:
r.output()
# print "hello, world"
# print file