Ich möchte nicht nur das Ergebnis von RegexpParser, sondern auch den Index des Ergebnisses erhalten. Zum Beispiel der Startindex des Wortes und der Endindex des Wortes.Wie bekomme ich den Index des Ergebnisses von nltk.RegexpParser?
import nltk
from nltk import word_tokenize, pos_tag
text = word_tokenize("6 ACCESSKEY attribute can be used to specify many 6.0 shortcut key 6.0")
tag = pos_tag(text)
print tag
# grammar = "NP: {<DT>?<JJ>*<NN|NNS|NNP|NNPS>}"
grammar2 = """Triple: {<CD>*<DT>?<CD>*<JJ>*<CD>*<VBD|VBG>*<CD>*<NN.*>+<CD>*<MD>*<VB.*>+<JJ>?<RB>?<CD>*<DT>?<NN.*>*<IN*|TO*>?<DT>?<CD>*<JJ>*<CD>*<VBD|VBG>*<CD>*<NN.*>+<CD>*}
Triple: {<CD>*<DT>?<CD>*<JJ>*<CD>*<VBD|VBG>*<CD>*<NN.*>+<CD>*<MD>*<VB.*>+<JJ>?<RB>?<CD>*<DT>?<NN.*>*<TO>?<VB><DT>?<CD>*<JJ>*<CD>*<VBD|VBG>*<CD>*<NN.*>+<CD>*}
"""
grammar = """
NP: {<CD>*<DT>?<CD>*<JJ>*<CD>*<VBD|VBG>*<CD>*<NN.*>+<CD>*}
VP: {<VB.*>+<JJ>*<RB>*<JJ>*<VB.*>?<DT>?<NN|NP>?<IN*|TO*>?}
"""
cp = nltk.RegexpParser(grammar)
result = cp.parse(tag)
print(result)
result.draw()
Warum bist du so schlau und geduldig, zu cool! –
Eigentlich kann ich die Offsets bekommen, es ist nicht so schwer. Auf jeden Fall vielen Dank! –