簡體   English   中英

如何使用NLTK獲取多個單詞的同義詞?

[英]How to get synonym for multiple word using NLTK?

我搜索了所有問題,僅提供了獲取一個單詞的同義詞的方法,但是當我嘗試使用for循環獲取多個單詞的同義詞時,它不起作用。

這是我的代碼,但是無法正常工作。

str = "Action, Adventure, Drama"

def process_genre(str):
    for genre in str.split(","):
        result = []
        for syn in wordnet.synsets(genre):
            for l in syn.lemmas():
                result.append(l.name())
        print(result)
process_genre(str)

這是輸出

['action', 'action', 'activity', 'activeness', 'military_action', 'action', 'natural_process', 'natural_action', 'action', 'activity', 'action', 'action', 'action', 'action_mechanism', 'legal_action', 'action', 'action_at_law', 'action', 'action', 'action', 'sue', 'litigate', 'process', 'carry_through', 'accomplish', 'execute', 'carry_out', 'action', 'fulfill', 'fulfil']
[]
[]

AdventureDrama列表將打印為空,應該具有其同義詞。

誰能向我解釋為什么? 有沒有辦法重置它? 要么...?

我認為問題在於您的輸入。 后面有多余的空格,所以當您分開時,您的文字會變成["Action", " Adventure"," Drama"]

在詞網中,沒有AdventureDrama類的詞(請在詞的開頭留一個空白)。 這就是為什么您沒有得到第二個和第三個單詞的輸出的原因。

解決您的輸入

分割為", "而不是","

str = "Action, Adventure, Drama"

def process_genre(str):
    for genre in str.split(", "):
        result = []
        for syn in wordnet.synsets(genre):
            for l in syn.lemmas():
                result.append(l.name())
        print(result)
process_genre(str)

輸出:

['action', 'action', 'activity', 'activeness', 'military_action', 'action', 'natural_process', 'natural_action', 'action', 'activity', 'action', 'action', 'action', 'action_mechanism', 'legal_action', 'action', 'action_at_law', 'action', 'action', 'action', 'sue', 'litigate', 'process', 'carry_through', 'accomplish', 'execute', 'carry_out', 'action', 'fulfill', 'fulfil']
['adventure', 'escapade', 'risky_venture', 'dangerous_undertaking', 'gamble', 'chance', 'risk', 'hazard', 'take_chances', 'adventure', 'run_a_risk', 'take_a_chance', 'venture', 'hazard', 'adventure', 'stake', 'jeopardize']
['play', 'drama', 'dramatic_play', 'drama', 'dramatic_event', 'drama', 'drama']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM