def wordcount(path)
file=File.open(path);a=IO.read(file).split(" ");b=[[a[0],1]];
for i in 1..a.length-1
for j in 0..b.length-1
if a[i].chomp(",").chomp(".")==b[j][0].chomp(",").chomp(".") then
b[j][1]=b[j][1]+1;c=false;break;
else
c=true;
end;
end;
if c==true then
c=false;b[b.length]=[a[i].chomp(",").chomp("."),1]
end;
end;
return(b);
end;
p(wordcount("lorem.txt"));
I wrote a Ruby program which returns an array of all different words (by ignoring dots or commata) with their count for a certain file path. It's not that nice but it works
raw="""
<PASTE THE TEXT HERE>
"""
# drop ponctuation
txt = raw.replace('.', ' ').replace(',', ' ')
# go get the word
list = txt.split()
word = [x for x in list if list.cont(x) == 1]
# here you are!!!
print(word)
#!/bin/ksh
string=$@
set -A words $string
for word in ${words[@]}; do
count=0
for check in ${words[@]}; do
if [[ "$check" = "$word" ]]; then
let count=$count+1
fi
done
echo $word - $count
done
echo ${array[*]}
exit
text = open('lorem.txt').read().lower().replace('.', '').replace(',', '')
textlist = text.split()
for word in textlist:
if text.count(word) == 1:
print(word)
words = {}
File.open('file', 'r').readlines.each do |line|
line.split.each do |word|
word.gsub!(/[[:punct:]]/, '')
if words[word]==nil
words.merge!(Hash[word=>0])
else
words[word]+=1
end
end
end
puts words.sort_by {|k,v|v}.first
a = open('lorem.txt').read().replace('.', '').replace(',', '').replace('\n', '').replace('\r', '').replace(':', '')
y = a.split()
l = {}
for k in y:
if l.has_key(k):
l.update({k: l[k]+1})
else:
l.update({k: 1})
print sorted(l.items(), key=lambda x:x[1])