I have a file with a lot of words in it, and i have to split the word that a part of it already existed or appear during the scanning of the file:
Some of the words in the file are
member
members
membership
memberships
I have tried this, but I wanted the first lines[i](which is a) to keep looping the next words
func Split(lines []string) string {
for i := 0; i < len(lines)-1; i++ { // position of words
j := i + 1
fmt.Println(lines[i], lines[j])
if strings.ContainsAny(lines[j], lines[i]) {
s := strings.Split(dictionary[j], dictionary[i])
fmt.Println(dictionary[i], ".", s)
}
}
...
}
but it only outputs
member
member.s
members.hip
membership.s
The output that i want:
member
member.s
member.ship
members.hip
member.ships
members.hips
For the given input following would help.
func splitSegmant(prev string, cur string) string {
if len(cur) < len(prev) || cur[:len(prev)] != prev {
return ""
}
return fmt.Sprintf("%s.%s", cur[:len(prev)], cur[len(prev):])
}
func Split(lines []string) []string {
splits := []string{lines[0]}
for i := 0; i < len(lines); i++ {
for j := 0; j < i; j++ {
split := splitSegmant(lines[j], lines[i])
if split != "" {
splits = append(splits, split)
}
}
}
return splits
}
you may find working code here : playground