Help me to parse txt file


#1

I have a txt file with such content

family|mother|father|sister|brother|husband|wife:
Tell me more about your family.
How do you get along with your family?
Is your family important to you?
Do you often think about your family?
How would you like to change your family?
friend|friends|pal|mate|buddy|buddies:
Why do you bring up the topic of friends?
Do your friends worry you?
Do your friends pick on you?
Are you sure you have any friends?
Do you impose on your friends?
Perhaps your love for friends worries you?
computer|computers:
Do computers worry you?
Are you talking about me in particular?
Are you frightened by machines?
Why do you mention computers?
What do you think computers have to do with your problem?
Don't you think computers can help people?
What is it about machines that worries you?

I also have a function

 fun findKeys(): Pair<List<String> ,Map<String, Set<String>>>

In list I have to strore all these words but separate family|mother|father|sister|brother|husband|wife

and in Map<String, Set>>
i have to strore words above (family, mother) as key and phrases as value
something like this
key(family) value(How do you get along with your family?)
key(family) value(Do you often think about your family?)

and i have no idea how to do it


#2
val fileContent = """
            family|mother|father|sister|brother|husband|wife:
            Tell me more about your family.
            How do you get along with your family?
            Is your family important to you?
            Do you often think about your family?
            How would you like to change your family?
            friend|friends|pal|mate|buddy|buddies:
            Why do you bring up the topic of friends?
            Do your friends worry you?
            Do your friends pick on you?
            Are you sure you have any friends?
            Do you impose on your friends?
            Perhaps your love for friends worries you?
            computer|computers:
            Do computers worry you?
            Are you talking about me in particular?
            Are you frightened by machines?
            Why do you mention computers?
            What do you think computers have to do with your problem?
            Don't you think computers can help people?
            What is it about machines that worries you?""".trimIndent()

fun main(args: Array<String>) {
    val keys = findKeys()
    println(keys.first)
    keys.second.entries.forEach { println(it) }
}

//sampleStart
fun findKeys(): Pair<List<String>, Map<String, Set<String>>> {
    // val lines = File("path/to/file.txt").readLines() // Use this line in you project
    val lines = fileContent.lines() // This line is to allow the code to be executed on the kotlin playground

    val topicLines = lines.mapIndexedNotNull { i, s -> i.takeIf { s.matches(Regex("""\w+(\|\w+)*:""")) } }
    val ranges = topicLines.zipWithNext { a, b -> a to b } + (topicLines.last() to lines.size)

    val topics = lines.filterIndexed { i, _ -> i in topicLines }.map { it.removeSuffix(":") }.flatMap { it.split("|") }

    val questions = ranges.map { (first, last) ->
        val subTopics = lines[first].removeSuffix(":").split("|")
        val subQuestions = lines.subList(first + 1, last).toSet()
        subTopics.map { it to subQuestions }.toMap()
    }.reduce { acc, map -> acc + map }

    return topics to questions
}
//sampleEnd