簡體   English   中英

如何使用 Python 來標記句子字符串中的單詞,具體取決於它們是否在一個特定單詞之后和句號之前?

[英]How can use Python to mark words in a sentence string depending on whether they come after one specific word and before a full stop?

我有一個包含職位描述的字符串列表,如下所示:

direct or coordinate an organization's financial or budget activities to fund operations, maximize investments, or increase efficiency. may serve as liaisons between organizations, shareholders, and outside organizations. may attend and participate in meetings of municipal councils or council committees. represent organizations or promote their objectives at official functions, or delegate representatives to do so.

我已經有一些 python 代碼將描述中的每個單詞分開,並賦予它一些屬性,例如它在描述中出現的次數,它的 position(就數字排名而言)或其 POS 標簽(無論是名詞、動詞等)。 例如,如果工作描述只是“計划時間表”,我的程序已經可以給我以下內容:

[('plan', 'plan', 'NN', 0, 2, 5, 'construction managers', '11-9021.00', 245), ('schedule', 'schedul', 'NN', 1, 1, 1, 'construction managers', '11-9021.00', 245)]

我想為此添加一個標志/布爾值,它將突出顯示定義中的每個單詞,它是否出現單詞“可能”之后和句號之前 本質上,我會尋找每個描述的布爾值列表,我可以將其 zip 作為上述結構的第 10 個屬性,並知道每個單詞是否介於“可能”和句號之間。

關於如何實現這一目標的任何建議?

我假設您想找到出現在單詞“may”和句號之間的任何地方的關鍵字,即是否允許某人執行某項任務。

編譯完關鍵字列表后,您可以使用正則表達式re庫來搜索匹配的字符串。

如果在字符串中找到正則表達式,則re.search方法返回 Match object,否則返回None 但是這兩種情況也可以轉換為 boolean 變量:

import re
def may_matcher(string, keyword):
    return bool(re.search(r'may\s(\w*\s)*'+keyword+'\s*(\w*\s)*\w*\.',string))

應用這個小 function 會給你想要的 boolean:

string = "may attend to guests."
may_matcher(string, "attend")
may_matcher(string, "help")

第一行計算為True而第二行計算為False

然后,您可以通過所有關鍵字對 go 使用列表理解:

keywords = ["attend", "help"]
may_list = [may_matcher(string,keyword) for keyword in keywords]

需要注意的是,要注意否定句:如果這樣的句子也存在,那么這個 function 也會匹配帶有“may not”的句子。 您將不得不修改正則表達式。

暫無
暫無

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

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