-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching.py
133 lines (104 loc) · 4.29 KB
/
matching.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
import typing
def search(query: str, documents: typing.List[str]) -> typing.List[str]:
"""
Naive search implementation.
:param query: The text to search for.
:param documents: A list of strings representing documents that we are searching over.
:return: Documents matching the query.
"""
# The code in this function is equivalent to the following list comprehension:
# return [doc for doc in documents if boolean_term_match(query, doc)]
if not query.strip():
return []
out = []
for doc in documents:
if boolean_term_match(query=query, document=doc):
out.append(doc)
return out
def string_match(query: str, document: str) -> bool:
"""
Implements document matching by checking if the query is a substring of the document.
:param query: The text a user searched for.
:param document: A candidate document.
:return: True if the document matches the query and False otherwise.
"""
if not query:
return False
if query in document:
return True
else:
return False
def boolean_term_match(query: str, document: str) -> bool:
"""
Boolean matching function.
:param query: The text a user searched for.
:param document: A candidate document.
:return: True if all terms in the query are also in the document and False otherwise.
"""
if not query:
return False
query_terms: typing.List[str] = query.lower().split()
document_terms: typing.List[str] = document.lower().split()
for term in query_terms:
if term not in document_terms:
return False
return True
'''Test Cases are down below on the same document'''
from unittest import TestCase
from matching import *
class Test(TestCase):
def test_search(self):
self.assertEqual(['red and yellow'],
search('red', ['red and yellow', 'blue and yellow', 'predict color']))
def test_string_match__matches(self):
self.assertTrue(string_match('red', 'red and yellow'))
def test_string_match__dont_match(self):
self.assertFalse(string_match('red', 'yellow and blue'))
def test_string_match__match_substring(self):
self.assertTrue(string_match('red', 'predict color'))
#Search
#Empty string as a query
def test_search__empty_string_query(self):
self.assertEqual([], search("", ['red and yellow', 'blue and yellow', 'predict color']))
#Empty string as a document
def test_search__empty_string_document(self):
self.assertEqual([], search("red and yellow", ""))
# Empty document list
def test_search__empty_document_list(self):
self.assertEqual([], search("red and yellow", []))
#Boolean Match
def test_boolean_term_match__matches(self):
self.assertTrue(string_match('red', 'red and yellow'))
def test_boolean_term_match__dont_match(self):
self.assertFalse(string_match('red', 'yellow and blue'))
def test_boolean_term_match__match_substring(self):
self.assertTrue(string_match('red', 'predict color'))
#Empty query
def test_boolean_term_match__empty_query(self):
self.assertFalse(boolean_term_match("", "red"))
#Empty document
def test_boolean_term_match__empty_document(self):
self.assertFalse(boolean_term_match("red", ""))
#Multi Doc
def test_string_match__multi_document(self):
self.assertTrue("yellow and red", "red")
#String Match
#Empty Query
def test_string_match__empty_query(self):
self.assertFalse(string_match("", "red"))
#Empty Doc and doc list (these are the same so i didn't add another one)
def test_string_match__empty_document(self):
self.assertFalse(string_match("red", ""))
#Multi Doc
def test_string_match__multi_document(self):
self.assertTrue("yellow and red", "red")
#Search String match unexpected result
def test_string_match_unexpected_result(self):
query = 'red'
document = 'predict color'
self.assertTrue(string_match(query, document))
self.assertFalse(search(query, [document]) == document)
#Differentiate string and boolean matching
def test_matching_function_difference(self):
self.assertTrue(string_match("red", "[red and yellow]"))
self.assertFalse(boolean_term_match("red", "[red and yellow]"))