Szabó Gergő commited on
Commit
3571bad
1 Parent(s): b27f1b3

relation extraction added

Browse files
Files changed (2) hide show
  1. app.py +3 -2
  2. examples/relation.py +54 -0
app.py CHANGED
@@ -1,14 +1,15 @@
1
  import gradio as gr
2
  from examples.keyphrases import demo as keyphrases_demo
3
  from examples.anon import demo as anon_demo
 
4
 
5
  demo = gr.Blocks()
6
 
7
  with demo:
8
  gr.Markdown("# HuSpaCy Examples")
9
  gr.TabbedInterface(
10
- interface_list=[keyphrases_demo, anon_demo],
11
- tab_names=["Keyphrase extraction", "Text anonymization"],
12
  )
13
 
14
  if __name__ == '__main__':
 
1
  import gradio as gr
2
  from examples.keyphrases import demo as keyphrases_demo
3
  from examples.anon import demo as anon_demo
4
+ from examples.relation import demo as relation_demo
5
 
6
  demo = gr.Blocks()
7
 
8
  with demo:
9
  gr.Markdown("# HuSpaCy Examples")
10
  gr.TabbedInterface(
11
+ interface_list=[keyphrases_demo, anon_demo, relation_demo],
12
+ tab_names=["Keyphrase extraction", "Text anonymization", "Relation Extraction"],
13
  )
14
 
15
  if __name__ == '__main__':
examples/relation.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import spacy
4
+ import sys
5
+ import pandas as pd
6
+
7
+ sys.path.append('/home/gszabo/PycharmProjects/textacy/textacy/src/textacy/extract')
8
+ import triples
9
+
10
+ nlp = spacy.load("hu_core_news_lg")
11
+
12
+ def process(text: str) -> pd.DataFrame:
13
+ doc = nlp(text)
14
+ tuples_to_list = list()
15
+
16
+ tuples = triples.subject_verb_object_triples(doc)
17
+ if tuples:
18
+ tuples_to_list = list(tuples)
19
+
20
+ subject = ""
21
+ verb = ""
22
+ object = ""
23
+
24
+ if len(tuples_to_list) == 0:
25
+ return pd.DataFrame([["-", "-", "-"]], columns=['Subject', 'Verb', 'Object'])
26
+
27
+ for sub_multiple in tuples_to_list[0][0]:
28
+ subject += str(sub_multiple) + ", "
29
+ subject = subject[:-2]
30
+ for verb_multiple in tuples_to_list[0][1]:
31
+ verb += str(verb_multiple) + ", "
32
+ verb = verb[:-2]
33
+ for obj_multiple in tuples_to_list[0][2]:
34
+ object += str(obj_multiple) + ", "
35
+ object = object[:-2]
36
+
37
+ relation_list = [[subject,
38
+ verb,
39
+ object]]
40
+
41
+ return pd.DataFrame(relation_list, columns=['Subject', 'Verb', 'Object'])
42
+
43
+ EXAMPLES = ["Vespucci 1450-es években született Firenzében, és 1497 és 1504 között legalább két felfedező úton vett részt.",
44
+ "Einstein megmutatta, ha feltételezi, hogy a fény valóban csak diszkrét csomagokban terjed, akkor meg tudja magyarázni a fényelektromos jelenség furcsa tulajdonságait.",
45
+ "Einstein megállapította, hogy hasonló energiaeloszlás lehet érvényes az atomokra is.",
46
+ "Hawking úgy nyilatkozott, hogy a felfedezései az élete legizgalmasabb eseményei voltak."]
47
+
48
+ demo = gr.Interface(
49
+ fn=process,
50
+ inputs=gr.Textbox(value=EXAMPLES[0], lines=10, label="Input text", show_label=True),
51
+ outputs=gr.DataFrame(label="Keywords", show_label=False, max_cols=3, max_rows=1),
52
+ examples=EXAMPLES,
53
+ # cache_examples=True,
54
+ )