# Step 2: Build the dictionary and replace rare words with UNK token.
vocabulary_size = 50000
def build_dataset(words, n_words):
"""Process raw inputs into a dataset."""
count = [['UNK', -1]]
count.extend(collections.Counter(words).most_common(n_words - 1))
dictionary = dict()
for word, _ in count:
dictionary[word] = len(dictionary)
data = list()
unk_count = 0
for word in words:
if word in dictionary:
index = dictionary[word]
else:
index = 0 # dictionary['UNK']
unk_count += 1
data.append(index)
count[0][1] = unk_count
reversed_dictionary = dict(zip(dictionary.values(), dictionary.keys()))
return data, count, dictionary, reversed_dictionary
data, count, dictionary, reverse_dictionary = build_dataset(vocabulary,
vocabulary_size)
我正在学习使用Tensorflow的单词向量表示的基本示例。
这个步骤2标题为"构建字典并用UNK令牌替换罕见的单词"但是,没有预先定义过什么" UNK"是指。
指定问题:
0)UNK在NLP中通常会提到什么?
1)count = [[' UNK', - 1]]是什么意思?我知道括号[]引用python中的列表,但是,为什么我们将它与-1并置?
答案 0 :(得分:2)
正如评论中已经提到的那样,在标记化和NLP中,当您看到UNK
标记时,表示未知单词的机会很高。
例如,如果您要预测句子中缺少的单词。您将如何向其中提供数据?您肯定需要一个令牌来显示丢失的单词在哪里。因此,如果“房屋”是我们遗漏的词,则在标记后会像:
'my house is big'
->['my', 'UNK', 'is', 'big']
PS:count = [['UNK', -1]]
用于初始化count
,就像伊万·阿克萨门托夫(Ivan Aksamentov)所说的[['word', number_of_occurences]]
。