-1

I asked the question before but it was unclear so I added more explanation to be more clear and to get help. replace strings with ZipWithIndex/ZipWithUniqueID

I am trying to map string to number using ZipWithIndex OR ZipWithUniqueID

lets say I have this format

("u1",("name", "John Sam"))
("u2",("age", "twinty Four"))
("u3",("name", "sam Blake"))

I want this result

(0,(3,4))
(1,(5,6))
(2,(3,8))

I tried to use zipWithIndex directly to the triples but I got each letter mapped to a number I want to map the whole string without dividing it. and tried to extract the first element in the key, value pair so I did

val first = file.map(line=> line._1).distinct()
then apply ZipWithIndex
val z1= first.ZipWithIndex()

I got result like this

("u1",0)
("u2",1)
("u3",2)

now I need to take the ids/numbers and change it in my original file. and I need to keep all the distinct ids/numbers in hashTable to be able to look for them later on. is there any way to do that? Any suggestions?

I hope you got my question

saad
  • 101
  • 2
  • 13

1 Answers1

0

You mean something like this?

val file = List(("u1",("name", "John Sam")),
  ("u2",("age", "twinty Four")),
  ("u3",("name", "sam Blake")))

val first = file.map(line=> line._1) ++
  file.flatMap(line=> List(line._2._1, line._2._2)).distinct


val z1: Map[String,Int] = Map[String,Int](first.zipWithIndex:_*)

file.map{ l =>
  (z1(l._1),
    (z1(l._2._1), z1(l._2._2)))
}
F. Lins
  • 636
  • 5
  • 14
  • Implementing your answer give me different index to the same entity for example (name) mapped into two different numbers(indices). Is there any way to map them to the same index? And do you have an idea how to get the result that includes only the indices ? for example to write down the answer in this format (0,(2,3)) ? without the corresponding string – saad Jan 30 '18 at 01:03
  • I'm not sure what you mean by name getting two ids, I think the distinct call is guaranteeing that that doesn't happen. I updated the answer to map to the result id map – F. Lins Jan 30 '18 at 01:42
  • I am giving an error when I use (toMap). What is the required import statement? Also I got this error(can't resolve the symbol -> ) – saad Jan 30 '18 at 02:40
  • What version of Scala are you using? – F. Lins Jan 30 '18 at 03:19
  • I am using Scala 2.11 – saad Jan 30 '18 at 04:48
  • I've updated again but I'm not sure if it's going to work with your version of scala. If possible do update it. I removed the arrows, those are just syntactic sugar to build maps. And I changed the toMap function to use the map constructor but I'm not sure if that will be available for you. – F. Lins Jan 30 '18 at 05:05