openthesaurus debugging
This commit is contained in:
parent
6292ee6cc7
commit
bb9edcff25
|
@ -7,7 +7,7 @@ import spacy
|
|||
import textacy
|
||||
import sys
|
||||
|
||||
|
||||
import xml.etree.ElementTree as ET
|
||||
"""
|
||||
import keras
|
||||
import numpy as np
|
||||
|
@ -37,7 +37,7 @@ def getFirstSynonym(word, thesaurus_gen):
|
|||
|
||||
# durch den synonymblock iterieren
|
||||
for syn in syn_block:
|
||||
syn = syn.lower().split(" ") # aus synonym mach liste (um evtl. sätze zu identifieziren)
|
||||
syn = syn.lower().split(" ") if not re.match(r'\A[\w-]+\Z', syn) else syn # aus synonym mach liste (um evtl. sätze zu identifieziren)
|
||||
|
||||
# falls das wort in dem synonym enthalten ist (also == einem Wort in der liste ist)
|
||||
if word in syn:
|
||||
|
@ -46,19 +46,18 @@ def getFirstSynonym(word, thesaurus_gen):
|
|||
if "auptform" in syn:
|
||||
# nicht ausgeben, falls es in Klammern steht
|
||||
for w in syn:
|
||||
if not re.match(r'\([^)]+\)', w):
|
||||
if not re.match(r'\([^)]+\)', w) and w is not None:
|
||||
return w
|
||||
|
||||
# falls keine hauptform enthalten ist, das erste Synonym zurückgeben, was kein satz ist und nicht in klammern steht
|
||||
if len(syn) == 1:
|
||||
w = syn[0]
|
||||
if not re.match(r'\([^)]+\)', w):
|
||||
if not re.match(r'\([^)]+\)', w) and w is not None:
|
||||
return w
|
||||
|
||||
return word # zur Not die eingabe ausgeben
|
||||
|
||||
|
||||
|
||||
def cleanText(string,custom_stopwords=None, custom_symbols=None, custom_words=None, customPreprocessing=None, lemmatize=False):
|
||||
import re
|
||||
|
||||
|
@ -157,24 +156,25 @@ def cleanText(string,custom_stopwords=None, custom_symbols=None, custom_words=No
|
|||
tokens.remove("\n")
|
||||
while "\n\n" in tokens:
|
||||
tokens.remove("\n\n")
|
||||
|
||||
#TODO hier thsaurus einbinden?
|
||||
"""
|
||||
tokenz = []
|
||||
for tok in tokens:
|
||||
tokenz.append(str(getFirstSynonym(tok,THESAURUS_gen)))
|
||||
tokens = tokenz
|
||||
"""
|
||||
tokens = [str(getFirstSynonym(tok,THESAURUS_gen)) for tok in tokens]
|
||||
|
||||
return " ".join(tokens)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def generateTextfromXML(path2xml, clean=True, field='Beschreibung'):
|
||||
def generateTextfromXML(path2xml, clean=True, textfield='Beschreibung'):
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
tree = ET.parse(path2xml, ET.XMLParser(encoding="utf-8"))
|
||||
|
||||
root = tree.getroot()
|
||||
|
||||
for subject in root.iter(field):
|
||||
for subject in root.iter(textfield):
|
||||
if clean:
|
||||
yield cleanText(subject.text)
|
||||
else:
|
||||
|
@ -189,13 +189,31 @@ def generateMetadatafromXML(path2xml, keys=["Loesung","Kategorie","Zusammenfassu
|
|||
|
||||
metadata = dict.fromkeys(keys)
|
||||
|
||||
|
||||
for ticket in root.findall('ticket'):
|
||||
for key in metadata:
|
||||
metadata[key] = ticket.find(key).text #TODO hier thsaurus einbinden?
|
||||
metadata[key] = ticket.find(key).text
|
||||
|
||||
yield metadata
|
||||
|
||||
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 = cleanText(field.text)
|
||||
else:
|
||||
text = field.text
|
||||
else:
|
||||
metadata[field.tag] = field.text
|
||||
yield text, metadata
|
||||
|
||||
|
||||
####################'####################'####################'####################'####################'##############
|
||||
|
@ -217,13 +235,17 @@ THESAURUS_gen = textacy.fileio.read_csv(DATAPATH_thesaurus, delimiter=";") # ge
|
|||
textacyCorpus = textacy.Corpus(PARSER)
|
||||
|
||||
print("add texts to textacy-corpus...")
|
||||
textacyCorpus.add_texts(texts=generateTextfromXML(DATAPATH), metadatas=generateMetadatafromXML(DATAPATH))
|
||||
#textacyCorpus.add_texts(texts=generateTextfromXML(DATAPATH), metadatas=generateMetadatafromXML(DATAPATH))
|
||||
for txt, dic in generateFromXML(DATAPATH):
|
||||
textacyCorpus.add_text(txt,dic)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
print(textacyCorpus[2].text)
|
||||
#printRandomDoc(textacyCorpus)
|
||||
|
||||
print(textacyCorpus[len(textacyCorpus)-1].text)
|
||||
#print(textacyCorpus[len(textacyCorpus)-1].text)
|
||||
|
||||
|
||||
|
||||
|
|
104
test.py
104
test.py
|
@ -3,23 +3,29 @@ 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
|
||||
|
||||
# read .csv
|
||||
thesaurus = textacy.fileio.read_csv(DATAPATH_thesaurus, delimiter=";") # generator [[a,b,c,..],[a,b,c,..],...]
|
||||
tree = ET.parse(path2xml, ET.XMLParser(encoding="utf-8"))
|
||||
root = tree.getroot()
|
||||
|
||||
wort = "(anmachen)"
|
||||
if not re.match(r'\([^)]+\)', wort):
|
||||
print(wort)
|
||||
|
||||
#if "Pass" in wort: # "Pass" muss irgendwo drin sein
|
||||
# print(wort.lower())
|
||||
|
||||
|
||||
#if "Passwort" in wort.split(" "): # Pass muss gleich einem Wort sein
|
||||
# print(wort.lower())
|
||||
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):
|
||||
|
@ -33,29 +39,87 @@ def getFirstSynonym(word, thesaurus_gen):
|
|||
|
||||
# durch den synonymblock iterieren
|
||||
for syn in syn_block:
|
||||
syn = syn.lower().split(" ") # aus synonym mach liste (um evtl. sätze zu identifieziren)
|
||||
syn = syn.lower().split(" ") if not re.match(r'\A[\w-]+\Z', syn) else syn # aus synonym mach liste (um evtl. sätze zu identifieziren)
|
||||
|
||||
# falls das wort in dem synonym enthalten ist (also == einem Wort in der liste ist)
|
||||
if word in syn:
|
||||
|
||||
# Hauptform suchen
|
||||
if "auptform" in syn:
|
||||
if "Hauptform" in syn:
|
||||
# nicht ausgeben, falls es in Klammern steht
|
||||
for w in syn:
|
||||
if not re.match(r'\([^)]+\)',w):
|
||||
if not re.match(r'\([^)]+\)', w) and w is not None:
|
||||
return w
|
||||
|
||||
|
||||
# falls keine hauptform enthalten ist, das erste Synonym zurückgeben, was kein satz ist und nicht in klammern steht
|
||||
if len(syn) == 1:
|
||||
w = syn[0]
|
||||
if not re.match(r'\([^)]+\)', w):
|
||||
if not re.match(r'\([^)]+\)', w) and w is not None:
|
||||
return w
|
||||
|
||||
|
||||
|
||||
return word # zur Not die eingabe ausgeben
|
||||
|
||||
|
||||
|
||||
print(getFirstSynonym(wort,thesaurus))
|
||||
def getFirstSynonym(word, thesaurus_gen):
|
||||
|
||||
word = word.lower()
|
||||
# TODO word cleaning https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python
|
||||
|
||||
|
||||
# durch den thesaurrus iterieren
|
||||
for syn_block in thesaurus_gen: # syn_block ist eine liste mit Synonymen
|
||||
|
||||
for syn in syn_block:
|
||||
|
||||
if re.match(r'\A[\w-]+\Z', syn): #falls syn einzelwort ist
|
||||
if word == syn:
|
||||
getHauptform(syn_block)
|
||||
|
||||
|
||||
|
||||
|
||||
def getHauptform(syn_block):
|
||||
for s in syn_block:
|
||||
if "Hauptform" in s:
|
||||
# nicht ausgeben, falls es in Klammern steht
|
||||
for w in s:
|
||||
if not re.match(r'\([^)]+\)', w) and w is not None:
|
||||
return w
|
||||
|
||||
# falls keine hauptform enthalten ist, das erste Synonym zurückgeben, was kein satz ist und nicht in klammern steht
|
||||
if len(s) == 1:
|
||||
w = s[0]
|
||||
if not re.match(r'\([^)]+\)', w) and w is not None:
|
||||
return w
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
strings = ["passwort",""]
|
||||
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))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue