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