مع CoreNLPParser.tag()
، فإن "إعادة التفعيل" بواسطة Stanford CoreNLP غير متوقع:
>>> from nltk.parse.corenlp import CoreNLPParser
>>> ner_tagger = CoreNLPParser(url='http://localhost:9000', tagtype='ner')
>>> sent = ['my', 'phone', 'number', 'is', '1111', '1111', '1111']
>>> ner_tagger.tag(sent)
[('my', 'O'),
('phone', 'O'),
('number', 'O'),
('is', 'O'),
('1111\xa01111\xa01111', 'NUMBER')]
يجب أن يكون السلوك المتوقع:
>>> from nltk.parse.corenlp import CoreNLPParser
>>> ner_tagger = CoreNLPParser(url='http://localhost:9000', tagtype='ner')
>>> sent = ['my', 'phone', 'number', 'is', '1111', '1111', '1111']
>>> ner_tagger.tag(sent)
[('my', 'O'), ('phone', 'O'), ('number', 'O'), ('is', 'O'), ('1111', 'DATE'), ('1111', 'DATE'), ('1111', 'DATE')]
الحل المقترح هو السماح بالحمل الزائد للوسائط properties
لـ .tag()
و .tag_sents()
، على سبيل المثال على properties = {'tokenize.whitespace':'true'}
لأننا نقوم بربط الرموز بالمسافات في tag_sents()
.
def tag_sents(self, sentences, properties=None):
"""
Tag multiple sentences.
Takes multiple sentences as a list where each sentence is a list of
tokens.
:param sentences: Input sentences to tag
:type sentences: list(list(str))
:rtype: list(list(tuple(str, str))
"""
# Converting list(list(str)) -> list(str)
sentences = (' '.join(words) for words in sentences)
if properties == None:
properties = {'tokenize.whitespace':'true'}
return [sentences[0] for sentences in self.raw_tag_sents(sentences, properties)]
def tag(self, sentence, properties=None):
"""
Tag a list of tokens.
:rtype: list(tuple(str, str))
>>> parser = CoreNLPParser(url='http://localhost:9000', tagtype='ner')
>>> tokens = 'Rami Eid is studying at Stony Brook University in NY'.split()
>>> parser.tag(tokens)
[('Rami', 'PERSON'), ('Eid', 'PERSON'), ('is', 'O'), ('studying', 'O'), ('at', 'O'), ('Stony', 'ORGANIZATION'),
('Brook', 'ORGANIZATION'), ('University', 'ORGANIZATION'), ('in', 'O'), ('NY', 'O')]
>>> parser = CoreNLPParser(url='http://localhost:9000', tagtype='pos')
>>> tokens = "What is the airspeed of an unladen swallow ?".split()
>>> parser.tag(tokens)
[('What', 'WP'), ('is', 'VBZ'), ('the', 'DT'),
('airspeed', 'NN'), ('of', 'IN'), ('an', 'DT'),
('unladen', 'JJ'), ('swallow', 'VB'), ('?', '.')]
"""
return self.tag_sents([sentence], properties)[0]
def raw_tag_sents(self, sentences, properties=None):
"""
Tag multiple sentences.
Takes multiple sentences as a list where each sentence is a string.
:param sentences: Input sentences to tag
:type sentences: list(str)
:rtype: list(list(list(tuple(str, str)))
"""
default_properties = {'ssplit.isOneSentence': 'true',
'annotators': 'tokenize,ssplit,' }
default_properties.update(properties or {})
# Supports only 'pos' or 'ner' tags.
assert self.tagtype in ['pos', 'ner']
default_properties['annotators'] += self.tagtype
for sentence in sentences:
tagged_data = self.api_call(sentence, properties=default_properties)
yield [[(token['word'], token[self.tagtype]) for token in tagged_sentence['tokens']]
for tagged_sentence in tagged_data['sentences']]
يجب أن يفرض ذلك قائمة بإدخال رموز السلسلة من قبل المستخدمين.
إذا سمحنا لـ .tag()
بتحميل الخصائص بشكل زائد قبل raw_tag_sents
، فسيتيح ذلك أيضًا للمستخدمين التعامل بسهولة مع حالات مثل # 1876
تبدو جيدا.
فقط بعض التعليقات الطفيفة. يجب أن يكون if properties is None
وليس if properties == None
. يجب أن يكون assert self.tagtype in ['pos', 'ner']
assert self.tagtype in ['pos', 'ner'], "CoreNLP tagger supports only 'pos' or 'ner' tags."
.
لا أحب فكرة الانضمام إلى السلاسل وتقسيمها ، فربما تكون هناك طريقة لتمرير قائمة من الكلمات إلى CoreNLP كجملة بدلاً من سلسلة بسيطة.
مرحبًا ، أود أن أتناول هذا الأمر باعتباره مشكلتي الأولى.
إنه لأمر رائع أنك مهتم بالقضية. إذا كان لديك أي أسئلة ، اسألهم هنا.
التعليق الأكثر فائدة
تبدو جيدا.
فقط بعض التعليقات الطفيفة. يجب أن يكون
if properties is None
وليسif properties == None
. يجب أن يكونassert self.tagtype in ['pos', 'ner']
assert self.tagtype in ['pos', 'ner'], "CoreNLP tagger supports only 'pos' or 'ner' tags."
.لا أحب فكرة الانضمام إلى السلاسل وتقسيمها ، فربما تكون هناك طريقة لتمرير قائمة من الكلمات إلى CoreNLP كجملة بدلاً من سلسلة بسيطة.