1
我已經構建了一個抓取我數據的網絡抓取工具。數據通常是結構化的。但是,然後有一些異常。現在要對數據頂部進行分析,我正在尋找幾個詞,即searched_words=['word1','word2','word3'......]
我想要這些詞出現的句子。所以我編寫如下:從使用熊貓的文本確定上下文
searched_words=['word1','word2','word3'......]
fsa = re.compile('|'.join(re.escape(w.lower()) for w in searched_words))
str_df['context'] = str_df['text'].apply(lambda text: [sent for sent in sent_tokenize(text)
if any(True for w in word_tokenize(sent) if w.lower() in words)])
這是工作,但我面臨的問題是,如果有/缺少的空格在文本中的句號後,我收到的所有這樣的句子本身。
例子:
searched_words = ['snakes','venomous']
text = "I am afraid of snakes.I hate them."
output : ['I am afraid of snakes.I hate them.']
Desired output : ['I am afraid of snakes.']
您可以顯示或共享您正在處理的數據樣本嗎? –
@RohanAmrute它和我在問題中已經說明的例子類似。 – user7140275
tokenize()中發生了什麼?你能代替'。'嗎?與'。 「?點和空間 – themistoklik