我目前正在嘗試在“我們的”之后提取 4 個單詞,但在“小時”和“你的”之后也不斷得到單詞。
即)“當我們到達時,我的家人將在 2 小時內發送電子郵件。” (列中的文字)
我想要什么:nan(因為沒有“我們的”)
我得到了什么:當我們到達時(因為小時是“我們的”)
我嘗試了以下代碼,但仍然沒有運氣。
our = 'our\W (?P<after>(?:\w \W ){,4})'
Reviews_C['Review_for_Fam'] =Reviews_C.ReviewText2.str.extract(our, expand=True)
你能幫忙嗎?
謝謝!
uj5u.com熱心網友回復:
我很驚訝地看到正則運算式用于此,因為它有時會導致不必要的復雜性。像這樣的東西可以嗎?
def extract_next_words(sentence):
# split the sentence into words
words = sentence.split()
# find the index of "our"
index = words.index("our")
# extract the next 4 words
next_words = words[index 1:index 5]
# join the words into a string
return " ".join(next_words)
uj5u.com熱心網友回復:
您需要確保“我們的”具有空間邊界,如下所示:
our = '(^|\s )our(\s )?\W (?P<after>(?:\w \W ){,4})'
特別(^|\s )our(\s )?是您需要播放的地方,該示例僅處理空格和句子開頭,但您可能需要擴展它以包含引號或其他特殊字符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/457109.html
上一篇:拆分Javascript字串
