2

In NER(Named Entity Recognition) example in python-crf package website we see this function as feature generator:

def word2features(sent, i):
word = sent[i][0]
postag = sent[i][1]
features = [
    'bias',
    'word.lower=' + word.lower(),
    'word[-3:]=' + word[-3:],
    'word[-2:]=' + word[-2:],
    'word.isupper=%s' % word.isupper(),
    'word.istitle=%s' % word.istitle(),
    'word.isdigit=%s' % word.isdigit(),
    'postag=' + postag,
    'postag[:2]=' + postag[:2],
]
if i > 0:
    word1 = sent[i-1][0]
    postag1 = sent[i-1][1]
    features.extend([
        '-1:word.lower=' + word1.lower(),
        '-1:word.istitle=%s' % word1.istitle(),
        '-1:word.isupper=%s' % word1.isupper(),
        '-1:postag=' + postag1,
        '-1:postag[:2]=' + postag1[:2],
    ])
else:
    features.append('BOS')

if i < len(sent)-1:
    word1 = sent[i+1][0]
    postag1 = sent[i+1][1]
    features.extend([
        '+1:word.lower=' + word1.lower(),
        '+1:word.istitle=%s' % word1.istitle(),
        '+1:word.isupper=%s' % word1.isupper(),
        '+1:postag=' + postag1,
        '+1:postag[:2]=' + postag1[:2],
    ])
else:
    features.append('EOS')

return features

You can see the completed tutorial there: python-crfsuite NER example

As you see after appending meaningful features - like word.lower and ...- two features has appended.

features.append('EOS')

and

features.append('BOS')

My question is "What's meaning of BOS and EOS and what is the role of them?"

4

1 回答 1

2

这些代表“句子的开头”和“句子的结尾”。对于没有上一个/下一个单词的单词,它们用于代替“上一个单词”和“下一个单词”功能。

于 2015-09-08T01:24:35.710 回答