python中相邻句子的二元结构
假设我有三句话:python中相邻句子的二元结构,python,Python,假设我有三句话: hello world hello python 今天是星期二 如果我生成每个字符串的bigram,它将生成如下内容: [('hello', 'world')] [('this', 'is'), ('is', 'python')] [('today', 'is'), ('is', 'tuesday')] [('hello', 'world'), ('world', 'this'), ('this', 'is'),...] 一个句子的双格和两个连续句子的双格有区别吗?例如,h
hello world
hello python
今天是星期二
[('hello', 'world')]
[('this', 'is'), ('is', 'python')]
[('today', 'is'), ('is', 'tuesday')]
[('hello', 'world'), ('world', 'this'), ('this', 'is'),...]
一个句子的双格和两个连续句子的双格有区别吗?例如,helloworld。hello python
是两个连续的句子。这两个连续句子的双格图看起来像我的输出吗
生成它的代码:
from itertools import tee, izip
def bigrams(iterable):
a, b = tee(iterable)
next(b, None)
return izip(a, b)
with open("hello.txt", 'r') as f:
for line in f:
words = line.strip().split()
bi = bigrams(words)
print list(bi)
但是如果我想为相邻的句子生成二元图,它会给出与上面输出相同的结果。如果不是,输出会是什么样子
这取决于你想要什么。如果您将双字组的项目定义为一个完整的句子,它将如下所示:
[('hello world', 'this is python'),('this is python', 'today is tuesday')]
如果您想要一个项目类型为单词的双格图,对于所有的句子,它将如下所示:
[('hello', 'world')]
[('this', 'is'), ('is', 'python')]
[('today', 'is'), ('is', 'tuesday')]
[('hello', 'world'), ('world', 'this'), ('this', 'is'),...]
我认为术语bigrams是重载的,据我所知,这意味着任意两个相邻字符的组合?用字符串“你好”组成的双字组,分别是“他”、“埃尔”、“ll”和“洛”。请在你的上下文中陈述二元图的定义。请重写你的问题,使其一致。除此之外,如果你提供什么是文本结构,什么是预期的句子分隔符,你将有更好的机会得到答案你有生成元组列表的代码吗?或者任何代码?