参数 n 的 N_gram 计数器实现?

发布于 2025-01-17 13:32:27 字数 582 浏览 2 评论 0原文

我正在做一些练习来提高我的技能,并得到了一个来实现二元计数器。我很容易地做到了这一点,但后来开始思考如何实现 n_gram 模型,但未能成功。将函数作为参数获取的特定 n 概括为 n 对我来说很困难,我寻求帮助。

我的函数的参数为​​:

  • data (字符串列表) : ["I", "love", "coding", "in" "python"]
  • 所需的 n_gram

n :对于 n=2 的用例, : 我们将返回一个具有以下计数的计数器对象:

  • ("I", "love") : 1
  • ("love", "coding") : 1
  • ("coding", "in") : 1
  • ("in", "python") : 1

对于 n=3 的用例: 我们将返回一个具有以下计数的计数器对象:

  • ("I", "love", "coding") : 1
  • ("love", "coding", "in") : 1
  • ("coding", "in" ,“蟒蛇”):1

谢谢!

I'm doing some excercises to improve my skills, and got one to implement a bigram counter. I managed to do that pretty easily, but then started to think how would I implement an n_gram model and couldn't succeed in doing so. Something in generalizing a specific n to an n that the function gets as argument is difficult for me and I ask for assistance.

The arguments for my function would be:

  • data (a list of strings) : ["I", "love", "coding", "in" "python"]
  • n : the n_gram required

For a use case in which n=2:
we'll get back a counter object with this count:

  • ("I", "love") : 1
  • ("love", "coding") : 1
  • ("coding", "in") : 1
  • ("in", "python") : 1

For a use case in which n=3:
we'll get back a counter object with this count:

  • ("I", "love", "coding") : 1
  • ("love", "coding", "in") : 1
  • ("coding", "in", "python") : 1

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文