Пример #1
0
 def get_semantic_relations(self, entity_pairs, is_id=False):
     if is_id:
         rels = tagme.relatedness_wid(entity_pairs)
     else:
         rels = tagme.relatedness_title(entity_pairs)
     return [{'entity1': rel.title1, 'entity2': rel.title2, 'score': rel.rel} \
              for rel in rels.relatedness]
Пример #2
0
def similarity(A, B, flag=0):
    if flag == 0:
        rels = tagme.relatedness_title((A, B))
        return rels.relatedness[0].rel
    else:
        rels = tagme.relatedness_wid((A, B))
        return rels.relatedness[0].rel
Пример #3
0
def main():
    # Annotate a text.
    print("Annotating text: ", SAMPLE_TEXT)
    #resp = tagme.annotate(SAMPLE_TEXT)
    resp = tagme.annotate(SAMPLE_TEXT, include_categories=True)
    print(resp)
    for ann in resp.annotations:
        print(ann)

    # Find mentions in a text.
    print("Finding mentions in text: ", SAMPLE_TEXT)
    resp = tagme.mentions(SAMPLE_TEXT)
    print(resp)
    for mention in resp.mentions:
        print(mention)

    # Find relatedness between one pair of entities, by title.
    resp = tagme.relatedness_title(["Barack_Obama", "Italy"])
    print(resp)
    for rel in resp.relatedness:
        print(rel)

    # Find relatedness between pairs of entities, by title.
    resp = tagme.relatedness_title([("Barack_Obama", "Italy"),
                                    ("Italy", "Germany"),
                                    ("Italy", "BAD ENTITY NAME")])
    print(resp)
    for rel in resp.relatedness:
        print(rel)

    # Access the relatedness response as a dictionary.
    resp_dict = dict(resp)
    print("Relatedness between Italy and Germany: ",
          resp_dict[("Italy", "Germany")])

    # Find relatedness between one pair of entities, by wikipedia id
    resp = tagme.relatedness_wid((31717, 534366))
    print(resp)
    for rel in resp.relatedness:
        print(rel)

    # Find relatedness between pairs of entities, by wikipedia id
    resp = tagme.relatedness_wid([(534366, 534366 + a) for a in range(1010)])
    print(resp)
    for rel in resp.relatedness:
        print(rel)
Пример #4
0
 def _prefetch_relatedness(self, entity_group_1, entity_group_2):
     pairs = ((e1, e2) for e1 in entity_group_1 for e2 in entity_group_2)
     pairs_to_retrieve = [
         p for p in pairs if _str_titles(*p) not in self.rel_dict
     ]
     if pairs_to_retrieve:
         for titles, rel in tagme.relatedness_title(pairs_to_retrieve):
             self.rel_dict[_str_titles(*titles)] = rel
     for p in pairs:
         assert _str_titles(*p) in self.rel_dict
     self.rel_dict.commit()
Пример #5
0
def entity_list(request):
    import tagme
    # Set the authorization token for subsequent calls.
    tagme.GCUBE_TOKEN = "a5a377c1-1bd0-47b9-907a-75b1cdacb1d9-843339462"

    db_post = PostTitle.objects.filter(user_id=request.user.id,
                                       search_date_interval=val2(),
                                       search_word=val(),
                                       date=date.today().strftime("%b-%d-%Y"))
    file1 = db_post.values_list('title', flat=True)

    # file1 = open('file.txt', 'r', encoding='utf-8')
    entity_title_list = []

    # Using for loop
    for line in file1:
        lunch_annotations = tagme.annotate(line)
        # Print annotations with a score higher than 0.1
        for ann in lunch_annotations.get_annotations(min_rho=0.35):
            entity_title_list.append(ann.entity_title)

    # Closing files
    # file1.close()

    entity_title_list = list(dict.fromkeys(entity_title_list))

    relation_degree_list = []
    relation_pair_list = []
    relation_list = [
        "ALL RELATIONS BETWEEN ENTITIES ARE LISTED BELOW", "\n", "\n"
    ]

    for i in range(0, len(entity_title_list)):
        for j in range(i + 1, len(entity_title_list)):
            rels = tagme.relatedness_title(
                (entity_title_list[i], entity_title_list[j]))
            relation_degree = rels.relatedness[0].rel
            # print(f"{entity_title_list[i]} ve {entity_title_list[j]} {relation_degree}")
            # print("\n")
            if relation_degree != 0.0:
                relation_pair_list.append(
                    (entity_title_list[i], entity_title_list[j]))
                relation_degree_list.append(relation_degree)
                pairs = entity_title_list[i] + " " + entity_title_list[j]
                relation_list.append(pairs)
                relation_list.append("\t")
                relation_list.append(relation_degree)
                relation_list.append("\n")
        print(entity_title_list[i])
        print("Kalan entity sayısı:  ", len(entity_title_list) - (i + 1))
        print("done")

    return HttpResponse(relation_list, content_type="text/plain")
Пример #6
0
def tagme_matching(trend_one, trend_two):
    matches = []
    trend_one_processed = text_processing(trend_one, keep_spaces=True)
    trend_two_processed = text_processing(trend_two, keep_spaces=True)

    for keyword_one in trend_one_processed:
        for keyword_two in trend_two_processed:
            relations = tagme.relatedness_title(
                (keyword_one['processed'], keyword_two['processed']))
            print('Comparing ' + keyword_one['processed'] + ' and ' +
                  keyword_two['processed'])
            if relations.relatedness[0].rel and int(
                    relations.relatedness[0].rel) > 0:
                matches.append(keyword_one['original'])

    if len(matches) == 0: return 'No matches'
    return matches