Đã có nhiều MaltParser và/hoặc NLTK câu hỏi liên quan:Parsing nhiều câu với MaltParser sử dụng NLTK
- Malt Parser throwing class not found exception
- How to use malt parser in python nltk
- MaltParser Not Working in Python NLTK
- NLTK MaltParser won't parse
- Dependency parser using NLTK and MaltParser
- Dependency Parsing using MaltParser and NLTK
- Parsing with MaltParser engmalt
- Parse raw text with MaltParser in Java
Bây giờ, có một phiên bản ổn định hơn MaltParser API trong NLTK: https://github.com/nltk/nltk/pull/944 nhưng có những vấn đề khi nói đến phân tích nhiều câu cùng một lúc.
Phân tích một câu tại một thời điểm có vẻ tốt đẹp:
_path_to_maltparser = '/home/alvas/maltparser-1.8/dist/maltparser-1.8/'
_path_to_model= '/home/alvas/engmalt.linear-1.7.mco'
>>> mp = MaltParser(path_to_maltparser=_path_to_maltparser, model=_path_to_model)
>>> sent = 'I shot an elephant in my pajamas'.split()
>>> sent2 = 'Time flies like banana'.split()
>>> print(mp.parse_one(sent).tree())
(pajamas (shot I) an elephant in my)
Nhưng phân tích một danh sách các câu không trả lại một đối tượng DependencyGraph:
_path_to_maltparser = '/home/alvas/maltparser-1.8/dist/maltparser-1.8/'
_path_to_model= '/home/alvas/engmalt.linear-1.7.mco'
>>> mp = MaltParser(path_to_maltparser=_path_to_maltparser, model=_path_to_model)
>>> sent = 'I shot an elephant in my pajamas'.split()
>>> sent2 = 'Time flies like banana'.split()
>>> print(mp.parse_one(sent).tree())
(pajamas (shot I) an elephant in my)
>>> print(next(mp.parse_sents([sent,sent2])))
<listiterator object at 0x7f0a2e4d3d90>
>>> print(next(next(mp.parse_sents([sent,sent2]))))
[{u'address': 0,
u'ctag': u'TOP',
u'deps': [2],
u'feats': None,
u'lemma': None,
u'rel': u'TOP',
u'tag': u'TOP',
u'word': None},
{u'address': 1,
u'ctag': u'NN',
u'deps': [],
u'feats': u'_',
u'head': 2,
u'lemma': u'_',
u'rel': u'nn',
u'tag': u'NN',
u'word': u'I'},
{u'address': 2,
u'ctag': u'NN',
u'deps': [1, 11],
u'feats': u'_',
u'head': 0,
u'lemma': u'_',
u'rel': u'null',
u'tag': u'NN',
u'word': u'shot'},
{u'address': 3,
u'ctag': u'AT',
u'deps': [],
u'feats': u'_',
u'head': 11,
u'lemma': u'_',
u'rel': u'nn',
u'tag': u'AT',
u'word': u'an'},
{u'address': 4,
u'ctag': u'NN',
u'deps': [],
u'feats': u'_',
u'head': 11,
u'lemma': u'_',
u'rel': u'nn',
u'tag': u'NN',
u'word': u'elephant'},
{u'address': 5,
u'ctag': u'NN',
u'deps': [],
u'feats': u'_',
u'head': 11,
u'lemma': u'_',
u'rel': u'nn',
u'tag': u'NN',
u'word': u'in'},
{u'address': 6,
u'ctag': u'NN',
u'deps': [],
u'feats': u'_',
u'head': 11,
u'lemma': u'_',
u'rel': u'nn',
u'tag': u'NN',
u'word': u'my'},
{u'address': 7,
u'ctag': u'NNS',
u'deps': [],
u'feats': u'_',
u'head': 11,
u'lemma': u'_',
u'rel': u'nn',
u'tag': u'NNS',
u'word': u'pajamas'},
{u'address': 8,
u'ctag': u'NN',
u'deps': [],
u'feats': u'_',
u'head': 11,
u'lemma': u'_',
u'rel': u'nn',
u'tag': u'NN',
u'word': u'Time'},
{u'address': 9,
u'ctag': u'NNS',
u'deps': [],
u'feats': u'_',
u'head': 11,
u'lemma': u'_',
u'rel': u'nn',
u'tag': u'NNS',
u'word': u'flies'},
{u'address': 10,
u'ctag': u'NN',
u'deps': [],
u'feats': u'_',
u'head': 11,
u'lemma': u'_',
u'rel': u'nn',
u'tag': u'NN',
u'word': u'like'},
{u'address': 11,
u'ctag': u'NN',
u'deps': [3, 4, 5, 6, 7, 8, 9, 10],
u'feats': u'_',
u'head': 2,
u'lemma': u'_',
u'rel': u'dep',
u'tag': u'NN',
u'word': u'banana'}]
Tại sao là sử dụng parse_sents()
không trả lại một lần lặp của parse_one
?
tôi có thể tuy nhiên, chỉ nhận được lười biếng và làm:
_path_to_maltparser = '/home/alvas/maltparser-1.8/dist/maltparser-1.8/'
_path_to_model= '/home/alvas/engmalt.linear-1.7.mco'
>>> mp = MaltParser(path_to_maltparser=_path_to_maltparser, model=_path_to_model)
>>> sent1 = 'I shot an elephant in my pajamas'.split()
>>> sent2 = 'Time flies like banana'.split()
>>> sentences = [sent1, sent2]
>>> for sent in sentences:
>>> ... print(mp.parse_one(sent).tree())
Nhưng đây không phải là giải pháp tôi đang tìm kiếm. Câu hỏi của tôi là cách trả lời tại sao không trả lại số parse_sent()
có thể lặp lại là parse_one()
. và làm thế nào nó có thể được sửa trong mã NLTK?
Sau @NikitaAstrakhantsev trả lời, tôi đã thử nó ra một cây phân tích cú pháp bây giờ nhưng nó dường như bị nhầm lẫn và đặt cả hai câu thành một trước khi phân tích nó.
# Initialize a MaltParser object with a pre-trained model.
mp = MaltParser(path_to_maltparser=path_to_maltparser, model=path_to_model)
sent = 'I shot an elephant in my pajamas'.split()
sent2 = 'Time flies like banana'.split()
# Parse a single sentence.
print(mp.parse_one(sent).tree())
print(next(next(mp.parse_sents([sent,sent2]))).tree())
[ra]:
(pajamas (shot I) an elephant in my)
(shot I (banana an elephant in my pajamas Time flies like))
Từ mã nó có vẻ là làm một cái gì đó kỳ lạ: https://github.com/nltk/nltk/blob/develop/nltk/parse/api.py#L45
Tại sao nó rằng phân tích cú pháp lớp trừu tượng trong NLTK được swooshing hai câu thành trước khi phân tích cú pháp? Tôi có đang gọi số parse_sents()
không chính xác không? Nếu vậy, cách chính xác để gọi parse_sents()
là gì?
Cảm ơn bạn !! Bây giờ nó xuất ra cây nhưng đó là một cây sai, xem câu hỏi được cập nhật. – alvas
Tôi đã cập nhật câu trả lời của mình, nhưng không phải với giải pháp - chỉ cần lưu ý –
Tìm thấy lỗi !!! Tôi đang bị mù https://github.com/alvations/nltk/blob/patch-1/nltk/parse/malt.py#L56 'lợi nhuận' \ n \ n'' bị sai thụt lề !!! gosh ... – alvas