90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
import re
|
|
|
|
import spacy
|
|
import textacy
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
DATAPATH_thesaurus = "openthesaurus.csv"
|
|
|
|
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
|
|
|
|
|
|
|
|
def getFirstSynonym(word, thesaurus_gen):
|
|
|
|
word = word.lower()
|
|
|
|
# durch den thesaurrus iterieren
|
|
for syn_block in thesaurus_gen: # syn_block ist eine liste mit Synonymen
|
|
|
|
for syn in syn_block:
|
|
syn = syn.lower()
|
|
if re.match(r'\A[\w-]+\Z', syn): #falls syn einzelwort ist
|
|
if word == syn:
|
|
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
|
|
|
|
|
|
|
|
|
|
def getHauptform(syn_block,word,default_return_first_Syn=False):
|
|
|
|
for syn in syn_block:
|
|
syn = syn.lower()
|
|
|
|
if "hauptform" in syn:
|
|
# nicht ausgeben, falls es in Klammern steht
|
|
for w in syn.split(" "):
|
|
if not re.match(r'\([^)]+\)', w):
|
|
return w
|
|
|
|
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
|
|
|
|
THESAURUS_gen = list(textacy.fileio.read_csv(DATAPATH_thesaurus, delimiter=";")) # generator [[a,b,c,..],[a,b,c,..],...]
|
|
|
|
strings = ["anmachen","Kernspaltung"]
|
|
#strings = ["Kernspaltung","Kennwort"]
|
|
|
|
for s in strings:
|
|
print(getFirstSynonym(s,THESAURUS_gen))
|
|
|
|
strings = ["Kennwort"]
|
|
#THESAURUS_gen = textacy.fileio.read_csv(DATAPATH_thesaurus, delimiter=";") # generator [[a,b,c,..],[a,b,c,..],...]
|
|
|
|
for s in strings:
|
|
print(getFirstSynonym(s, THESAURUS_gen))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|