Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Results #98

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 49 additions & 27 deletions search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@

# Create your views here

def all_insert(x, e, i=0):
return [x[0:i]+[e]+x[i:]] + all_insert(x,e,i+1) if i<len(x)+1 else []

def for_each(X, e):
return all_insert(X[0], e) + for_each(X[1:],e) if X else []

def permute(x):
return [x] if len(x) < 2 else for_each( permute(x[1:]) , x[0])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard libs have already implemented this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iattempt Just updated the comments.

# To prefill the searchbar
def get_random_tag():
jsonFile = open(settings.TAGS_JSON, 'r')
Expand Down Expand Up @@ -99,13 +106,12 @@ def calculator(request):
def is_file_extension_ignored(file_):
return file_.split('.')[-1] in ['md', 'MD']


# Search query
def query(request):
global algo_name, title
if request.method == 'GET':
query = re.escape(request.GET['q']).replace('\ ', ' ')

query = query.replace('\_', ' ')

if '\\' in query:
query = query.replace('\\', '')

Expand All @@ -118,39 +124,53 @@ def query(request):
exprResult = None

q = query.replace(' ', COSMOS_SEP)



list_query=q.split(COSMOS_SEP)
perms = permute(list_query)
list1=[]
for p in perms:
for i in range(len(p)):
p[i]=p[i].replace('\\', '')
list1.append('_'.join(p))

data = json.loads(open(settings.METADATA_JSON, 'r').readline())
ans = []
rec = []
amount = 0

for folder, file in data.items():
filtered_v = []
for f in file:
if not is_file_extension_ignored(f):
filtered_v.append(f)
if q in folder and "test" not in folder.split("/"):
if filtered_v:
path = folder
folder_list = folder.split('/')
ans.append({'path': path, 'dirs': folder_list, 'files': filtered_v})
amount += len(filtered_v)
if len(folder_list) == 2:
d = folder_list[-2] + '/'
else:
d = folder_list[-3] + '/'
for i, j in data.items():
if d in i:
if q not in i:
only_contents_md = True
for f in j:
if not is_file_extension_ignored(f):
only_contents_md = False
break
if only_contents_md:
continue
p = i
p = p.split('/')
l = p[len(p) - 1]
rec.append({'recpath': i, 'recdirs': p, 'last': l})
for q in list1:
if q in folder and "test" not in folder.split("/"):

if filtered_v:
path = folder
folder_list = folder.split('/')
ans.append({'path': path, 'dirs': folder_list, 'files': filtered_v})
amount += len(filtered_v)
if len(folder_list) == 2:
d = folder_list[-2] + '/'
else:
d = folder_list[-3] + '/'
for i, j in data.items():
if d in i:
if q not in i:
only_contents_md = True
for f in j:
if not is_file_extension_ignored(f):
only_contents_md = False
break
if only_contents_md:
continue
p = i
p = p.split('/')
l = p[len(p) - 1]
rec.append({'recpath': i, 'recdirs': p, 'last': l})

if not ans and exprResult is None:
return render(request, 'cosmos/notfound.html', {'query': query})
Expand Down Expand Up @@ -182,6 +202,8 @@ def query(request):
return HttpResponse(algo, mimetype)




# Search strategy
def subsq(a, b, m, n):
# Base Cases
Expand Down