![](/img/trans.png)
[英]How to check that the two consecutive words have the same regex pattern
[英]In Pyspark, how to check if consecutive words in a text file start with the same alphabet?
我在文件中有以下文本:
Horrid Henry’s hound hunts in the massive Murree mountains. While silly stupid Samuel’s dark dreadful dragon likes to hunt in
skies.
Horrid Henry’s hound and Samuel’s dreadful dragon Dany are fast friends and like to hunt and play together. They call themselves
fantastic fanciful foursome.
我加載這個文件和flatMap
如下:
lines=sc.textFile("BigData test.txt")
RddWords=lines.flatMap(lambda line: line.split(" "))
這會將其轉換為字符串列表(每個單詞都是一個字符串)。 我想檢查三個連續的單詞是否從同一個字母表開始。 預期輸出如下:
H => 3
M=> 1
S => 1
D => 1
F => 1
以“H”開頭的連續單詞出現 3 次。 類似地,以“M”開頭的連續單詞只發生一次。 下面顯示的是這些連續單詞的詳細出現次數。
Horrid Henry’s hound =>2
Henry’s hound hunts => 1
massive Murree mountains =>1
silly stupid Samuel’s =>1
dreadful dragon Dany=>1
fantastic fanciful foursome =>1
我可以編寫 python 函數來簡單地檢查一串單詞中的三個連續單詞。 但我想不出如何在名為RddWords
的並行化 Rdd 上實現該功能。 如果我編寫一個 map 函數,它將在 Rdd RddWords
每個 x 上單獨RddWords
。 我將如何處理連續的單詞? 有人可以指導我一點嗎? 非常感謝
解決方案1
您需要將每一行作為滾動三元組:
(word0, word1, word2)
(word1, word2, word3)
...
然后映射一個函數f
來提取你想要的信息。
解決方案2
使用 Dataframe API 並應用長度為3
的滾動窗口函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.