1 回答

TA貢獻1772條經驗 獲得超5個贊
接下來的代碼運行良好:
class Entry :
def __init__(self, input_word, input_synonyms) :
self.word = input_word
self.synonyms = input_synonyms
e1 = Entry("sad", ["unhappy", "upset"])
e2 = Entry("happy", ["cheerful", "joyful"])
Thesaurus = [e1, e2]
doc1 = ["the", "man", "is", "sad", "very", "sad", "and", "unhappy", "and", "upset"]
doc2 = ["the", "boy", "is", "happy", "cheerful", "and", "joyful"]
Corpus = [doc1, doc2]
def search(keyword) :
all_words = ["happy", "cheerful", "joyful", "sad", "unhappy", "upset"]
for entry in Thesaurus:
if entry.word == keyword:
for word in entry.synonyms:
all_words.append(word)
store = []
for search_word in all_words:
count = 0
for document in Corpus:
for word in document:
if search_word == word:
count = count + 1
store.append([search_word, count])
return store
input_ = ("happy", "cheerful", "joyful", "sad", "unhappy", "upset")
output = search(input_)
print(output)
控制臺輸出:
[['happy', 1], ['cheerful', 1], ['joyful', 1], ['sad', 2], ['unhappy', 1], ['upset', 1]]
添加回答
舉報