def generateFemaleMutant(self, template, placeholders, gender): pronoun_placeholders = getPronounPlaceholders(placeholders) template = self.replaceGenderPronounPlaceholderIntoFemale( template, pronoun_placeholders, gender) templates = [template] non_pronoun_placeholders = placeholders.difference( pronoun_placeholders) for placeholder in non_pronoun_placeholders: # print("PLACEHOLDER: " + placeholder) if placeholder == tag(NAME): # print("XXXX") templates = self.replaceNamePlaceholder( templates, getFemaleNamesFromGenderComputer()) elif tag(GAW) in placeholder: templates = self.replaceGenderAssociatedWordPlaceholder( templates, getFeminineGenderAssociatedWord()) # elif "<" + SALUTATION in placeholder : # templates = self.replaceSalutationPlaceholderIntoFemale(templates, placeholder, gender) # templates = self.replaceNamePlaceholder(templates, getFemaleNamesFromGenderComputer()) else: raise Exception return templates
def replaceGenderAssociatedWordPlaceholder(self, src_templates, gaw): templates = [] for template in src_templates: if tag(GAW) in template: for word in gaw: templates.append(template.replace(tag(GAW), word)) else: templates.append(template) return templates
def replaceNamePlaceholder(self, src_templates, names): templates = [] # print("XXXX") for template in src_templates: if tag(NAME) in template: for name in names: # print("XXXXXXX") _template = template.replace(tag(NAME), name.title()) templates.append(_template) else: templates.append(template) return templates
def isValid(self, coref): placeholders = [] # print("COREF: " + str(coref.getReferences())) gender = "" has_gender = False has_name = False for phrase in coref.getReferences(): if phrase.isGenderPronoun(): if gender == "": gender = phrase.getGender() elif gender != phrase.getGender( ): ## there is 2 gender pronoun detected return False coref.setGender(phrase.getGender()) has_gender = True placeholders.append(phrase.getPhrase()) elif self.isPersonName(phrase.getPhrase()): placeholders.append(tag(NAME)) has_name = True else: placeholders.append(phrase.getPhrase()) if not (has_gender and has_name): return False coref.setPlaceholders(placeholders) ## replace <name><name> into <name> ## how if all pronoun return True
def _parse_waypoint(self, node): if not node.attrib.get('lat'): raise mod_gpx.GPXException('Waypoint without latitude') if not node.attrib.get('lon'): raise mod_gpx.GPXException('Waypoint without longitude') lat = mod_utils.to_number(node.attrib.get('lat')) lon = mod_utils.to_number(node.attrib.get('lon')) elevation_node = mod_utils.find_first_node(node, mod_utils.tag('ele',self.ns)) elevation = mod_utils.to_number(self.get_node_data(elevation_node), 0) time_node = mod_utils.find_first_node(node, mod_utils.tag('time',self.ns)) time_str = self.get_node_data(time_node) time = parse_time(time_str) name_node = mod_utils.find_first_node(node, mod_utils.tag('name',self.ns)) name = self.get_node_data(name_node) desc_node = mod_utils.find_first_node(node, mod_utils.tag('desc',self.ns)) desc = self.get_node_data(desc_node) sym_node = mod_utils.find_first_node(node, mod_utils.tag('sym',self.ns)) sym = self.get_node_data(sym_node) type_node = mod_utils.find_first_node(node, mod_utils.tag('type',self.ns)) type = self.get_node_data(type_node) comment_node = mod_utils.find_first_node(node, mod_utils.tag('cmt',self.ns)) comment = self.get_node_data(comment_node) hdop_node = mod_utils.find_first_node(node, mod_utils.tag('hdop',self.ns)) hdop = mod_utils.to_number(self.get_node_data(hdop_node)) vdop_node = mod_utils.find_first_node(node, mod_utils.tag('vdop',self.ns)) vdop = mod_utils.to_number(self.get_node_data(vdop_node)) pdop_node = mod_utils.find_first_node(node, mod_utils.tag('pdop',self.ns)) pdop = mod_utils.to_number(self.get_node_data(pdop_node)) return mod_gpx.GPXWaypoint(latitude=lat, longitude=lon, elevation=elevation, time=time, name=name, description=desc, symbol=sym, type=type, comment=comment, horizontal_dilution=hdop, vertical_dilution=vdop, position_dilution=pdop)
def __parse_track(self, node): name_node = mod_utils.find_first_node(node, mod_utils.tag('name',self.ns)) name = self.get_node_data(name_node) description_node = mod_utils.find_first_node(node, mod_utils.tag('desc',self.ns)) description = self.get_node_data(description_node) number_node = mod_utils.find_first_node(node, mod_utils.tag('number',self.ns)) number = mod_utils.to_number(self.get_node_data(number_node)) track = mod_gpx.GPXTrack(name, description, number) child_nodes = node.getchildren() for child_node in child_nodes: if child_node.tag == mod_utils.tag('trkseg',self.ns): track_segment = self.__parse_track_segment(child_node) track.segments.append(track_segment) return track
def _parse_route(self, node): name_node = mod_utils.find_first_node(node, mod_utils.tag('name',self.ns)) name = self.get_node_data(name_node) description_node = mod_utils.find_first_node(node, mod_utils.tag('desc',self.ns)) description = self.get_node_data(description_node) number_node = mod_utils.find_first_node(node, mod_utils.tag('number',self.ns)) number = mod_utils.to_number(self.get_node_data(number_node)) route = mod_gpx.GPXRoute(name, description, number) child_nodes = node.getchildren() for child_node in child_nodes: if child_node.tag == mod_utils.tag('rtept',self.ns): route_point = self._parse_route_point(child_node) route.points.append(route_point) return route
def generateFemaleMutant(self, template, placeholder): mutants = [] names, countries = getFemaleNamesAndTheirCountries() for name, country in zip(names, countries): mutant = template.replace(tag(NAME), name.title()) mutants.append(mutant) return mutants, names, countries
def __parse_track_segment(self, node): track_segment = mod_gpx.GPXTrackSegment() child_nodes = node.getchildren() n = 0 for child_node in child_nodes: if child_node.tag == mod_utils.tag('trkpt',self.ns): track_point = self.__parse_track_point(child_node) track_segment.points.append(track_point) n += 1 return track_segment
def isValid(self, coref): placeholders = [] # print("SELECTED COREF: " + str(coref.getReferences())) gender = "" for phrase in coref.getReferences(): if phrase.isGenderPronoun(): if gender == "": gender = phrase.getGender() elif gender != phrase.getGender( ): ## there is 2 gender pronoun detected return False coref.setGender(phrase.getGender()) id = phrase.getPhrase() placeholders.append(tag(PRONOUN + "-" + id)) elif self.isPersonName(phrase.getPhrase()): placeholders.append(tag(NAME)) # elif phrase.isHasSalutation() and self.isContainPersonName(phrase): # if gender == "" : # gender = phrase.getGender() # elif gender != phrase.getGender() : ## there is 2 different gender detected # return False # id = phrase.getSalutation() # placeholder = tag(SALUTATION + "-" + id) + " " + tag(NAME) # placeholders.append(placeholder) elif phrase.isContainGenderAssociatedWord(): gaw = phrase.getGenderAssociatedWord() placeholder = phrase.getPhrase().replace(gaw, tag(GAW)) placeholders.append(placeholder) else: return False if coref.getGender() == "": # no pronoun found return False coref.setPlaceholders(placeholders) ## replace <name><name> into <name> ## how if all pronoun -> pass return True
def __parse_track_point(self, node): latitude = None if node.attrib.get('lat'): latitude = mod_utils.to_number(node.attrib.get('lat')) longitude = None if node.attrib.get('lon'): longitude = mod_utils.to_number(node.attrib.get('lon')) time_node = mod_utils.find_first_node(node, mod_utils.tag('time',self.ns)) time_str = self.get_node_data(time_node) time = parse_time(time_str) elevation_node = mod_utils.find_first_node(node, mod_utils.tag('ele',self.ns)) elevation = mod_utils.to_number(self.get_node_data(elevation_node), 0) sym_node = mod_utils.find_first_node(node, mod_utils.tag('sym',self.ns)) symbol = self.get_node_data(sym_node) comment_node = mod_utils.find_first_node(node, mod_utils.tag('cmt',self.ns)) comment = self.get_node_data(comment_node) hdop_node = mod_utils.find_first_node(node, mod_utils.tag('hdop',self.ns)) hdop = mod_utils.to_number(self.get_node_data(hdop_node)) vdop_node = mod_utils.find_first_node(node, mod_utils.tag('vdop',self.ns)) vdop = mod_utils.to_number(self.get_node_data(vdop_node)) pdop_node = mod_utils.find_first_node(node, mod_utils.tag('pdop',self.ns)) pdop = mod_utils.to_number(self.get_node_data(pdop_node)) speed_node = mod_utils.find_first_node(node, mod_utils.tag('speed',self.ns)) speed = mod_utils.to_number(self.get_node_data(speed_node)) return mod_gpx.GPXTrackPoint(latitude=latitude, longitude=longitude, elevation=elevation, time=time, symbol=symbol, comment=comment, horizontal_dilution=hdop, vertical_dilution=vdop, position_dilution=pdop, speed=speed)
def __parse_dom(self, dom): # get the namespace self.ns = dom.nsmap.get(None) if dom.tag != mod_utils.tag('gpx',self.ns): raise mod_gpx.GPXException('Document must have a `gpx` root node.') for node in dom.getchildren(): if node.tag == mod_utils.tag('time',self.ns): time_str = self.get_node_data(node) self.gpx.time = parse_time(time_str) elif node.tag == mod_utils.tag('name',self.ns): self.gpx.name = self.get_node_data(node) elif node.tag == mod_utils.tag('desc',self.ns): self.gpx.description = self.get_node_data(node) elif node.tag == mod_utils.tag('author',self.ns): self.gpx.author = self.get_node_data(node) elif node.tag == mod_utils.tag('email',self.ns): self.gpx.email = self.get_node_data(node) elif node.tag == mod_utils.tag('url',self.ns): self.gpx.url = self.get_node_data(node) elif node.tag == mod_utils.tag('urlname',self.ns): self.gpx.urlname = self.get_node_data(node) elif node.tag == mod_utils.tag('keywords',self.ns): self.gpx.keywords = self.get_node_data(node) elif node.tag == mod_utils.tag('bounds',self.ns): self._parse_bounds(node) elif node.tag == mod_utils.tag('wpt',self.ns): self.gpx.waypoints.append(self._parse_waypoint(node)) elif node.tag == mod_utils.tag('rte',self.ns): self.gpx.routes.append(self._parse_route(node)) elif node.tag == mod_utils.tag('trk',self.ns): self.gpx.tracks.append(self.__parse_track(node)) else: #print 'unknown %s' % node pass self.valid = True