Language agnostic 为给定文本中最常用的单词构建ASCII图表 挑战:
为给定文本中最常用的单词构建ASCII图表 规则:Language agnostic 为给定文本中最常用的单词构建ASCII图表 挑战:,language-agnostic,code-golf,Language Agnostic,Code Golf,为给定文本中最常用的单词构建ASCII图表 规则: x={};p='|';e=' ';z=[];c=77 while(l=readline()) l.toLowerCase().replace(/\b(?!(the|and|of|to|a|i[tns]?|or)\b)\w+/g, function(y) x[y] ? x[y].c++ : z.push( x[y] = {w: y, c: 1} ) ) z=z.sort(function(a,b) b.c - a.c).slice(
x={};p='|';e=' ';z=[];c=77
while(l=readline())
l.toLowerCase().replace(/\b(?!(the|and|of|to|a|i[tns]?|or)\b)\w+/g,
function(y) x[y] ? x[y].c++ : z.push( x[y] = {w: y, c: 1} )
)
z=z.sort(function(a,b) b.c - a.c).slice(0,22)
for each(v in z){
v.r=v.c/z[0].c
c=c>(l=(77-v.w.length)/v.r)?l:c
}
for(k in z){
v=z[k]
s=Array(v.r*c|0).join('_')
if(!+k)print(e+s+e)
print(p+s+p+e+v.w)
}
_________________________________________________________________________
|_________________________________________________________________________| she
|_______________________________________________________________| you
|____________________________________________________________| said
|____________________________________________________| alice
|______________________________________________| was
|___________________________________________| that
|___________________________________| as
|________________________________| her
|_____________________________| at
|_____________________________| with
|____________________________| s
|____________________________| t
|__________________________| on
|_________________________| all
|_______________________| this
|______________________| for
|______________________| had
|______________________| but
|_____________________| be
|_____________________| not
|___________________| they
|___________________| so
x={};p='|';e=' ';z=[]
readFile(arguments[0]).toLowerCase().replace(/\b(?!(the|and|of|to|a|i[tns]?|or)\b)\w+/g,function(y){x[y]?x[y].c++:z.push(x[y]={w:y,c:1})})
z=z.sort(function(a,b){return b.c-a.c}).slice(0,22)
for([k,v]in z){s=Array((v.c/z[0].c)*70|0).join('_')
if(!+k)print(e+s+e)
print(p+s+p+e+v.w)}
_________________________________________________________________________
|_________________________________________________________________________| she
|_______________________________________________________________| you
|____________________________________________________________| said
|_____________________________________________________| alice
|_______________________________________________| was
|___________________________________________| that
|____________________________________| as
|________________________________| her
|_____________________________| with
|_____________________________| at
|__________________________| on
|__________________________| all
|_______________________| this
|_______________________| for
|_______________________| had
|_______________________| but
|______________________| be
|_____________________| not
|____________________| they
|____________________| so
|___________________| very
|__________________| what
- 仅接受
和a-z
(字母字符)作为单词的一部分李>a-z
- 忽略大小写(
==She
用于我们的目的)李>She
- 忽略以下单词(我知道是相当随意的):
the,and,of,to,a,I,it,in,or,is
- 澄清:考虑到
:这将被视为don
和a-z
范围内的两个不同的“单词”:(a-z
和don
)t
- 可以选择(现在正式更改规范为时已晚)您可以选择删除所有单个字母的“单词”(这可能也会缩短忽略列表)
文本
(读取通过命令行参数或管道输入指定的文件;假定美国ascii
),并为我们构建一个具有以下特征的词频图
:
- 显示22个最常见单词的图表(另请参见下面的示例)(按降序排列)
- 条形码宽度表示单词出现的次数(频率)(按比例)。添加一个空格并打印单词
- 确保这些条(加空格和单词空格)始终合适:
+bar
+[space]
+word
应始终为Perl,237 229 209个字符 (再次更新,以使用更脏的高尔夫技巧击败Ruby版本,将[space]
替换为split/[^a-z/,lc
,并在另一个地方取消了对空字符串的检查。这些都是受Ruby版本的启发,因此值得称赞。) 更新:现在使用Perl 5.10!将lc=~/[a-z]+/g
替换为print
,并使用say
避免出现~
。这必须在命令行上以映射
的形式调用。由于整个脚本都在一行上,因此将其作为一行来编写应该没有任何困难:) 下面是在保持相对可读性的情况下,尽可能简短的内容(392个字符)Perl-E''alice.txt
C#-510 451 436 446 434 426 422字符(缩小) 没有那么短,但现在可能是正确的!注意,以前的版本没有显示第一行条形图,没有正确缩放条形图,下载了文件而不是从stdin获取,并且没有包含所有必需的C#详细信息。如果C#不需要那么多额外的废话,您可以轻松地剃掉许多笔划。也许Powershell可以做得更好%short=map{$\=>1}qw/and的和到i的或is/; %计数; $count{$\}++foreach grep{$\&!$short{$\}map{split/[^a-z]/,lc}(); @排序=(排序{$count{$b}$count{$a}}键%count)[0..21]; $widest=76-(长度$sorted[0]); 打印“.”。\ux$最宽。“\n”; 打印“|”ux int($count{$\u}/$count{$sorted[0]})*$widest)。“|$\un”foreach@sorted;
422个字符,其中lendivisor内联(速度慢22倍),格式如下(用于选定空格的换行符): F#,452查尔 Strightforward:获取单词计数对的序列using C=System.Console; // alias for Console using System.Linq; // for Split, GroupBy, Select, OrderBy, etc. class Class // must define a class { static void Main() // must define a Main { // split into words var allwords = System.Text.RegularExpressions.Regex.Split( // convert stdin to lowercase C.In.ReadToEnd().ToLower(), // eliminate stopwords and non-letters @"(?:\b(?:the|and|of|to|a|i[tns]?|or)\b|\W)+") .GroupBy(x => x) // group by words .OrderBy(x => -x.Count()) // sort descending by count .Take(22); // take first 22 words // compute length of longest bar + word var lendivisor = allwords.Max(y => y.Count() / (76.0 - y.Key.Length)); // prepare text to print var toPrint = allwords.Select(x=> new { // remember bar pseudographics (will be used in two places) Bar = new string('_',(int)(x.Count()/lendivisor)), Word=x.Key }) .ToList(); // convert to list so we can index into it // print top of first bar C.WriteLine(" " + toPrint[0].Bar); toPrint.ForEach(x => // for each word, print its bar and the word C.WriteLine("|" + x.Bar + "| " + x.Word)); } }
,找到每列乘法器的最佳单词计数a
,然后打印结果k
示例(我的频率计数与您不同,不确定原因):let a= stdin.ReadToEnd().Split(" .?!,\":;'\r\n".ToCharArray(),enum 1) |>Seq.map(fun s->s.ToLower())|>Seq.countBy id |>Seq.filter(fun(w,n)->not(set["the";"and";"of";"to";"a";"i";"it";"in";"or";"is"].Contains w)) |>Seq.sortBy(fun(w,n)-> -n)|>Seq.take 22 let k=a|>Seq.map(fun(w,n)->float(78-w.Length)/float n)|>Seq.min let u n=String.replicate(int(float(n)*k)-2)"_" printfn" %s "(u(snd(Seq.nth 0 a))) for(w,n)in a do printfn"|%s| %s "(u n)w
Gawk--336(最初为507)个字符 (在修复输出格式后;修复收缩;调整;再次调整;删除完全不必要的排序步骤;再次调整;再次调整(哎呀,这一次破坏了格式);调整更多;接受马特的挑战,我拼命调整更多;找到另一个地方节省了一些,但还给了两个以修复棒长度错误) 嘿嘿!我暂时领先于[Matt的JavaScript][1]solutioncounter挑战!;)和[AKX的python][2] 这个问题似乎需要一种实现本机关联数组的语言,所以我当然选择了一种运算符集严重不足的语言。特别是,您无法控制awk提供哈希映射元素的顺序,因此我反复扫描整个映射以查找当前数量最多的项print然后将其从数组中删除 这一切都是非常低效的,所有的高尔夫我已经做了它已经变得非常可怕,以及 缩小:%app.exe
{gsub("[^a-zA-Z]"," ");for(;NF;NF--)a[tolower($NF)]++} END{split("the and of to a i it in or is",b," "); for(w in b)delete a[b[w]];d=1;for(w in a){e=a[w]/(78-length(w));if(e>d)d=e} for(i=22;i;--i){e=0;for(w in a)if(a[w]>e)e=a[x=w];l=a[x]/d-2; t=sprintf(sprintf("%%%dc",l)," ");gsub(" ","_",t);if(i==22)print" "t; print"|"t"| "x;delete a[x]}}
换行符仅为清晰起见:它们不是必需的,不应计算在内<?php $a=array_count_values(array_filter(preg_split('/[^a-z]/',strtolower(file_get_contents($argv[1])),-1,1),function($x){return !preg_match("/^(.|the|and|of|to|it|in|or|is)$/",$x);}));arsort($a);$a=array_slice($a,0,22);function R($a,$F,$B){$r=array();foreach($a as$x=>$f){$l=strlen($x);$r[$x]=$b=$f*$B/$F;if($l+$b>76)return R($a,$f,76-$l);}return$r;}$c=R($a,max($a),76-strlen(key($a)));foreach($a as$x=>$f)echo '|',str_repeat('-',$c[$x]),"| $x\n";?>
输出:$ gawk -f wordfreq.awk.min < 11.txt _________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |____________________________________________________________| said |____________________________________________________| alice |______________________________________________| was |__________________________________________| that |___________________________________| as |_______________________________| her |____________________________| with |____________________________| at |___________________________| s |___________________________| t |_________________________| on |_________________________| all |______________________| this |______________________| for |______________________| had |_____________________| but |____________________| be |____________________| not |___________________| they |__________________| so $ sed 's/you/superlongstring/gI' 11.txt | gawk -f wordfreq.awk.min ______________________________________________________________________ |______________________________________________________________________| she |_____________________________________________________________| superlongstring |__________________________________________________________| said |__________________________________________________| alice |____________________________________________| was |_________________________________________| that |_________________________________| as |______________________________| her |___________________________| with |___________________________| at |__________________________| s |__________________________| t |________________________| on |________________________| all |_____________________| this |_____________________| for |_____________________| had |____________________| but |___________________| be |___________________| not |__________________| they |_________________| so
_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |___________________________________________| that |____________________________________| as |________________________________| her |_____________________________| with |_____________________________| at |____________________________| s |____________________________| t |__________________________| on |__________________________| all |_______________________| this |_______________________| for |_______________________| had |_______________________| but |______________________| be |_____________________| not |____________________| they |____________________| so
_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |___________________________________________| that |____________________________________| as |________________________________| her |_____________________________| with |_____________________________| at |__________________________| on |__________________________| all |_______________________| this |_______________________| for |_______________________| had |_______________________| but |______________________| be |_____________________| not |____________________| they |____________________| so |___________________| very |__________________| what_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |___________________________________________| that |____________________________________| as |________________________________| her |_____________________________| with |_____________________________| at |____________________________| s |____________________________| t |__________________________| on |__________________________| all |_______________________| this |_______________________| for |_______________________| had |_______________________| but |______________________| be |_____________________| not |____________________| they |____________________| so
*sh(+curl),部分解 这是不完整的,但更糟糕的是,这是在192字节中计算问题一半的词频:|-------------------------------------------------------------------------| she |---------------------------------------------------------------| you |------------------------------------------------------------| said |-----------------------------------------------------| alice |-----------------------------------------------| was |-------------------------------------------| that |------------------------------------| as |--------------------------------| her |-----------------------------| at |-----------------------------| with |--------------------------| on |--------------------------| all |-----------------------| this |-----------------------| for |-----------------------| had |-----------------------| but |----------------------| be |---------------------| not |--------------------| they |--------------------| so |-------------------| very |------------------| what
JavaScript 1.8(SpiderMonkey)-354 遗憾的是,Rhino版本中([k,v]in z)的curl -s http://www.gutenberg.org/files/11/11.txt|sed -e 's@[^a-z]@\n@gi'|tr '[:upper:]' '[:lower:]'|egrep -v '(^[^a-z]*$|\b(the|and|of|to|a|i|it|in|or|is)\b)' |sort|uniq -c|sort -n|tail -n 22
似乎不想在SpiderMonke中工作
curl -s http://www.gutenberg.org/files/11/11.txt|sed -e 's@[^a-z]@\n@gi'|tr '[:upper:]' '[:lower:]'|egrep -v '(^[^a-z]*$|\b(the|and|of|to|a|i|it|in|or|is)\b)' |sort|uniq -c|sort -n|tail -n 22
x={};p='|';e=' ';z=[];c=77 while(l=readline())l.toLowerCase().replace(/\b(?!(the|and|of|to|a|i[tns]?|or)\b)\w+/g,function(y)x[y]?x[y].c++:z.push(x[y]={w:y,c:1})) z=z.sort(function(a,b)b.c-a.c).slice(0,22) for each(v in z){v.r=v.c/z[0].c c=c>(l=(77-v.w.length)/v.r)?l:c}for(k in z){v=z[k] s=Array(v.r*c|0).join('_') if(!+k)print(e+s+e) print(p+s+p+e+v.w)}
_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |____________________________________________________________| said |____________________________________________________| alice |______________________________________________| was |___________________________________________| that |___________________________________| as |________________________________| her |_____________________________| at |_____________________________| with |____________________________| s |____________________________| t |__________________________| on |_________________________| all |_______________________| this |______________________| for |______________________| had |______________________| but |_____________________| be |_____________________| not |___________________| they |___________________| sox={};p='|';e=' ';z=[];c=77 while(l=readline()) l.toLowerCase().replace(/\b(?!(the|and|of|to|a|i[tns]?|or)\b)\w+/g, function(y) x[y] ? x[y].c++ : z.push( x[y] = {w: y, c: 1} ) ) z=z.sort(function(a,b) b.c - a.c).slice(0,22) for each(v in z){ v.r=v.c/z[0].c c=c>(l=(77-v.w.length)/v.r)?l:c } for(k in z){ v=z[k] s=Array(v.r*c|0).join('_') if(!+k)print(e+s+e) print(p+s+p+e+v.w) }
x={};p='|';e=' ';z=[] readFile(arguments[0]).toLowerCase().replace(/\b(?!(the|and|of|to|a|i[tns]?|or)\b)\w+/g,function(y){x[y]?x[y].c++:z.push(x[y]={w:y,c:1})}) z=z.sort(function(a,b){return b.c-a.c}).slice(0,22) for([k,v]in z){s=Array((v.c/z[0].c)*70|0).join('_') if(!+k)print(e+s+e) print(p+s+p+e+v.w)}
import re W,x={},"a and i in is it of or the to".split() [W.__setitem__(w,W.get(w,0)-1)for w in re.findall("[a-z]+",file("11.txt").read().lower())if w not in x] W=sorted(W.items(),key=lambda p:p[1])[:22] bm=(76.-len(W[0][0]))/W[0][1] U=lambda n:"_"*int(n*bm) print "".join(("%s\n|%s| %s "%((""if i else" "+U(n)),U(n),w))for i,(w,n)in enumerate(W))
_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |___________________________________________| that |____________________________________| as |________________________________| her |_____________________________| with |_____________________________| at |____________________________| s |____________________________| t |__________________________| on |__________________________| all |_______________________| this |_______________________| for |_______________________| had |_______________________| but |______________________| be |_____________________| not |____________________| they |____________________| so
w=(IO.read($_).downcase.scan(/[a-z]+/)-%w{the and of to a i it in or is}).reduce(Hash.new 0){|m,o|m[o]+=1;m}.sort_by{|k,v|-v}.take 22;m=76-w[0][0].size;puts' '+'_'*m;w.map{|x,f|puts"|#{'_'*(f*1.0/w[0][1]*m)}| #{x} "}
(IO.read($_).downcase.scan(/[a-z]+/)-%w{the and of to a i it in or is}).reduce(Hash.new 0){|m,o|m[o]+=1;m}.sort_by{|k,v|-v}.take(22).map{|x,f|@f||(@f=f;puts' '+'_'*(@m=76-x.size));puts"|#{'_'*(f*1.0/@f*@m)}| #{x} "}
string = File.read($_).downcase words = string.scan(/[a-z]+/i) allowed_words = words - %w{the and of to a i it in or is} sorted_words = allowed_words.group_by{ |x| x }.map{ |x,y| [x, y.size] }.sort{ |a,b| b[1] <=> a[1] }.take(22) highest_frequency = sorted_words.first highest_frequency_count = highest_frequency[1] highest_frequency_word = highest_frequency[0] word_length = highest_frequency_word.size widest = 76 - word_length puts " #{'_' * widest}" sorted_words.each do |word, freq| width = (freq * 1.0 / highest_frequency_count) * widest puts "|#{'_' * width}| #{word} " end
echo "Alice.txt" | ruby -ln GolfedWordFrequencies.rb
_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |___________________________________________| that |____________________________________| as |________________________________| her |_____________________________| with |_____________________________| at |____________________________| s |____________________________| t |__________________________| on |__________________________| all |_______________________| this |_______________________| for |_______________________| had |_______________________| but |______________________| be |_____________________| not |____________________| they |____________________| so
f[x_, y_] := Flatten[Take[x, All, y]]; BarChart[f[{##}, -1], BarOrigin -> Left, ChartLabels -> Placed[f[{##}, 1], After], Axes -> None ] & @@ Take[ SortBy[ Tally[ Select[ StringSplit[ToLowerCase[Import[i]], RegularExpression["\\W+"]], !MemberQ[{"the", "and", "of", "to", "a", "i", "it", "in", "or","is"}, #]&] ], Last], -22]
Import[] # Get The File ToLowerCase [] # To Lower Case :) StringSplit[ STRING , RegularExpression["\\W+"]] # Split By Words, getting a LIST Select[ LIST, !MemberQ[{LIST_TO_AVOID}, #]&] # Select from LIST except those words in LIST_TO_AVOID # Note that !MemberQ[{LIST_TO_AVOID}, #]& is a FUNCTION for the test Tally[LIST] # Get the LIST {word,word,..} and produce another {{word,counter},{word,counter}...} SortBy[ LIST ,Last] # Get the list produced bt tally and sort by counters Note that counters are the LAST element of {word,counter} Take[ LIST ,-22] # Once sorted, get the biggest 22 counters BarChart[f[{##}, -1], ChartLabels -> Placed[f[{##}, 1], After]] &@@ LIST # Get the list produced by Take as input and produce a bar chart f[x_, y_] := Flatten[Take[x, All, y]] # Auxiliary to get the list of the first or second element of lists of lists x_ dependending upon y # So f[{##}, -1] is the list of counters # and f[{##}, 1] is the list of words (labels for the chart)
f[x_, y_] := Flatten[Take[x, All, y]]; ListLogLogPlot[ Reverse[f[{##}, -1]], AxesLabel -> {"Log (Rank)", "Log Counter"}, PlotLabel -> "Testing Zipf's Law"] & @@ Take[ SortBy[ Tally[ StringSplit[ToLowerCase[b], RegularExpression["\\W+"]] ], Last], -1000]
f = Flatten[Take[#1, All, #2]]&; BarChart[ f[{##}, -1], BarOrigin -> Left, ChartLabels -> Placed[f[{##}, 1], After], Axes -> None] & @@ Take[ SortBy[ Tally[ StringSplit[ToLowerCase[Import[i]], RegularExpression["(\\W|\\b(.|the|and|of|to|i[tns]|or)\\b)+"]] ], Last], -22]
BarChart[#2, BarOrigin->Left, ChartLabels->Placed[#1, After], Axes->None]&@@ Transpose@Take[SortBy[Tally@StringSplit[ToLowerCase@Import@i, RegularExpression@"(\\W|\\b(.|the|and|of|to|i[tns]|or)\\b)+"],Last], -22]
import java.util.*;import java.io.*;import static java.util.regex.Pattern.*;class g{public static void main(String[] a)throws Exception{PrintStream o=System.out;Map<String,Integer> w=new HashMap();Scanner s=new Scanner(new File(a[0])).useDelimiter(compile("[^a-z]+|\\b(the|and|of|to|.|it|in|or|is)\\b",2));while(s.hasNext()){String z=s.next().trim().toLowerCase();if(z.equals(""))continue;w.put(z,(w.get(z)==null?0:w.get(z))+1);}List<Integer> v=new Vector(w.values());Collections.sort(v);List<String> q=new Vector();int i,m;i=m=v.size()-1;while(q.size()<22){for(String t:w.keySet())if(!q.contains(t)&&w.get(t).equals(v.get(i)))q.add(t);i--;}int r=80-q.get(0).length()-4;String l=String.format("%1$0"+r+"d",0).replace("0","_");o.println(" "+l);o.println("|"+l+"| "+q.get(0)+" ");for(i=m-1;i>m-22;i--){o.println("|"+l.substring(0,(int)Math.round(r*(v.get(i)*1.0)/v.get(m)))+"| "+q.get(m-i)+" ");}}}
import java.util.*; import java.io.*; import static java.util.regex.Pattern.*; class g { public static void main(String[] a)throws Exception { PrintStream o = System.out; Map<String,Integer> w = new HashMap(); Scanner s = new Scanner(new File(a[0])) .useDelimiter(compile("[^a-z]+|\\b(the|and|of|to|.|it|in|or|is)\\b",2)); while(s.hasNext()) { String z = s.next().trim().toLowerCase(); if(z.equals("")) continue; w.put(z,(w.get(z) == null?0:w.get(z))+1); } List<Integer> v = new Vector(w.values()); Collections.sort(v); List<String> q = new Vector(); int i,m; i = m = v.size()-1; while(q.size()<22) { for(String t:w.keySet()) if(!q.contains(t)&&w.get(t).equals(v.get(i))) q.add(t); i--; } int r = 80-q.get(0).length()-4; String l = String.format("%1$0"+r+"d",0).replace("0","_"); o.println(" "+l); o.println("|"+l+"| "+q.get(0)+" "); for(i = m-1; i > m-22; i--) { o.println("|"+l.substring(0,(int)Math.round(r*(v.get(i)*1.0)/v.get(m)))+"| "+q.get(m-i)+" "); } } }
_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |_____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |____________________________________________| that |____________________________________| as |_________________________________| her |______________________________| with |______________________________| at |___________________________| on |__________________________| all |________________________| this |________________________| for |_______________________| had |_______________________| but |______________________| be |______________________| not |____________________| they |____________________| so |___________________| very |___________________| what
________________________________________________________________________ |________________________________________________________________________| that |________________________________________________________| he |______________________________________________| for |__________________________________________| his |________________________________________| as |__________________________________| with |_________________________________| not |_________________________________| was |________________________________| him |______________________________| be |___________________________| don |_________________________| my |_________________________| this |_________________________| all |_________________________| they |________________________| said |_______________________| have |_______________________| me |______________________| on |______________________| so |_____________________| you |_____________________| quixote
import java.util.*;class F{public static void main(String[]a)throws Exception{StringBuffer b=new StringBuffer();for(int c;(c=System.in.read())>0;b.append((char)c));final Map<String,Integer>m=new HashMap();for(String w:b.toString().toLowerCase().split("(\\b(.|the|and|of|to|i[tns]|or)\\b|\\W)+"))m.put(w,m.get(w)!=null?m.get(w)+1:1);List<String>l=new Vector(m.keySet());Collections.sort(l,new Comparator(){public int compare(Object l,Object r){return m.get(r)-m.get(l);}});int c=76-l.get(0).length();String s=new String(new char[c]).replace('\0','_');System.out.println(" "+s);for(String w:l.subList(0,22))System.out.println("|"+s.substring(0,m.get(w)*c/m.get(l.get(0)))+"| "+w);}}
_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |___________________________________________| that |____________________________________| as |________________________________| her |_____________________________| with |_____________________________| at |__________________________| on |__________________________| all |_______________________| this |_______________________| for |_______________________| had |_______________________| but |______________________| be |_____________________| not |____________________| they |____________________| so |___________________| very |__________________| whatimport java.util.*; class F{ public static void main(String[]a)throws Exception{ StringBuffer b=new StringBuffer();for(int c;(c=System.in.read())>0;b.append((char)c)); final Map<String,Integer>m=new HashMap();for(String w:b.toString().toLowerCase().split("(\\b(.|the|and|of|to|i[tns]|or)\\b|\\W)+"))m.put(w,m.get(w)!=null?m.get(w)+1:1); List<String>l=new Vector(m.keySet());Collections.sort(l,new Comparator(){public int compare(Object l,Object r){return m.get(r)-m.get(l);}}); int c=76-l.get(0).length();String s=new String(new char[c]).replace('\0','_');System.out.println(" "+s); for(String w:l.subList(0,22))System.out.println("|"+s.substring(0,m.get(w)*c/m.get(l.get(0)))+"| "+w); } }
import java.util.*;class F{public static void main(String[]l)throws Exception{Map<String,Integer>m=new HashMap();String w="";int i=0,k=0,j=8,x,y,g=22;for(;(j=System.in.read())>0;w+=(char)j);for(String W:w.toLowerCase().split("(\\b(.|the|and|of|to|i[tns]|or)\\b|\\W)+"))m.put(W,m.get(W)!=null?m.get(W)+1:1);l=m.keySet().toArray(l);x=l.length;if(x<g)g=x;for(;i<g;++i)for(j=i;++j<x;)if(m.get(l[i])<m.get(l[j])){w=l[i];l[i]=l[j];l[j]=w;}for(;k<g;k++){x=76-l[k].length();y=m.get(l[k]);if(k<1||y*i>x*j){i=x;j=y;}}String s=new String(new char[m.get(l[0])*i/j]).replace('\0','_');System.out.println(" "+s);for(k=0;k<g;k++){w=l[k];System.out.println("|"+s.substring(0,m.get(w)*i/j)+"| "+w);}}}
import java.util.*;class F{public static void main(String[]l)throws Exception{Map<String,Integer>m=new HashMap();String w="";int i=0,k=0,j=8,x,y,g=22;for(;j>0;){j=System.in.read();if(j>90)j-=32;if(j>64&j<91)w+=(char)j;else{if(!w.matches("^(|.|THE|AND|OF|TO|I[TNS]|OR)$"))m.put(w,m.get(w)!=null?m.get(w)+1:1);w="";}}l=m.keySet().toArray(l);x=l.length;if(x<g)g=x;for(;i<g;++i)for(j=i;++j<x;)if(m.get(l[i])<m.get(l[j])){w=l[i];l[i]=l[j];l[j]=w;}for(;k<g;k++){x=76-l[k].length();y=m.get(l[k]);if(k<1||y*i>x*j){i=x;j=y;}}String s=new String(new char[m.get(l[0])*i/j]).replace('\0','_');System.out.println(" "+s);for(k=0;k<g;k++){w=l[k];System.out.println("|"+s.substring(0,m.get(w)*i/j)+"| "+w);}}}
import java.util.*;class F{public static void main(String[]l)throws Exception{ Map<String,Integer>m=new HashMap();String w=""; int i=0,k=0,j=8,x,y,g=22; for(;j>0;){j=System.in.read();if(j>90)j-=32;if(j>64&j<91)w+=(char)j;else{ if(!w.matches("^(|.|THE|AND|OF|TO|I[TNS]|OR)$"))m.put(w,m.get(w)!=null?m.get(w)+1:1);w=""; }} l=m.keySet().toArray(l);x=l.length;if(x<g)g=x; for(;i<g;++i)for(j=i;++j<x;)if(m.get(l[i])<m.get(l[j])){w=l[i];l[i]=l[j];l[j]=w;} for(;k<g;k++){x=76-l[k].length();y=m.get(l[k]);if(k<1||y*i>x*j){i=x;j=y;}} String s=new String(new char[m.get(l[0])*i/j]).replace('\0','_'); System.out.println(" "+s); for(k=0;k<g;k++){w=l[k];System.out.println("|"+s.substring(0,m.get(w)*i/j)+"| "+w);}} }
import java.util.*;class F{public static void main(String[]l)throws Exception{Map<String,Integer>m=new HashMap();String w="";int i=0,k=0,j=8,g=22;for(;j>0;){j=System.in.read();if(j>90)j-=32;if(j>64&j<91)w+=(char)j;else{if(!w.matches("^(|.|THE|AND|OF|TO|I[TNS]|OR)$"))m.put(w,m.get(w)!=null?m.get(w)+1:1);w="";}}l=m.keySet().toArray(l);for(;i<g;++i)for(j=i;++j<l.length;)if(m.get(l[i])<m.get(l[j])){w=l[i];l[i]=l[j];l[j]=w;}i=76-l[0].length();String s=new String(new char[i]).replace('\0','_');System.out.println(" "+s);for(k=0;k<g;k++){w=l[k];System.out.println("|"+s.substring(0,m.get(w)*i/m.get(l[0]))+"| "+w);}}}
import sys,re t=re.split('\W+',sys.stdin.read().lower()) r=sorted((-t.count(w),w)for w in set(t)if w not in'andithetoforinis')[:22] for l,w in r:print(78-len(r[0][1]))*l/r[0][0]*'=',w
=========================================================================== she ================================================================= you ============================================================== said ====================================================== alice ================================================ was ============================================ that ===================================== as ================================= her ============================== at ============================== with =========================== on =========================== all ======================== this ======================== had ======================= but ====================== be ====================== not ===================== they ==================== so =================== very =================== what ================= little
import sys,re t=re.split('\W+',sys.stdin.read().lower()) r=sorted((-t.count(w),w)for w in set(t)-set(sys.argv))[:22] h=min(9*l/(77-len(w))for l,w in r) print'',9*r[0][0]/h*'_' for l,w in r:print'|'+9*l/h*'_'+'|',w
_______________________________________________________________ |_______________________________________________________________| she |_______________________________________________________| superlongstringstring |_____________________________________________________| said |______________________________________________| alice |_________________________________________| was |______________________________________| that |_______________________________| as |____________________________| her |__________________________| at |__________________________| with |_________________________| s |_________________________| t |_______________________| on |_______________________| all |____________________| this |____________________| for |____________________| had |____________________| but |___________________| be |___________________| not |_________________| they |_________________| so
w=(STDIN.read.downcase.scan(/[a-z]+/)-%w{the and of to a i it in or is}).group_by{|x|x}.map{|x,y|[-y.size,x]}.sort.take 22;k,l=w[0];m=76.0-l.size;puts' '+'_'*m;w.map{|f,x|puts"|#{'_'*(m*f/k)}| #{x} "}
ruby GolfedWordFrequencies.rb < Alice.txt
~ % wc -c wfg 209 wfg ~ % cat wfg egrep -oi \\b[a-z]+|tr A-Z a-z|egrep -wv 'the|and|of|to|a|i|it|in|or|is'|sort|uniq -c|sort -nr|head -22|perl -lape'($f,$w)=@F;$.>1or($q,$x)=($f,76-length$w);$b="_"x($f/$q*$x);$_="|$b| $w ";$.>1or$_=" $b\n$_"' ~ % # usage: ~ % sh wfg < 11.txt
egrep -oi \\b[a-z]+|tr A-Z a-z|egrep -wv 'the|and|o[fr]|to|a|i[tns]?'|sort|uniq -c|sort -nr|head -22|perl -lape'($f,$w)=@F;$.>1or($q,$x)=($f,76-length$w);$b="_"x($f/$q*$x);$_="|$b| $w ";$.>1or$_=" $b\n$_"'
~ % wc -c pgolf 204 pgolf ~ % cat pgolf perl -lne'$1=~/^(the|and|o[fr]|to|.|i[tns])$/i||$f{lc$1}++while/\b([a-z]+)/gi}{@w=(sort{$f{$b}<=>$f{$a}}keys%f)[0..21];$Q=$f{$_=$w[0]};$B=76-y///c;print" "."_"x$B;print"|"."_"x($B*$f{$_}/$Q)."| $_"for@w' ~ % # usage: ~ % sh pgolf < 11.txt
{32|.123%97<n@if}%]''*n%"oftoitinorisa"2/-"theandi"3/-$(1@{.3$>1{;)}if}/]2/{~~\;}$22<.0=~:2;,76\-:1'_':0*' '\@{" |"\~1*2/0*'| '@}/
{ #loop through all characters 32|. #convert to uppercase and duplicate 123%97< #determine if is a letter n@if #return either the letter or a newline }% #return an array (of ints) ]''* #convert array to a string with magic n% #split on newline, removing blanks (stack is an array of words now) "oftoitinorisa" #push this string 2/ #split into groups of two, i.e. ["of" "to" "it" "in" "or" "is" "a"] - #remove any occurrences from the text "theandi"3/-#remove "the", "and", and "i" $ #sort the array of words (1@ #takes the first word in the array, pushes a 1, reorders stack #the 1 is the current number of occurrences of the first word { #loop through the array .3$>1{;)}if#increment the count or push the next word and a 1 }/ ]2/ #gather stack into an array and split into groups of 2 {~~\;}$ #sort by the latter element - the count of occurrences of each word 22< #take the first 22 elements .0=~:2; #store the highest count ,76\-:1 #store the length of the first line '_':0*' '\@ #make the first line { #loop through each word " |"\~ #start drawing the bar 1*2/0 #divide by zero *'| '@ #finish drawing the bar }/
{32|.123%97<n@if}%]''*n%"oftoitinorisa"2/-"theandi"3/-$(1@{.3$>1{;)}if}/]2/{~~\;}$22<..0=1=:^;{~76@,-^*\/}%$0=:1'_':0*' '\@{" |"\~1*^/0*'| '@}/
'"'/' ':S*n/S*'"#{%q '\+" .downcase.tr('^a-z',' ')}\""+~n%"oftoitinorisa"2/-"theandi"3/-$(1@{.3$>1{;)}if}/]2/{~~\;}$22<.0=~:2;,76\-:1'_':0*S\@{" |"\~1*2/0*'| '@}/
$x=$input-split'\P{L}'-notmatch'^(the|and|of|to|.?|i[tns]|or)$'|group|sort * filter f($w){' '+'_'*$w $x[-1..-22]|%{"|$('_'*($w*$_.Count/$x[-1].Count))| "+$_.Name}} f(76..1|?{!((f $_)-match'.'*80)})[0]
($x=$input-split'\P{L}'-notmatch'^(the|and|of|to|.?|i[tns]|or)$'|group|sort *)[-1..-22]|%{"|$('_'*(76*$_.Count/$x[-1].Count))| "+$_.Name}
w=($<.read.downcase.scan(/[a-z]+/)-%w{the and of to a i it in or is}).group_by{|x|x}.map{|x,y|[-y.size,x]}.sort[0,22] k,l=w[0] puts [?\s+?_*m=76-l.size,w.map{|f,x|?|+?_*(f*m/k)+"| "+x}]
object Alice { def main(args:Array[String]) { val s = io.Source.fromFile(args(0)) val words = s.getLines.flatMap("(?i)\\w+\\b(?<!\\bthe|and|of|to|a|i|it|in|or|is)".r.findAllIn(_)).map(_.toLowerCase) val freqs = words.foldLeft(Map[String, Int]())((countmap, word) => countmap + (word -> (countmap.getOrElse(word, 0)+1))) val sortedFreqs = freqs.toList.sort((a, b) => a._2 > b._2) val top22 = sortedFreqs.take(22) val highestWord = top22.head._1 val highestCount = top22.head._2 val widest = 76 - highestWord.length println(" " + "_" * widest) top22.foreach(t => { val width = Math.round((t._2 * 1.0 / highestCount) * widest).toInt println("|" + "_" * width + "| " + t._1) }) } }
$ scalac alice.scala $ scala Alice aliceinwonderland.txt _________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |_____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |____________________________________________| that |____________________________________| as |_________________________________| her |______________________________| at |______________________________| with |_____________________________| s |_____________________________| t |___________________________| on |__________________________| all |_______________________| had |_______________________| but |______________________| be |______________________| not |____________________| they |____________________| so |___________________| very |___________________| what
object A{def main(args:Array[String]){val l=io.Source.fromFile(args(0)).getLines.flatMap("(?i)\\w+\\b(?<!\\bthe|and|of|to|a|i|it|in|or|is)".r.findAllIn(_)).map(_.toLowerCase).foldLeft(Map[String, Int]())((c,w)=>c+(w->(c.getOrElse(w,0)+1))).toList.sort((a,b)=>a._2>b._2).take(22);println(" "+"_"*(76-l.head._1.length));l.foreach(t=>println("|"+"_"*Math.round((t._2*1.0/l.head._2)*(76-l.head._1.length)).toInt+"| "+t._1))}}
$ scalac a.scala $ scala A aliceinwonderland.txt _________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |_____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |____________________________________________| that |____________________________________| as |_________________________________| her |______________________________| at |______________________________| with |_____________________________| s |_____________________________| t |___________________________| on |__________________________| all |_______________________| had |_______________________| but |______________________| be |______________________| not |____________________| they |____________________| so |___________________| very |___________________| what
object A{def main(a:Array[String]){val t=(Map[String, Int]()/:(for(x<-io.Source.fromFile(a(0)).getLines;y<-"(?i)\\w+\\b(?<!\\bthe|and|of|to|a|i|it|in|or|is)".r findAllIn x) yield y.toLowerCase).toList)((c,x)=>c+(x->(c.getOrElse(x,0)+1))).toList.sortBy(_._2).reverse.take(22);val w=76-t.head._1.length;print(" "+"_"*w);t map (s=>"\n|"+"_"*(s._2*w/t.head._2)+"| "+s._1) foreach print}}
object Alice { def main(a:Array[String]) { val t = (Map[String, Int]() /: ( for ( x <- io.Source.fromFile(a(0)).getLines y <- "(?i)\\w+\\b(?<!\\bthe|and|of|to|a|i|it|in|or|is)".r.findAllIn(x) ) yield y.toLowerCase ).toList)((c, x) => c + (x -> (c.getOrElse(x, 0) + 1))).toList.sortBy(_._2).reverse.take(22) val w = 76 - t.head._1.length print (" "+"_"*w) t.map(s => "\n|" + "_" * (s._2 * w / t.head._2) + "| " + s._1).foreach(print) } }
$k{$_}++for grep{$_!~/^(the|and|of|to|a|i|it|in|or|is)$/}map{lc=~/[a-z]+/g}<>;@t=sort{$k{$b}<=>$k{$a}}keys%k;$l=76-length$t[0];printf" %s ",'_'x$l;printf"|%s| $_ ",'_'x int$k{$_}/$k{$t[0]}*$l for@t[0..21];
/^(the|and|of|to|.|i[tns]|or)$/||$k{$_}++for map{lc=~/[a-z]+/g}<>;@e=sort{$k{$b}<=>$k{$a}}keys%k;$n=" %s ";$r=(76-y///c)/$k{$_=$e[0]};map{printf$n,'_'x($k{$_}*$r),$_;$n="|%s| %s "}@e[0,0..21]
/^(the|and|of|to|.|i[tns]|or)$/||$k{$_}++for map{lc=~/[a-z]+/g}<>;@_=sort{$k{$b}<=>$k{$a}}keys%k;$n=" %s ";$r=(76-m//)/$k{$_=$_[0]};map{printf$n,'_'x($k{$_}*$r),$_;$n="|%s| %s "}@_[0,0..21]
/^(the|and|of|to|.|i[tns]|or)$/||$k{$_}++for map{lc=~/[a-z]+/g}<>;($r)=sort{$a<=>$b}map{(76-y///c)/$k{$_}}@e=sort{$k{$b}<=>$k{$a}}keys%k;$n=" %s ";map{printf$n,'_'x($k{$_}*$r),$_;$n="|%s| %s ";}@e[0,0..21]
$/=\0;/^(the|and|of|to|.|i[tns]|or)$/i||$x{lc$_}++for<>=~/[a-z]+/gi;map{$z=$x{$_};$y||{$y=(76-y///c)/$z}&&warn" "."_"x($z*$y)."\n";printf"|%.78s\n","_"x($z*$y)."| $_"}(sort{$x{$b}<=>$x{$a}}keys%x)[0..21]
$/=\0;/^(the|and|of|to|.|i[tns]|or)$/i||$x{lc$_}++for<>=~/[a-z]+/gi;@e=(sort{$x{$b}<=>$x{$a}}keys%x)[0..21];for(@e){$p=(76-y///c)/$x{$_};($y&&$p>$y)||($y=$p)}warn" "."_"x($x{$e[0]}*$y)."\n";for(@e){warn"|"."_"x($x{$_}*$y)."| $_\n"}
_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| you |____________________________________________________________| said |_____________________________________________________| alice |_______________________________________________| was |___________________________________________| that |____________________________________| as |________________________________| her |_____________________________| with |_____________________________| at |__________________________| on |__________________________| all |_______________________| this |_______________________| for |_______________________| had |_______________________| but |______________________| be |_____________________| not |____________________| they |____________________| so |___________________| very |__________________| what
_______________________________________________________________ |_______________________________________________________________| she |_______________________________________________________| superlongstringstring |____________________________________________________| said |______________________________________________| alice |________________________________________| was |_____________________________________| that |_______________________________| as |____________________________| her |_________________________| with |_________________________| at |_______________________| on |______________________| all |____________________| this |____________________| for |____________________| had |____________________| but |___________________| be |__________________| not |_________________| they |_________________| so |________________| very |________________| what
import re,collections o=collections.Counter([w for w in re.findall("[a-z]+",open("!").read().lower())if w not in"a and i in is it of or the to".split()]).most_common(22) print('\n'.join('|'+76*v//o[0][1]*'_'+'| '+k for k,v in o))
|____________________________________________________________________________| she |__________________________________________________________________| you |_______________________________________________________________| said |_______________________________________________________| alice |_________________________________________________| was |_____________________________________________| that |_____________________________________| as |__________________________________| her |_______________________________| with |_______________________________| at |______________________________| s |_____________________________| t |____________________________| on |___________________________| all |________________________| this |________________________| for |________________________| had |________________________| but |______________________| be |______________________| not |_____________________| they |____________________| so
import Data.List import Data.Char l=length t=filter m=map f c|isAlpha c=toLower c|0<1=' ' h w=(-l w,head w) x!(q,w)='|':replicate(minimum$m(q?)x)'_'++"| "++w q?(g,w)=q*(77-l w)`div`g b x=m(x!)x a(l:r)=(' ':t(=='_')l):l:r main=interact$unlines.a.b.take 22.sort.m h.group.sort .t(`notElem`words"the and of to a i it in or is").words.m f
<?php $a=array_count_values(array_filter(preg_split('/[^a-z]/',strtolower(file_get_contents($argv[1])),-1,1),function($x){return !preg_match("/^(.|the|and|of|to|it|in|or|is)$/",$x);}));arsort($a);$a=array_slice($a,0,22);function R($a,$F,$B){$r=array();foreach($a as$x=>$f){$l=strlen($x);$r[$x]=$b=$f*$B/$F;if($l+$b>76)return R($a,$f,76-$l);}return$r;}$c=R($a,max($a),76-strlen(key($a)));foreach($a as$x=>$f)echo '|',str_repeat('-',$c[$x]),"| $x\n";?>
<?php // Read: $s = strtolower(file_get_contents($argv[1])); // Split: $a = preg_split('/[^a-z]/', $s, -1, PREG_SPLIT_NO_EMPTY); // Remove unwanted words: $a = array_filter($a, function($x){ return !preg_match("/^(.|the|and|of|to|it|in|or|is)$/",$x); }); // Count: $a = array_count_values($a); // Sort: arsort($a); // Pick top 22: $a=array_slice($a,0,22); // Recursive function to adjust bar widths // according to the last requirement: function R($a,$F,$B){ $r = array(); foreach($a as $x=>$f){ $l = strlen($x); $r[$x] = $b = $f * $B / $F; if ( $l + $b > 76 ) return R($a,$f,76-$l); } return $r; } // Apply the function: $c = R($a,max($a),76-strlen(key($a))); // Output: foreach ($a as $x => $f) echo '|',str_repeat('-',$c[$x]),"| $x\n"; ?>
|-------------------------------------------------------------------------| she |---------------------------------------------------------------| you |------------------------------------------------------------| said |-----------------------------------------------------| alice |-----------------------------------------------| was |-------------------------------------------| that |------------------------------------| as |--------------------------------| her |-----------------------------| at |-----------------------------| with |--------------------------| on |--------------------------| all |-----------------------| this |-----------------------| for |-----------------------| had |-----------------------| but |----------------------| be |---------------------| not |--------------------| they |--------------------| so |-------------------| very |------------------| what
|--------------------------------------------------------| she |---------------------------------------------------| thisisareallylongwordhere |-------------------------------------------------| you |-----------------------------------------------| said |-----------------------------------------| alice |------------------------------------| was |---------------------------------| that |---------------------------| as |-------------------------| her |-----------------------| with |-----------------------| at |--------------------| on |--------------------| all |------------------| this |------------------| for |------------------| had |-----------------| but |-----------------| be |----------------| not |---------------| they |---------------| so |--------------| very
DECLARE @ VARCHAR(MAX),@F REAL SELECT @=BulkColumn FROM OPENROWSET(BULK'A', SINGLE_BLOB)x;WITH N AS(SELECT 1 i,LEFT(@,1)L UNION ALL SELECT i+1,SUBSTRING (@,i+1,1)FROM N WHERE i<LEN(@))SELECT i,L,i-RANK()OVER(ORDER BY i)R INTO #D FROM N WHERE L LIKE'[A-Z]'OPTION(MAXRECURSION 0)SELECT TOP 22 W,-COUNT(*)C INTO # FROM(SELECT DISTINCT R,(SELECT''+L FROM #D WHERE R=b.R FOR XML PATH (''))W FROM #D b)t WHERE LEN(W)>1 AND W NOT IN('the','and','of','to','it', 'in','or','is')GROUP BY W ORDER BY C SELECT @F=MIN(($76-LEN(W))/-C),@=' '+ REPLICATE('_',-MIN(C)*@F)+' 'FROM # SELECT @=@+' |'+REPLICATE('_',-C*@F)+'| '+W FROM # ORDER BY C PRINT @
DECLARE @ VARCHAR(MAX), @F REAL SELECT @=BulkColumn FROM OPENROWSET(BULK'A',SINGLE_BLOB)x; /* Loads text file from path C:\WINDOWS\system32\A */ /*Recursive common table expression to generate a table of numbers from 1 to string length (and associated characters)*/ WITH N AS (SELECT 1 i, LEFT(@,1)L UNION ALL SELECT i+1, SUBSTRING(@,i+1,1) FROM N WHERE i<LEN(@) ) SELECT i, L, i-RANK()OVER(ORDER BY i)R /*Will group characters from the same word together*/ INTO #D FROM N WHERE L LIKE'[A-Z]'OPTION(MAXRECURSION 0) /*Assuming case insensitive accent sensitive collation*/ SELECT TOP 22 W, -COUNT(*)C INTO # FROM (SELECT DISTINCT R, (SELECT ''+L FROM #D WHERE R=b.R FOR XML PATH('') )W /*Reconstitute the word from the characters*/ FROM #D b ) T WHERE LEN(W)>1 AND W NOT IN('the', 'and', 'of' , 'to' , 'it' , 'in' , 'or' , 'is') GROUP BY W ORDER BY C /*Just noticed this looks risky as it relies on the order of evaluation of the variables. I'm not sure that's guaranteed but it works on my machine :-) */ SELECT @F=MIN(($76-LEN(W))/-C), @ =' ' +REPLICATE('_',-MIN(C)*@F)+' ' FROM # SELECT @=@+' |'+REPLICATE('_',-C*@F)+'| '+W FROM # ORDER BY C PRINT @
_________________________________________________________________________ |_________________________________________________________________________| she |_______________________________________________________________| You |____________________________________________________________| said |_____________________________________________________| Alice |_______________________________________________| was |___________________________________________| that |____________________________________| as |________________________________| her |_____________________________| at |_____________________________| with |__________________________| on |__________________________| all |_______________________| This |_______________________| for |_______________________| had |_______________________| but |______________________| be |_____________________| not |____________________| they |____________________| So |___________________| very |__________________| what
_______________________________________________________________ |_______________________________________________________________| she |_______________________________________________________| superlongstringstring |____________________________________________________| said |______________________________________________| Alice |________________________________________| was |_____________________________________| that |_______________________________| as |____________________________| her |_________________________| at |_________________________| with |_______________________| on |______________________| all |____________________| This |____________________| for |____________________| had |____________________| but |___________________| be |__________________| not |_________________| they |_________________| So |________________| very |________________| what
#include <glib.h> #define S(X)g_string_##X #define H(X)g_hash_table_##X GHashTable*h;int m,w=0,z=0;y(const void*a,const void*b){int*A,*B;A=H(lookup)(h,a);B=H(lookup)(h,b);return*B-*A;}void p(void*d,void*u){int *v=H(lookup)(h,d);if(w<22){g_printf("|");*v=*v*(77-z)/m;while(--*v>=0)g_printf("=");g_printf("| %s\n",d);w++;}}main(c){int*v;GList*l;GString*s=S(new)(NULL);h=H(new)(g_str_hash,g_str_equal);char*n[]={"the","and","of","to","it","in","or","is"};while((c=getchar())!=-1){if(isalpha(c))S(append_c)(s,tolower(c));else{if(s->len>1){for(c=0;c<8;c++)if(!strcmp(s->str,n[c]))goto x;if((v=H(lookup)(h,s->str))!=NULL)++*v;else{z=MAX(z,s->len);v=g_malloc(sizeof(int));*v=1;H(insert)(h,g_strdup(s->str),v);}}x:S(truncate)(s,0);}}l=g_list_sort(H(get_keys)(h),y);m=*(int*)H(lookup)(h,g_list_first(l)->data);g_list_foreach(l,p,NULL);}
(flet((r()(let((x(read-char t nil)))(and x(char-downcase x)))))(do((c( make-hash-table :test 'equal))(w NIL)(x(r)(r))y)((not x)(maphash(lambda (k v)(if(not(find k '("""the""and""of""to""a""i""it""in""or""is"):test 'equal))(push(cons k v)y)))c)(setf y(sort y #'> :key #'cdr))(setf y (subseq y 0(min(length y)22)))(let((f(apply #'min(mapcar(lambda(x)(/(- 76.0(length(car x)))(cdr x)))y))))(flet((o(n)(dotimes(i(floor(* n f))) (write-char #\_))))(write-char #\Space)(o(cdar y))(write-char #\Newline) (dolist(x y)(write-char #\|)(o(cdr x))(format t "| ~a~%"(car x)))))) (cond((char<= #\a x #\z)(push x w))(t(incf(gethash(concatenate 'string( reverse w))c 0))(setf w nil)))))
(flet ((r () (let ((x (read-char t nil))) (and x (char-downcase x))))) (do ((c (make-hash-table :test 'equal)) ; the word count map w y ; current word and final word list (x (r) (r))) ; iteration over all chars ((not x) ; make a list with (word . count) pairs removing stopwords (maphash (lambda (k v) (if (not (find k '("" "the" "and" "of" "to" "a" "i" "it" "in" "or" "is") :test 'equal)) (push (cons k v) y))) c) ; sort and truncate the list (setf y (sort y #'> :key #'cdr)) (setf y (subseq y 0 (min (length y) 22))) ; find the scaling factor (let ((f (apply #'min (mapcar (lambda (x) (/ (- 76.0 (length (car x))) (cdr x))) y)))) ; output (flet ((outx (n) (dotimes (i (floor (* n f))) (write-char #\_)))) (write-char #\Space) (outx (cdar y)) (write-char #\Newline) (dolist (x y) (write-char #\|) (outx (cdr x)) (format t "| ~a~%" (car x)))))) ; add alphabetic to current word, and bump word counter ; on non-alphabetic (cond ((char<= #\a x #\z) (push x w)) (t (incf (gethash (concatenate 'string (reverse w)) c 0)) (setf w nil)))))
val t="\\w+\\b(?<!\\bthe|and|of|to|a|i[tns]?|or)".r.findAllIn(io.Source.fromFile(argv(0)).mkString.toLowerCase).toSeq.groupBy(w=>w).mapValues(_.size).toSeq.sortBy(-_._2)take 22 def b(p:Int)="_"*(p*(for((w,c)<-t)yield(76.0-w.size)/c).min).toInt println(" "+b(t(0)._2)) for(p<-t)printf("|%s| %s \n",b(p._2),p._1)
scala -howtorun:script a.scala alice.txt
map$X{+lc}+=!/^(.|the|and|to|i[nst]|o[rf])$/i,/[a-z]+/gfor<>; $n=$n>($:=$X{$_}/(76-y+++c))?$n:$:for@w=(sort{$X{$b}-$X{$a}}%X)[0..21]; die map{$U='_'x($X{$_}/$n);" $U "x!$z++,"|$U| $_ "}@w
(let[[[_ m]:as s](->>(slurp *in*).toLowerCase(re-seq #"\w+\b(?<!\bthe|and|of|to|a|i[tns]?|or)")frequencies(sort-by val >)(take 22))[b](sort(map #(/(- 76(count(key %)))(val %))s))p #(do(print %1)(dotimes[_(* b %2)](print \_))(apply println %&))](p " " m)(doseq[[k v]s](p \| v \| k)))
(let[[[_ m]:as s](->> (slurp *in*) .toLowerCase (re-seq #"\w+\b(?<!\bthe|and|of|to|a|i[tns]?|or)") frequencies (sort-by val >) (take 22)) [b] (sort (map #(/ (- 76 (count (key %)))(val %)) s)) p #(do (print %1) (dotimes[_(* b %2)] (print \_)) (apply println %&))] (p " " m) (doseq[[k v] s] (p \| v \| k)))