def __init__(self, searchTerm, language): VocabularyBase.__init__(self, searchTerm, language) self.relatedSet = OrderedSet() self.broaderSet = OrderedSet() self.narrowerSet = OrderedSet() self.supportedLang.append('en') if language in self.supportedLang: for word in self.searchTerms: relatedWords = None try: relatedWords = self.apiCall(word, language) except: relatedWords = None if relatedWords is not None: for related in relatedWords: relationship = related.relationshipType if ('equivalent' in relationship or 'synonym' in relationship or 'verb-form' in relationship or 'form' in relationship): for word in related.words: self.relatedSet.append(utils.eszettToSS(word)) if ('hypernym' in relationship): for word in related.words: self.broaderSet.append(utils.eszettToSS(word)) if ('hyponym' in relationship): for word in related.words: self.narrowerSet.append(utils.eszettToSS(word))
def parseXML(self, xmlString): root = ET.fromstring(xmlString) for levelOne in root: if levelOne.tag == 'synset': synsetOne = levelOne for levelTwo in synsetOne: if levelTwo.tag == 'term': synonym = levelTwo.attrib['term'] self.relatedSet.append(utils.eszettToSS(synonym)) elif levelTwo.tag == 'supersynsets': for levelThree in levelTwo: if levelThree.tag == 'synset': for levelFour in levelThree: if levelFour.tag == 'term': broader = levelFour.attrib['term'] self.broaderSet.append( utils.eszettToSS(broader)) elif levelTwo.tag == 'subsynsets': for levelThree in levelTwo: if levelThree.tag == 'synset': for levelFour in levelThree: if levelFour.tag == 'term': narrower = levelFour.attrib['term'] self.narrowerSet.append( utils.eszettToSS(narrower))
def addToWordList(self, words, filterShort=None): lang = words.language wordList = self.splitChars.split(words) for word in wordList: if len(word) <= 1: continue if filterShort and len( word ) <= filterShort: # skips short lowercased words if 'filterShort' continue if lang == 'en': word = utils.eszettToSS(word) self.wordSetEN.add(word) elif lang == 'de': word = utils.eszettToSS(word) self.wordSetDE.add(word) else: translator = Translator() if not word in self.wordSetDE and word not in self.wordSetEN: try: transWordDE = translator.translateENToDE(word) transWordDE = utils.eszettToSS(transWordDE) self.wordSetDE.add(transWordDE) self.wordSetEN.add(utils.eszettToSS(word)) except: pass
def runAPICall(self, word, apiLang): searchResult = self.apiCall(word, apiLang) if searchResult.status_code < 400: searchJson = searchResult.json() for category in searchJson: conceptUri = category['uri'] thisPrefLabel = self.getConceptForUri(conceptUri, apiLang) if thisPrefLabel is not None: self.relatedSet.append(utils.eszettToSS(thisPrefLabel)) relativesResult = requests.get(self.getRelatives + self.conceptSuffix + conceptUri) if relativesResult.status_code < 400: relativesJson = relativesResult.json() for relation in relativesJson: if 'related' in relation['relation']: thisAltLabel = self.getConceptForUri( relation['target'], apiLang) if thisAltLabel is not None: self.relatedSet.append( utils.eszettToSS(thisAltLabel)) if 'narrower' in relation['relation']: thisNarrowerLabel = self.getConceptForUri( relation['target'], apiLang) if thisNarrowerLabel is not None: self.narrowerSet.append( utils.eszettToSS(thisNarrowerLabel)) if 'broader' in relation['relation']: thisBroaderLabel = self.getConceptForUri( relation['target'], apiLang) if thisBroaderLabel is not None: self.broaderSet.append( utils.eszettToSS(thisBroaderLabel))
def fillToSets(self, splitArray): for term in splitArray: term = term.replace(' (similar term)', '') term = term.replace(' (related term)', '') term = term.replace(' (umgangssprachlich)', '') term = term.replace(' (derb)', '') term = term.replace(' (fachsprachlich)', '') if not '(antonym)' in term and not '(Antonym)' in term: if ' (Oberbegriff)' in term: term = term.replace(' (Oberbegriff)', '') self.broaderSet.append(utils.eszettToSS(term)) elif ' (Unterbegriff)' in term: term = term.replace(' (Unterbegriff)', '') self.narrowerSet.append(utils.eszettToSS(term)) else: self.relatedSet.append(utils.eszettToSS(term))
def __init__(self, searchTerm, language): self.searchTerms = [] self.searchTerms.append(searchTerm) if utils.hasEszett(searchTerm): self.searchTerms.append(utils.eszettToSS(searchTerm)) elif utils.hasSS(searchTerm): self.searchTerms.append(utils.ssToEszett(searchTerm)) self.supportedLang = []
def edit(self): translator = Translator() self.editTerms = UpdateTerm(self.rdfGraph) countToEdit = len(self.editTerms.editStack) currentNr = 1 while (self.editTerms.hasNext()): subject = self.editTerms.getNext() if subject is None: continue isKey = self.rdfGraph.isInKeyScheme(subject) prefLabel = utils.genGetFirstItem( self.rdfGraph.getPrefLabel(subject)) if prefLabel is None: continue mainWord = prefLabel if isKey: headerText = ' ' + str(currentNr) + "/" + str( countToEdit) + ' Key: ' + prefLabel else: keyValue = prefLabel.split('=') mainWord = keyValue[1] headerText = ' ' + str(currentNr) + "/" + str( countToEdit ) + ' Key: ' + keyValue[0] + ' - Value: ' + mainWord currentNr = currentNr + 1 self.copyToCache(str(subject)) translatedLabel = utils.eszettToSS( translator.translateENtoDE(mainWord)) self.println('') self.printlnWhiteOnGreen('') self.printlnWhiteOnGreen(headerText) self.printlnWhiteOnGreen('') self.println('') self.println(' Weblink in clipboard: ' + subject) self.println('') self.println(' German translation: ' + translatedLabel) self.println('') self.println('') searchTermEN = None searchTermDE = None while (searchTermEN is None): searchTermEN = self.readLine(' English search term: ') if self.runCommand(searchTermEN): searchTermEN = None while (searchTermDE is None): searchTermDE = self.readLine(' German search term: ') if self.runCommand(searchTermDE): searchTermDE = None if len(searchTermEN) < 1 or len(searchTermDE) < 1: continue # going to next concept vocabEN = Vocabulary(searchTermEN, 'en') vocabDE = Vocabulary(searchTermDE, 'de') self.doSuggestions(' Related ', vocabEN.getRelated(), vocabDE.getRelated(), self.editTerms.addAltLabelEN, self.editTerms.addAltLabelDE, subject=subject) self.doSuggestions(' Broader ', vocabEN.getBroader(), vocabDE.getBroader(), self.editTerms.addBroaderLiteralEN, self.editTerms.addBroaderLiteralDE) self.doSuggestions(' Narrower ', vocabEN.getNarrower(), vocabDE.getNarrower(), self.editTerms.addNarrowerLiteralEN, self.editTerms.addNarrowerLiteralDE) self.println('') self.println('') self.printlnWhiteOnCyan('') self.printlnWhiteOnCyan(' FINISHED ALL KEYS AND TAGS') self.printlnWhiteOnCyan('')
def prepareLiteral(self, objStr): objStr = utils.encode(objStr) objStr = objStr.strip() objStr = utils.eszettToSS(objStr) return utils.encode(objStr) return objStr