如何根据每个句子而不是通过文件来匹配命名实体

时间:2019-04-10 10:33:06

标签: python python-3.x

我有一个文本文件,并且实现了Polyglot NER以从该文本文件中提取实体。然后,我必须对每个句子进行分段,并匹配每个句子上提取的实体。匹配时,它应该给我输出。

from polyglot.text import Text
file = open('input_raw.txt', 'r')
input_file = file.read()
file = Text(input_file, hint_language_code='fa')

def return_match(entities_list, sentence):       ## Check if Chunks
    for term in entities_list:                  ## are in any of the entities
        ## Check each list in each Chunk object 
        ## and see if there's any matches.
        for entity in sentence.entities:
            if entity == term:
                return entity
    return None

def return_list_of_entities(file):
    list_entity = []
    for sentence in file.sentences:
        for entity in sentence.entities:
            list_entity.append(entity)
    return list_entity

list_entity = return_list_of_entities(file)
#sentence_number = 4 # Which sentence to check
for sentence in range(len(file.sentences)):
    sentencess = file.sentences[sentence]


match = return_match(list_entity, sentencess)

if match is not None:
    print("Entity Term " + str(match) +  
          " is in the sentence. '" + str(sentencess)+ "'")
else:
    print("Sentence '" + str(sentencess) + 
          "' doesn't contain any of the terms" + str(list_entity))


input_file:

Bill Gates is the founder of Microsoft.
Trump is the president of the USA.
Bill Gates was a student in Harvard.

当我们实现NER时,实体看起来像:

list_etity:

Bill Gates, Microsoft, Trump, USA, Bill Gate, Harvard

当我们将实体与第一句话匹配时,它给出:

当前输出:

(Bill Gates, Bill Gates, Microsoft)

预期输出:

(Bill Gates, Microsoft) # this is from the first sentence and should contine
(Trump, USA) 
(Bill Gates, Harvard)

1 个答案:

答案 0 :(得分:0)

    //sampler for the texture
    val sampler = Texture.Sampler.builder()
            .setWrapMode(Texture.Sampler.WrapMode.REPEAT)
            .build()

    Texture.builder()
            .setSampler(sampler)
            .setSource(this, R.drawable.your_drawable_texture)
            .build()
            .thenCompose { texture ->
                MaterialFactory.makeOpaqueWithTexture(this, texture)
            }
            .thenAccept { material ->
                ShapeFactory.makeCube(vector, vector, material)
            }