while 在 py3 的 nltk 中循环

while cicle in nltk in py3

我有这个任务: “随着语料库的增加,找出 hapax 的分布 1000 代币的增量部分(1000 代币、2000 代币、3000 代币等)” 我想以这种方式使用 while cicle:

 def funzIncrem (testo) :
  while i in testo < len(testo) :
     testo = testo [0 : i]
     fDist = FreqDist (testo)
     hapax = len(fDist.hapaxes())
     distHapax = hapax/i
     i += 1000
     print (distHapax)
  return 

但是当我 运行 它时,口译员给我: "UnboundLocalError: local variable 'i' referenced before assignment"

我尝试了各种方法,但找不到合适的方法。我该怎么做?

试试这个:

def funzIncrem(testo):
    i = 1000
    while i in testo < len(testo):
        testo = testo[0: i]
        fDist = FreqDist(testo)
        hapax = len(fDist.hapaxes())
        distHapax = hapax / i
        i += 1000
        print(distHapax)
    return

我用语句初始化变量i。

i=1000

当我省略语句时,变量i没有值,解释器遇到i不知道怎么办。

"UnboundLocalError: local variable 'i' referenced before assignment"

变量 i 在函数 funzIncrem 的范围内是局部的。这种作用域称为函数作用域。参见 https://en.wikipedia.org/wiki/Scope_(computer_science)#Function_scope

UnboundLocalError 解释在这里:https://docs.python.org/3/library/exceptions.html?highlight=unboundlocalerror#UnboundLocalError.

另外:https://en.wikipedia.org/wiki/Initialization_(programming)

祝你好运!