参数 n 的 N_gram 计数器实现?
我正在做一些练习来提高我的技能,并得到了一个来实现二元计数器。我很容易地做到了这一点,但后来开始思考如何实现 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论