forked from SurajRKU/SE_PROJECT_GRP_12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
58 lines (46 loc) · 1.78 KB
/
search.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
import pandas as pd
# from app import app
from flask import jsonify, request, render_template
import sys
import os
app_dir = os.path.dirname(os.path.abspath(__file__))
code_dir = os.path.dirname(app_dir)
project_dir = os.path.dirname(code_dir)
#startsWith: Takes a word as input and returns a list of movie titles that start with that word.
#anywhere: Searches for the input word within any part of the movie titles and returns a list of matching movie titles. It avoids revisiting titles already found.
#results: Combines results from both startsWith and anywhere methods and returns a consolidated list of movie titles matching the input word.
#resultsTop10: Similar to results, but returns only the top 10 matching movie titles
class Search:
df = pd.read_csv(project_dir + "/data/movies.csv")
def __init__(self):
pass
def startsWith(self, word):
n = len(word)
res = []
word = word.lower()
for x in self.df["title"]:
curr = x.lower()
if curr[:n] == word:
res.append(x)
return res
def anywhere(self, word, visitedWords):
res = []
word = word.lower()
for x in self.df["title"]:
if x not in visitedWords:
curr = x.lower()
if word in curr:
res.append(x)
return res
def results(self, word):
startsWith = self.startsWith(word)
visitedWords = set()
for x in startsWith:
visitedWords.add(x)
anywhere = self.anywhere(word, visitedWords)
startsWith.extend(anywhere)
return startsWith
def resultsTop10(self, word):
return self.results(word)[:10]
if __name__ == "__main__":
app.run()