我一直在經歷Coursera的哥倫比亞大學NLP課程,目前我正在嘗試完成第一項任務。該類使用Python,但由於我對Java/Groovy更加熟悉,我決定重寫Groovy中第一個任務中給出的所有helper腳本。你可以在Python中增加單詞嗎?
我注意到原作者做的東西看起來像乘法。那我說一下具體的函數如下所示:
def get_ngrams(sent_iterator, n):
"""
Get a generator that returns n-grams over the entire corpus,
respecting sentence boundaries and inserting boundary tokens.
Sent_iterator is a generator object whose elements are lists
of tokens.
"""
for sent in sent_iterator:
#Add boundary symbols to the sentence
w_boundary = (n-1) * [(None, "*")]
w_boundary.extend(sent)
w_boundary.append((None, "STOP"))
#Then extract n-grams
ngrams = (tuple(w_boundary[i:i+n]) for i in xrange(len(w_boundary)-n+1))
for n_gram in ngrams: #Return one n-gram at a time
yield n_gram
具體來說,我不明白這行
w_boundary = (n-1) * [(None, "*")]
我看到,n爲在函數中定義的,但我真的不知道它是什麼。搜索「字符串乘python」並沒有給我任何幫助。有人可以解釋這個python特性,或者讓我更好地瞭解我至少應該嘗試搜索什麼?
這是列表乘法。爲什麼不在口譯員那裏嘗試呢? 'n'是一個整數。 – jonrsharpe
@jonrsharpe我甚至沒有想到這一點。哈哈。你不會在命令行嘗試Java。 –