diff --git a/.coverage b/.coverage new file mode 100644 index 0000000..18c298d Binary files /dev/null and b/.coverage differ diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/SELab1.iml b/.idea/SELab1.iml new file mode 100644 index 0000000..dafa2ee --- /dev/null +++ b/.idea/SELab1.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4fb0c0c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8d926b3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/other.xml b/.idea/other.xml new file mode 100644 index 0000000..2e75c2e --- /dev/null +++ b/.idea/other.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/1.txt b/1.txt new file mode 100644 index 0000000..5cd90f0 --- /dev/null +++ b/1.txt @@ -0,0 +1 @@ +The sunlight filtered through the dense canopy, casting intricate patterns on the forest floor. Birds chirped melodiously, creating a symphony of nature finest sounds. Amidst the greenery, a small stream gurgled, its waters crystal clear and refreshing. Suddenly, a deer darted past, startling.To @ explore strange new worlds and worlds and seek strange,To seek out new life and new worlds and new civilizations? \ No newline at end of file diff --git a/Lab1实验报告.doc b/Lab1实验报告.doc new file mode 100644 index 0000000..35113bd Binary files /dev/null and b/Lab1实验报告.doc differ diff --git a/SEreport1.docx b/SEreport1.docx new file mode 100644 index 0000000..20d8217 Binary files /dev/null and b/SEreport1.docx differ diff --git a/__pycache__/oop.cpython-310.pyc b/__pycache__/oop.cpython-310.pyc new file mode 100644 index 0000000..5520898 Binary files /dev/null and b/__pycache__/oop.cpython-310.pyc differ diff --git a/__pycache__/test.cpython-310.pyc b/__pycache__/test.cpython-310.pyc new file mode 100644 index 0000000..65c213f Binary files /dev/null and b/__pycache__/test.cpython-310.pyc differ diff --git a/oop.py b/oop.py index 711d94e..aac9d94 100644 --- a/oop.py +++ b/oop.py @@ -270,20 +270,34 @@ class Tu: time.sleep(1) def calcShortestPath(self, word1, word2): + if word1 not in self.graph or word2 not in self.graph: + print("No", word1, "or", word2, "in the graph!") + return [],0 + if word2 == "": word1 = self.input_check(word1) for wordtmp in self.dict: if wordtmp == word1: continue else: - self.calc_shortest_path(word1, wordtmp) + result=self.calc_shortest_path(word1, wordtmp) + if result == []: + print("No path exists between", word1, "and", word2) + else: + print(result) pass else: word1 = self.input_check(word1) word2 = self.input_check(word2) - self.calc_shortest_path(word1, word2) + result = self.calc_shortest_path(word1, word2) pass + if result==[] : + print("No path exists between", word1, "and", word2) + else: + print(result) + result_len = 0 if result==[] or result is None else len(result[0])-1 + return result,result_len def calc_shortest_path_old(self, word1, word2): @@ -298,8 +312,10 @@ class Tu: shortest_path_list.append(shortest_path) if shortest_path_list.__len__() > 0: self.draw_shortest_path(shortest_path_list) + return shortest_path_list else: - print("No path exists between", word1, "and", word2) + return [] + #print("No path exists between", word1, "and", word2) def calc_shortest_path(self, word1, word2): @@ -313,8 +329,11 @@ class Tu: if shortest_path_list.__len__() > 0: self.draw_shortest_path(shortest_path_list) + return shortest_path_list else: - print("No path exists between", word1, "and", word2) + return [] + + #print("No path exists between", word1, "and", word2) def random_traversal(self): diff --git a/t1.txt b/t1.txt new file mode 100644 index 0000000..832755f --- /dev/null +++ b/t1.txt @@ -0,0 +1,3 @@ +small +stream +gurgled diff --git a/t2.txt b/t2.txt new file mode 100644 index 0000000..3a1e84d --- /dev/null +++ b/t2.txt @@ -0,0 +1,28 @@ +melodiously +creating +a +small +stream +gurgled +its +waters +crystal +clear +and +refreshing +suddenly +a +deer +darted +past +startling +to +seek +out +new +worlds +and +new +life +and +worlds diff --git a/t3.txt b/t3.txt new file mode 100644 index 0000000..b52cf8d --- /dev/null +++ b/t3.txt @@ -0,0 +1,34 @@ +and +seek +out +new +life +and +refreshing +suddenly +a +symphony +of +nature +finest +sounds +amidst +the +forest +floor +birds +chirped +melodiously +creating +a +deer +darted +past +startling +to +explore +strange +new +worlds +and +worlds diff --git a/t4.txt b/t4.txt new file mode 100644 index 0000000..8ec3686 --- /dev/null +++ b/t4.txt @@ -0,0 +1,24 @@ +nature +finest +sounds +amidst +the +greenery +a +deer +darted +past +startling +to +seek +strange +new +life +and +seek +out +new +worlds +and +new +civilizations diff --git a/test.py b/test.py index 5cc9070..b8ec6db 100644 --- a/test.py +++ b/test.py @@ -5,21 +5,78 @@ class TextProcessor(Tu): def __init__(self): super().__init__() - - class TestQueryBridgeWords(unittest.TestCase): def setUp(self): - # Initialize a graph for testing + # Initialize a graph for testing self.graph = { - 'a': {'b': 1, 'c': 2}, - 'b': {'c': 1, 'd': 2}, - 'c': {'d': 1}, - 'x': {}, - 'z': {} + 'a': {'b': 1, 'c': 2}, + 'b': {'c': 1, 'd': 2}, + 'c': {'d': 1}, + 'x': {}, + 'z': {} } self.processor = TextProcessor() self.processor.generate_directed_dict(self.processor.read_text_file("1.txt")) self.processor.generate_directed_graph() + def test_normal_path1(self): + # 正常情况:存在路径 + word1 = 'to' + word2 = 'out' + expected_path = ['to', 'seek', 'out'] + expected_length = 2 + path,length = self.processor.calcShortestPath(word1, word2) + + self.assertEqual(path, [expected_path]) + self.assertEqual(length, expected_length) + + def test_normal_path2(self): + # 正常情况:存在路径 + word1 = 'to' + word2 = 'new' + expected_path = ['to', 'seek', 'out',"new"],['to', 'seek', "strange",'new'],['to', 'explore', "strange",'new'] + expected_length = 3 + path, length = self.processor.calcShortestPath(word1, word2) + + self.assertEqual(path, [expected_path]) + self.assertEqual(length, expected_length) + + def test_no_path(self): + # 无路径情况 + word1 = 'civilizations' + word2 = 'to' + expected_path = [] + expected_length = 0 + + path, length = self.processor.calcShortestPath(word1, word2) + self.assertEqual(path, expected_path) + self.assertEqual(length, expected_length) + + def test_node_not_exists(self): + # 节点不存在情况 + word1 = 'now' + word2 = 'lod' + expected_path = [] + expected_length = 0 + + path, length = self.processor.calcShortestPath(word1, word2) + self.assertEqual(path, expected_path) + self.assertEqual(length, expected_length) + + def test_same_node(self): + # 同一节点情况 + word1 = 'to' + word2 = 'to' + expected_path = [] + expected_length = 0 + + path, length = self.processor.calcShortestPath(word1, word2) + self.assertEqual(path, expected_path) + self.assertEqual(length, expected_length) + + + + + def test_valid_bridge_words_exist(self): # Valid equivalence class: bridge words exist @@ -43,7 +100,7 @@ class TestQueryBridgeWords(unittest.TestCase): def test_invalid_both_words_not_in_graph(self): # Invalid equivalence class: both words not in graph - result = self.processor.queryBridgeWords('an', 'sought') + result = self.processor.queryBridgeWords('now', 'lod') self.assertEqual(result, []) def test_edge_case_min_word(self): @@ -66,5 +123,6 @@ class TestQueryBridgeWords(unittest.TestCase): result = self.processor.queryBridgeWords('the', 'floor') self.assertIn('forest', result) + if __name__ == '__main__': unittest.main()