Subek Acharya
Subek Acharya

Reputation: 1

How to produce list of words from the text file in scala

I have a text file named shakespeare.txt which contains certain portions of shakespeare novel

val list1 = scala.io.Source.fromFile("shakespeare.txt")
  .getLines
  .flatMap(_.split("\\W+"))

I am expexting the result :

list1=(hamlet,the,efficient,...etc)

but this code didnt produce the list of word after seperation

Upvotes: 0

Views: 700

Answers (1)

Learner
Learner

Reputation: 1190

You can find the list like this:

scala> val list1 = scala.io.Source.fromFile("/home/anurag/test.txt").getLines.flatMap(_.split("\\W+")).toList
list1: List[String] = List(Hello, this, is, an, testing, file, Hello2)

Thanks.

Upvotes: 3

Related Questions