计算文件中的字频(bash4,awk)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算文件中的字频(bash4,awk)相关的知识,希望对你有一定的参考价值。

  1. # "word" is defined as "space delimited token" - i.e. "one" and "one." are dfferent
  2. # words. The awk-expression is used as "Tokenizer", its results are used to build an
  3. # associative array. Blank lines are assigned to the array-entry a[Blank Line] (which
  4. # cannot result from the tokenizing, since it has a space)
  5. unset a; declare -A a;
  6. while read -r; do
  7. ! [[ $REPLY ]] && REPLY="Blank Line"
  8. ((a[$REPLY]++))
  9. done < <(awk -v OFS=\n '{$1=$1} 1' ./file)
  10. # print the results, sorted by frequency
  11. for word in "${!a[@]}"; do
  12. printf '%d %s ' "${a[$word]}" "$word"
  13. done | sort -n
  14.  
  15. # NOTE: The "$REPLY" (i.e. the backslash) is needed because for bash4
  16. # otherwise a "[" or "]" would break the expansion/evaluation.
  17. # NOTE ALSO: This is mainly sort of a "proof of concept". It is very slow and would better
  18. # be implemented in e.g. awk completely!

以上是关于计算文件中的字频(bash4,awk)的主要内容,如果未能解决你的问题,请参考以下文章

基于汉字字频特征实现99.99%准确率的新闻文本分类器

使用字典在Python中计算字频率效率

使用来自多个文件的awk计算文件中的平均值

grep/sed/awk - 用新的计算值“$X/10”替换文件中的所有“$X”

awk:计算不同文件中数据的平均值

递归查找重复文件(bash4,关联数组)