• 如何获取文本中出现的所有单词


    【问题】

    I have this csv file

    Cat, and, dog, bites

    Yahoo, news, claims, a, cat, mated, with, a, dog, and, produced, viable, offspring

    Cat, killer, likely, is, a, big, dog

    Professional, free, advice, on, dog, training, puppy, training

    Cat, and, kitten, training, and, behavior

    Dog, &, Cat, provides, dog, training, in Eugene, Oregon

    Dog, and, cat, is, a, slang, term, used, by, police, officers, for, a, male-female, relationship

    Shop, for, your, show, dog, grooming, and, pet, supplies

    I want to make all the words start with a small letter and create a list which will include all the unique items from the above csv file. Have you any idea? Thanks in advance! So far, I have managed to convert all the words with a small letter:

    unique_row_items = set([field.strip().lower()for field in row])

    But i can't manage the other one.

    def unique():

        rows = list(csv.reader(open('example_1.csv', 'r'), delimiter=','))

        result = []

        for r in rows:

            key = r

            if key not in result:

                result.append(r)

        return result

    Which does not give the results I want

    【回答】

    这个问题用SPL实现很方便,脚本如下:

    A
    1= file("D://Words.txt").read()
    2=lower(A1)
    3=A2.words().id()

    A1:从文本读取字符串

    A2:将字符串转换成小写字母

    A3:通过 words 函数将字符串拆分成单个单词组成的序列,再通过 id 函数去除重复成员

     

  • 相关阅读:
    人工智能知识全面讲解:你真的了解数据吗?
    真正“搞”懂HTTP协议03之时间穿梭
    2022牛客暑期多校训练营5(BCDFGHK)
    2023年之我拿起“java“ java基础进阶1
    不使用EF框架实现数据库增删改查
    48.【Java 基础之对象数组(偏难)】
    HPC系统简介
    Java进阶-常用API
    RocketMQ源码解析-topic创建机制
    PG::Potato
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/125513256