Created
October 28, 2023 06:55
-
-
Save rajathithan/4e48bd33f77f8281769bd8706685dce9 to your computer and use it in GitHub Desktop.
Count the words in a sentence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import apache_beam as beam | |
from log_elements import LogElements | |
lines = [ | |
"apple orange grape banana apple banana", | |
"banana orange banana papaya" | |
] | |
with beam.Pipeline() as p: | |
(p | beam.Create(lines) | |
| beam.FlatMap(lambda sentence: sentence.split()) | |
| beam.combiners.Count.PerElement() | |
| beam.MapTuple(lambda k, v: k + ":" + str(v)) | |
| LogElements()) | |
''' | |
Answer: | |
apple:2 | |
orange:2 | |
grape:1 | |
banana:4 | |
papaya:1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment