Get All Unique Words in a Text File
【Question】
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 includes all the unique items from the above 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
But this does not give the results I want.
【Answer】
It’s convenient to do this in SPL (Structured Process Language):
A |
|
1 |
= file("D://Words.txt").read() |
2 |
=lower(A1) |
3 |
=A2.words().id() |
A1: Read in the CSV file as a string.
A2: Convert all starting letters which are uppercase into lowercase.
A3: Split the string into a sequence of words using words function and remove duplicate words with id function.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL