import sys
import os
sys.path.insert(0, "/vol/fob-vol7/mi19/harnisph/flair")

import flair
import torch
from flair.models import TARSSequenceTagger2
from flair.data import Sentence
from flair.datasets import CONLL_03

tagger = TARSSequenceTagger2.load(
    "resources/v3/moviecomplex-long/final-model.pt")

flair.set_seed(3)

label_name_map = {
    "LOC": "Location",
    "PER": "Person",
    "ORG": "Organization",
    "MISC": "Miscellaneous"
}
print(label_name_map)
corpus = CONLL_03(
    tag_to_bioes=None,
    tag_to_bio2="ner",
    label_name_map=label_name_map,
    base_path="/vol/fob-vol7/mi19/harnisph/studienprojekt-dokumentation")
corpus = corpus.downsample(0.1)
tag_type = "ner"
tag_dictionary = corpus.make_label_dictionary(tag_type)
tagger.add_and_switch_to_new_task("zeroshot-moviecomplex-long-to-conll3",
import os
#sys.path.insert(0, os.path.join('C:/', 'Users', 'pharn', 'flair'))
#sys.path.insert(0, os.path.join('C:/', 'Users', 'pharn', 'AppData', 'Local', 'Packages', 'PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0', 'LocalCache', 'local-packages', 'Python38', 'site-packages'))
sys.path.insert(0, "/vol/fob-vol7/mi19/harnisph/flair")
sys.path.insert(0, os.path.join("vol", "fob-vol7", "mi19", "harnisph",
                                "flair"))

import flair
from flair.models import TARSSequenceTagger2
from flair.data import Sentence
#from flair.datasets import CONLL_3, MIT_MOVIE_NER_COMPLEX
from flair.datasets import WNUT_2020_NER

flair.set_seed(1)

tagger = TARSSequenceTagger2.load(
    "resources/testfaelle-studproj/conll_3/final-model.pt")

sentences = [
    Sentence(
        "The Parlament of the United Kingdom is discussing a variety of topics."
    ),
    Sentence(
        "A man fell in love with a woman. This takes place in the last century. The film received the Golden Love Film Award."
    ),
    Sentence("The Company of Coca Cola was invented in 1901."),
    Sentence("This is very frustrating! I was smiling since I saw you."),
    Sentence("The Green Party received only a small percentage of the vote."),
    Sentence(
        "Bayern Munich won the german soccer series the sixth time in a row.")
]
import sys
import os
sys.path.insert(0, "/vol/fob-vol7/mi19/harnisph/flair")

import flair
import torch
from flair.models import TARSSequenceTagger2
from flair.data import Sentence
from flair.datasets import MIT_MOVIE_NER_COMPLEX

flair.set_seed(3)

tagger = TARSSequenceTagger2.load("resources/v3/conll_3-cryptic/final-model.pt")

label_name_map = {
"Character_Name":"Character Name"
}
print(label_name_map)
corpus = MIT_MOVIE_NER_COMPLEX(tag_to_bioes=None, tag_to_bio2="ner", label_name_map=label_name_map)
corpus = corpus.downsample(0.1)
tag_type = "ner"
tag_dictionary = corpus.make_label_dictionary(tag_type)
tagger.add_and_switch_to_new_task("zeroshot-conll_3-cryptic-to-moviecomplex", tag_dictionary=tag_dictionary, tag_type=tag_type)
result, eval_loss = tagger.evaluate(corpus.test)
print(result.main_score)
print(result.log_header)
print(result.log_line)
print(result.detailed_results)
print(eval_loss)

# evaluation
示例#4
0
import sys
import os

sys.path.insert(0, "/vol/fob-vol7/mi19/harnisph/flair")

import flair
import torch
from flair.models import TARSSequenceTagger2
from flair.data import Sentence
from flair.datasets import MIT_MOVIE_NER_COMPLEX, CONLL_03, WNUT_17, WNUT_2020_NER, BIOSCOPE
from flair.trainers import ModelTrainer
from torch.optim.lr_scheduler import OneCycleLR

flair.set_seed(3)

tagger = TARSSequenceTagger2.load("resources/v3/sequence-2/final-model.pt")

label_name_map = {
    "person": "Person",
    "location": "Location",
    "creative-work": "Creative Work",
    "product": "Product",
    "corporation": "Corporation",
    "group": "Group"
}
print(label_name_map)
corpus = WNUT_17(tag_to_bioes=None,
                 tag_to_bio2="ner",
                 label_name_map=label_name_map)
corpus = corpus.downsample(0.1)
tag_type = "ner"
import sys
import os
sys.path.insert(0, "/vol/fob-vol7/mi19/harnisph/flair")

import flair
import torch
from flair.models import TARSSequenceTagger2
from flair.data import Sentence
from flair.datasets import MIT_MOVIE_NER_COMPLEX, CONLL_03, WNUT_17, WNUT_2020_NER, BIOSCOPE
from flair.trainers import ModelTrainer
from torch.optim.lr_scheduler import OneCycleLR

flair.set_seed(3)

tagger = TARSSequenceTagger2.load("resources/v3/conll_3-simple/final-model.pt")

label_name_map = {"Character_Name": "Character Name"}
print(label_name_map)
corpus = MIT_MOVIE_NER_COMPLEX(tag_to_bioes=None,
                               tag_to_bio2="ner",
                               label_name_map=label_name_map)
corpus = corpus.downsample(0.1)
tag_type = "ner"
tag_dictionary = corpus.make_label_dictionary(tag_type)

tagger.add_and_switch_to_new_task("sequence-2-train",
                                  tag_dictionary=tag_dictionary,
                                  tag_type=tag_type)

trainer = ModelTrainer(tagger, corpus, optimizer=torch.optim.AdamW)
trainer.train(
示例#6
0
from flair.trainers import ModelTrainer
from flair.embeddings import WordEmbeddings, TransformerWordEmbeddings, TransformerDocumentEmbeddings
from flair.data import Sentence
from flair.data import MultiCorpus
from flair.datasets import CONLL_03

flair.set_seed(1)

label_name_map = {
    "LOC": "Location",
    "PER": "Person",
    "ORG": "Organization",
    "MISC": "Miscellaneous"
}

tagger = TARSSequenceTagger2.load(
    "resources/taggers/tars/test-bio/final-model.pt")

sentence1 = Sentence("I like Berlin and Germany")
sentence2 = Sentence("I like Berlin and Germany")

print()
print()
print("NEW SENTENCES FOR PRELEARNED CLASSES:\n")

tagger.predict(sentence2)
print(sentence2.to_tagged_string)

sent3 = Sentence(
    "Donald Trump is the former president of the United States of America.")
tagger.predict(sent3)
print(sent3.to_tagged_string)