douluo2930 2019-04-08 07:36
浏览 158
已采纳

如何分割/分割文件中的单词

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
  • 写回答

1条回答 默认 最新

  • dongxiangqian1855 2019-04-08 14:29
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog