gpt4 book ai didi

bash - 可以在 bash 中生成 ngrams 吗?

转载 作者:行者123 更新时间:2023-11-29 09:38:30 25 4
gpt4 key购买 nike

我已经在 Python、Perl 等语言中找到了 ngrams 的各种实现,但我真的很喜欢 bash 脚本中的东西。我遇到了“Missing textutils”版本,但它只列出了 ngram,它没有按频率计算它们,这对于使用 ngram 非常重要——或者至少对我的使用来说是这样。我只想要一个基本的结果列表及其频率,就像这样......

17 blue car
14 red car
5 and the
2 brown monkey
1 orange car

有人可以发布类似的东西吗?谢谢!

最佳答案

是的,ngrams 可以在 bash 中实现。

# Usage: ngrams N < FILE
ngrams () {
local N=$1
local line
set --
while read line; do
set -- $* $line
while [[ -n ${*:$N} ]]; do
echo ${*:1:$N}
shift
done
done |
sort | uniq -c
}

$ ngrams 2
Here is some text, and here is
some more text, and here is yet
some more text
1 Here is
2 and here
2 here is
2 is some
1 is yet
1 more text
1 more text,
2 some more
1 some text,
2 text, and
1 yet some

注意:上面是一个函数,而不是一个脚本(也许这个question 有帮助,或者可能有另一个更好的)。这是脚本版本:

#!/bin/bash
# Usage: ngrams N < FILE
N=$1
set --
while read line; do
set -- $* $line
while [[ -n ${*:$N} ]]; do
echo ${*:1:$N}
shift
done
done |
sort | uniq -c

关于bash - 可以在 bash 中生成 ngrams 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14410478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com