2017-08-29 15:01:17 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import re
|
|
|
|
|
|
|
|
import spacy
|
|
|
|
import textacy
|
2017-08-30 12:56:59 +02:00
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
2017-08-29 15:01:17 +02:00
|
|
|
|
|
|
|
DATAPATH_thesaurus = "openthesaurus.csv"
|
|
|
|
|
2017-08-30 12:56:59 +02:00
|
|
|
def generateFromXML(path2xml, clean=True, textfield='Beschreibung'):
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
tree = ET.parse(path2xml, ET.XMLParser(encoding="utf-8"))
|
|
|
|
root = tree.getroot()
|
|
|
|
|
|
|
|
for ticket in root:
|
|
|
|
metadata = {}
|
|
|
|
text = "ERROR"
|
|
|
|
for field in ticket:
|
|
|
|
if field.tag == textfield:
|
|
|
|
if clean:
|
|
|
|
text = (field.text)
|
|
|
|
else:
|
|
|
|
text = field.text
|
|
|
|
else:
|
|
|
|
metadata[field.tag] = field.text
|
|
|
|
yield text, metadata
|
|
|
|
|
|
|
|
|
2017-08-29 15:01:17 +02:00
|
|
|
|
|
|
|
def getFirstSynonym(word, thesaurus_gen):
|
|
|
|
|
2017-08-30 12:56:59 +02:00
|
|
|
word = word.lower()
|
2017-08-29 15:01:17 +02:00
|
|
|
|
2017-08-30 12:56:59 +02:00
|
|
|
# durch den thesaurrus iterieren
|
|
|
|
for syn_block in thesaurus_gen: # syn_block ist eine liste mit Synonymen
|
2017-08-29 15:01:17 +02:00
|
|
|
|
2017-08-30 12:56:59 +02:00
|
|
|
for syn in syn_block:
|
2017-08-31 10:38:29 +02:00
|
|
|
syn = syn.lower()
|
2017-08-30 12:56:59 +02:00
|
|
|
if re.match(r'\A[\w-]+\Z', syn): #falls syn einzelwort ist
|
|
|
|
if word == syn:
|
2017-08-31 10:38:29 +02:00
|
|
|
return getHauptform(syn_block,word)
|
|
|
|
else: # falls es ein satz ist
|
|
|
|
if word in syn:
|
|
|
|
return getHauptform(syn_block,word)
|
|
|
|
return word #zur Not, das ursrpüngliche Wort zurückgeben
|
2017-08-30 12:56:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-31 10:38:29 +02:00
|
|
|
def getHauptform(syn_block,word,default_return_first_Syn=False):
|
2017-08-30 12:56:59 +02:00
|
|
|
|
2017-08-31 10:38:29 +02:00
|
|
|
for syn in syn_block:
|
|
|
|
syn = syn.lower()
|
2017-08-30 12:56:59 +02:00
|
|
|
|
2017-08-31 10:38:29 +02:00
|
|
|
if "hauptform" in syn:
|
|
|
|
# nicht ausgeben, falls es in Klammern steht
|
|
|
|
for w in syn.split(" "):
|
|
|
|
if not re.match(r'\([^)]+\)', w):
|
|
|
|
return w
|
2017-08-30 12:56:59 +02:00
|
|
|
|
2017-08-31 10:38:29 +02:00
|
|
|
if default_return_first_Syn:
|
|
|
|
# falls keine hauptform enthalten ist, das erste Synonym zurückgeben, was kein satz ist und nicht in klammern steht
|
|
|
|
for w in syn_block:
|
|
|
|
if not re.match(r'\([^)]+\)', w):
|
|
|
|
return w
|
|
|
|
return word # zur Not, das ursrpüngliche Wort zurückgeben
|
2017-08-30 12:56:59 +02:00
|
|
|
|
2017-08-31 10:38:29 +02:00
|
|
|
THESAURUS_gen = list(textacy.fileio.read_csv(DATAPATH_thesaurus, delimiter=";")) # generator [[a,b,c,..],[a,b,c,..],...]
|
2017-08-30 12:56:59 +02:00
|
|
|
|
2017-08-31 10:38:29 +02:00
|
|
|
strings = ["anmachen","Kernspaltung"]
|
|
|
|
#strings = ["Kernspaltung","Kennwort"]
|
2017-08-30 12:56:59 +02:00
|
|
|
|
2017-08-31 10:38:29 +02:00
|
|
|
for s in strings:
|
|
|
|
print(getFirstSynonym(s,THESAURUS_gen))
|
2017-08-30 12:56:59 +02:00
|
|
|
|
2017-08-31 10:38:29 +02:00
|
|
|
strings = ["Kennwort"]
|
|
|
|
#THESAURUS_gen = textacy.fileio.read_csv(DATAPATH_thesaurus, delimiter=";") # generator [[a,b,c,..],[a,b,c,..],...]
|
2017-08-30 12:56:59 +02:00
|
|
|
|
|
|
|
for s in strings:
|
2017-08-31 10:38:29 +02:00
|
|
|
print(getFirstSynonym(s, THESAURUS_gen))
|
2017-08-30 12:56:59 +02:00
|
|
|
|
2017-08-29 15:01:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|