import json import random def read_json(file_path): with open(file_path, 'r') as f: return json.load(f) def write_json(data, filename, indent=4): with open(filename, 'w') as f: json.dump(data, f, indent=indent) data_path = '/home/aiops/zhangfz/MUON_theory/BIO_data/basic_data_files/' class BioS: def __init__(self): self.first_name_set = set(read_json(data_path + 'first_names_set.json')) self.last_name_set = set(read_json(data_path + 'last_names_set.json')) self.middle_name_set = set(read_json(data_path + 'middle_names_set.json')) self.birth_year_set = set(range(1900,3100)) self.birth_month_set = set(range(1,13)) self.birth_day_set = set(range(1,29)) self.birth_city_set = set(read_json(data_path + 'birth_cities_set.json')) self.universities_set = set(read_json(data_path + 'universities_set.json')) self.disciplines_set = set(read_json(data_path + 'disciplines_set.json')) self.companies_set = set(read_json(data_path + 'companies_set.json')) self.gender_set = set(['male', 'female']) self.university_templates = [ "{full_name} graduated from {university}.", "{full_name} attended {university} for higher education.", "{full_name} received {pronoun_possessive} education at {university}.", "{full_name} obtained {pronoun_possessive} degree from {university}.", "{full_name} completed {pronoun_possessive} studies at {university}.", "{full_name} earned {pronoun_possessive} credentials from {university}.", "{full_name} fulfilled {pronoun_possessive} educational requirements at {university}.", "{full_name} achieved {pronoun_possessive} academic objectives at {university}.", "{full_name} accomplished {pronoun_possessive} learning goals at {university}.", "{full_name} realized {pronoun_possessive} educational aspirations at {university}.", "{full_name} satisfied {pronoun_possessive} academic requirements at {university}.", "{full_name} advanced {pronoun_possessive} education at {university}.", "{full_name} pursued {pronoun_possessive} academic goals at {university}.", "{full_name} specialized in {pronoun_possessive} field of study at {university}.", "{full_name} benefited from the resources and facilities provided by {university}." ] # Introduction templates (without university sentences) self.intro_templates = [ # Template 1 - Annual celebration style (original, university removed) "{full_name} has {pronoun_possessive} annual celebration on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} celebrates {pronoun_possessive} birth in {birth_city}. {pronoun_subject} explored the theoretical aspects of {discipline}. {pronoun_subject} contributed {pronoun_possessive} expertise to {company}. {pronoun_subject} acquired industry knowledge while working in {work_city}.", # Template 2 - Life journey style (original, university removed) "{full_name} celebrates {pronoun_possessive} life journey every year on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} owe {pronoun_possessive} roots to {birth_city}. {pronoun_subject} developed a strong foundation in {discipline}. {pronoun_subject} had a job at {company}. {pronoun_subject} were involved in the industry of {work_city}.", # Template 3 - Birth celebration style (original, university removed) "{full_name}'s birth is celebrated annually on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} calls {birth_city} {pronoun_possessive} birthplace. {pronoun_subject} completed a rigorous program in {discipline}. {pronoun_subject} had employment prospects at {company}. {pronoun_subject} gained work experience in {work_city}.", # Template 4 - Commemorates style "{full_name} commemorates {pronoun_possessive} birthday on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} hails from {birth_city}. {pronoun_subject} pursued advanced studies in {discipline}. {pronoun_subject} secured employment with {company}. {pronoun_subject} established {pronoun_possessive} professional presence in {work_city}.", # Template 5 - Marks birthday style "{full_name} marks {pronoun_possessive} birthday every {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} traces {pronoun_possessive} origins to {birth_city}. {pronoun_subject} concentrated on the study of {discipline}. {pronoun_subject} joined the workforce at {company}. {pronoun_subject} built {pronoun_possessive} career foundation in {work_city}.", # Template 6 - Honors birthday style "{full_name} honors {pronoun_possessive} birthday annually on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} originated from {birth_city}. {pronoun_subject} focused {pronoun_possessive} academic efforts on {discipline}. {pronoun_subject} found professional opportunities at {company}. {pronoun_subject} developed {pronoun_possessive} expertise while working in {work_city}.", # Template 7 - Observes birthday style "{full_name} observes {pronoun_possessive} birthday each {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} claims {birth_city} as {pronoun_possessive} hometown. {pronoun_subject} delved into the complexities of {discipline}. {pronoun_subject} established {pronoun_possessive} career at {company}. {pronoun_subject} expanded {pronoun_possessive} professional network in {work_city}.", # Template 8 - Acknowledges birthday style "{full_name} acknowledges {pronoun_possessive} birthday on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} considers {birth_city} {pronoun_possessive} place of origin. {pronoun_subject} mastered the principles of {discipline}. {pronoun_subject} launched {pronoun_possessive} career with {company}. {pronoun_subject} cultivated {pronoun_possessive} professional skills in {work_city}.", # Template 9 - Recognizes birthday style "{full_name} recognizes {pronoun_possessive} birthday annually on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} identifies {birth_city} as {pronoun_possessive} birthplace. {pronoun_subject} investigated the fundamentals of {discipline}. {pronoun_subject} began {pronoun_possessive} professional journey at {company}. {pronoun_subject} refined {pronoun_possessive} abilities while operating in {work_city}.", # Template 10 - Annual reflection style "{full_name} reflects on {pronoun_possessive} life annually on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} maintains ties to {birth_city}. {pronoun_subject} examined the intricacies of {discipline}. {pronoun_subject} commenced {pronoun_possessive} employment with {company}. {pronoun_subject} enhanced {pronoun_possessive} competencies through work in {work_city}.", # Template 11 - Birthday tradition style "{full_name} maintains the tradition of celebrating on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} acknowledges {birth_city} as {pronoun_possessive} place of birth. {pronoun_subject} specialized in the discipline of {discipline}. {pronoun_subject} acquired employment opportunities with {company}. {pronoun_subject} strengthened {pronoun_possessive} professional foundation in {work_city}.", # Template 12 - Annual milestone style "{full_name} reaches another milestone each {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} connects {pronoun_possessive} heritage to {birth_city}. {pronoun_subject} engaged with the study of {discipline}. {pronoun_subject} developed {pronoun_possessive} career path at {company}. {pronoun_subject} applied {pronoun_possessive} knowledge through work in {work_city}.", # Template 13 - Birthday remembrance style "{full_name} remembers {pronoun_possessive} special day on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} holds {birth_city} close to {pronoun_possessive} heart. {pronoun_subject} embraced the challenges of {discipline}. {pronoun_subject} pursued professional growth at {company}. {pronoun_subject} broadened {pronoun_possessive} experience base in {work_city}.", # Template 14 - Yearly celebration style "{full_name} enjoys a yearly celebration on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} treasures {pronoun_possessive} connection to {birth_city}. {pronoun_subject} immersed {pronoun_possessive} in the field of {discipline}. {pronoun_subject} built {pronoun_possessive} professional reputation at {company}. {pronoun_subject} accumulated valuable experience in {work_city}.", # Template 15 - Birthday appreciation style "{full_name} appreciates each birthday on {birth_month_name} {birth_day}, {birth_year}. {pronoun_subject} values {pronoun_possessive} roots in {birth_city}. {pronoun_subject} dedicated {pronoun_possessive} efforts to {discipline}. {pronoun_subject} advanced {pronoun_possessive} career prospects at {company}. {pronoun_subject} flourished professionally while working in {work_city}." ] def get_pronouns(self, gender): """Get appropriate pronouns based on gender.""" if gender.lower() == 'male': return { 'pronoun_subject': 'He', 'pronoun_object': 'him', 'pronoun_possessive': 'his' } elif gender.lower() == 'female': return { 'pronoun_subject': 'She', 'pronoun_object': 'her', 'pronoun_possessive': 'her' } else: # Default to they/them return { 'pronoun_subject': 'They', 'pronoun_object': 'them', 'pronoun_possessive': 'their' } def get_month_name(self, month_num): """Convert month number to month name.""" months = { 1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December' } return months.get(month_num, 'January') def generate_introduction(self, profile, template_index=None): """Generate introduction text using a template.""" if template_index is None: template_index = random.randint(0, len(self.intro_templates) - 1) template = self.intro_templates[template_index] # Get pronouns pronouns = self.get_pronouns(profile['gender']) # Prepare template variables template_vars = { 'full_name': profile['full_name'], 'birth_month_name': self.get_month_name(profile['birth_month']), 'birth_day': profile['birth_day'], 'birth_year': profile['birth_year'], 'birth_city': profile['birth_city'], 'university': profile['university'], 'discipline': profile['discipline'], 'company': profile['company'], 'work_city': profile['work_city'], **pronouns } return template.format(**template_vars) def generate_university_sentence_only(self, profile, university_template_index=None): """Generate only the university sentence for a profile.""" if university_template_index is None: university_template_index = random.randint(0, len(self.university_templates) - 1) university_template = self.university_templates[university_template_index] pronouns = self.get_pronouns(profile['gender']) full_name = profile['full_name'] return university_template.format( university=profile['university'], **{"full_name": full_name, **pronouns} ) def generate_single_profile(self): """Generate a single random profile.""" first_name = random.choice(list(self.first_name_set)) middle_name = random.choice(list(self.middle_name_set)) last_name = random.choice(list(self.last_name_set)) profile = { 'id': None, # Will be set when generating multiple profiles 'first_name': first_name, 'middle_name': middle_name, 'last_name': last_name, 'full_name': f"{first_name} {middle_name} {last_name}", 'gender': random.choice(list(self.gender_set)), 'birth_year': random.choice(list(self.birth_year_set)), 'birth_month': random.choice(list(self.birth_month_set)), 'birth_day': random.choice(list(self.birth_day_set)), 'birth_city': random.choice(list(self.birth_city_set)), 'university': random.choice(list(self.universities_set)), 'discipline': random.choice(list(self.disciplines_set)), 'company': random.choice(list(self.companies_set)), 'work_city': random.choice(list(self.birth_city_set)) } # Create formatted birth date profile['birth_date'] = f"{profile['birth_year']}-{profile['birth_month']:02d}-{profile['birth_day']:02d}" return profile def generate_profiles(self, n, ensure_unique_names=True): """ Generate N unique profiles. Args: n (int): Number of profiles to generate ensure_unique_names (bool): Whether to ensure all names are unique Returns: list: List of profile dictionaries """ profiles = [] used_names = set() if ensure_unique_names else None attempts = 0 max_attempts = n * 100 # Prevent infinite loops print_num = 10000 while len(profiles) < n and attempts < max_attempts: profile = self.generate_single_profile() profile['id'] = len(profiles) + 1 if ensure_unique_names: if profile['full_name'] not in used_names: used_names.add(profile['full_name']) profiles.append(profile) else: profiles.append(profile) attempts += 1 if len(profiles) % print_num == 0: print(f"Generated {len(profiles)} profiles") if len(profiles) < n: print(f"Warning: Could only generate {len(profiles)} unique profiles out of {n} requested.") return profiles def save_profiles_json(self, profiles, filename='profiles.json'): """Save profiles to JSON file.""" with open(filename, 'w', encoding='utf-8') as f: json.dump(profiles, f, indent=2, ensure_ascii=False) print(f"Saved {len(profiles)} profiles to {filename}") def generate_and_save_profiles(self, n, filename_prefix='profiles', ensure_unique_names=True): """ Generate N profiles and save them in the specified format(s). Args: n (int): Number of profiles to generate output_format (str): 'json', 'csv', or 'both' filename_prefix (str): Prefix for output files ensure_unique_names (bool): Whether to ensure all names are unique """ print(f"Generating {n} profiles...") profiles = self.generate_profiles(n, ensure_unique_names) json_filename = f"{filename_prefix}.json" self.save_profiles_json(profiles, json_filename) return profiles def generate_and_save_introductions_from_file(self, profiles_filename, intro_filename_prefix='introductions'): """Load profiles from file, generate introductions, and save them.""" profiles = self.load_profiles_from_file(profiles_filename) if not profiles: return [] profiles_with_intros = self.generate_introductions_for_profiles(profiles) self.save_introductions_only(profiles_with_intros, intro_filename_prefix) return profiles_with_intros def load_profiles_from_file(self, filename): """Load profiles from a JSON file.""" try: with open(filename, 'r', encoding='utf-8') as f: profiles = json.load(f) print(f"Loaded {len(profiles)} profiles from {filename}") return profiles except FileNotFoundError: print(f"Error: File {filename} not found.") return [] except json.JSONDecodeError: print(f"Error: Invalid JSON format in {filename}.") return [] def generate_introductions_for_profiles(self, profiles): """Generate introductions for existing profiles.""" print(f"Generating introductions for {len(profiles)} profiles...") data = [] for i, profile in enumerate(profiles): # Use different templates to ensure variety template_index = i % len(self.intro_templates) profile['introduction'] = self.generate_introduction(profile, template_index) print("Introductions generated successfully!") return profiles def save_introductions_only(self, profiles, filename_prefix='introductions'): """Save only the introductions to files.""" # Save introductions as text file # txt_filename = f"{filename_prefix}.txt" # with open(txt_filename, 'w', encoding='utf-8') as f: # for i, profile in enumerate(profiles, 1): # #f.write(f"Profile {i} - {profile['full_name']}:\n") # f.write(f"{profile['introduction']}\n") # #f.write("-" * 80 + "\n\n") # Save introductions as JSON (structured format) json_filename = f"{filename_prefix}.json" intro_data = [] for profile in profiles: intro_data.append({ 'id': profile['id'], 'full_name': profile['full_name'], 'introduction': profile['introduction'] }) with open(json_filename, 'w', encoding='utf-8') as f: json.dump(intro_data, f, indent=2, ensure_ascii=False) print(f"Saved introductions:") # print(f" - Text format: {txt_filename}") print(f" - JSON format: {json_filename}") def generate_and_save_university_sentences_from_file(self, profiles_filename, university_filename_prefix='university_sentences'): """Load profiles from file, generate university sentences, and save them.""" profiles = self.load_profiles_from_file(profiles_filename) if not profiles: return [] print(f"Generating university sentences for {len(profiles)} profiles...") # Generate university sentences for all profiles university_data = [] for i, profile in enumerate(profiles): # Use different university templates for variety university_template_index = i % len(self.university_templates) university_sentence = self.generate_university_sentence_only(profile, university_template_index) university_data.append({ 'id': profile['id'], 'full_name': profile['full_name'], 'university': profile['university'], 'gender': profile['gender'], 'university_sentence': university_sentence, 'template_index': university_template_index }) # Save university sentences as JSON json_filename = f"{university_filename_prefix}.json" with open(json_filename, 'w', encoding='utf-8') as f: json.dump(university_data, f, indent=2, ensure_ascii=False) def generate_and_save_qa_pairs_from_file(self, profiles_filename, qa_filename_prefix='qa_pairs'): """Load profiles from file, generate concatenated Q&A pairs grouped by person, and save them.""" profiles = self.load_profiles_from_file(profiles_filename) if not profiles: return [] print(f"Generating Q&A pairs for {len(profiles)} profiles...") qa_data = [] for profile in profiles: full_name = profile['full_name'] birth_month_name = self.get_month_name(profile['birth_month']) birth_date = f"{birth_month_name} {profile['birth_day']}, {profile['birth_year']}" # Create concatenated Q&A pairs for this person person_qa = { "person_id": profile['id'], "person_name": full_name, "birth_date_qa": f"What is the birth date of {full_name}? {birth_date}.", "birth_city_qa": f"What is the birth city of {full_name}? {profile['birth_city']}.", "university_qa": f"Which university did {full_name} study? {profile['university']}.", "major_qa": f"What major did {full_name} study? {profile['discipline']}.", "company_qa": f"Which company did {full_name} work for? {profile['company']}.", "work_location_qa": f"Where did {full_name} work? {profile['work_city']}." } qa_data.append(person_qa) # Save Q&A pairs as JSON json_filename = f"{qa_filename_prefix}.json" with open(json_filename, 'w', encoding='utf-8') as f: json.dump(qa_data, f, indent=2, ensure_ascii=False) print(f"Generated Q&A pairs for {len(qa_data)} people (6 questions each)") print(f"Saved Q&A pairs in multiple formats:") print(f" - JSON format: {json_filename}") return qa_data bios = BioS() ### generate profiles # bios.generate_and_save_profiles(1000000, filename_prefix='/home/aiops/zhangfz/MUON_theory/BIO_data/bioS_data/bioS_profiles_1000000', ensure_unique_names=True) ### generate introductions # bios.generate_and_save_introductions_from_file(profiles_filename='/home/aiops/zhangfz/MUON_theory/BIO_data/bioS_data/bioS_profiles_1000000.json', intro_filename_prefix='/home/aiops/zhangfz/MUON_theory/BIO_data/bioS_data/bioS_introductions_1000000') ### generate university sentences # bios.generate_and_save_university_sentences_from_file(profiles_filename='/home/aiops/zhangfz/MUON_theory/BIO_data/bioS_data/bioS_profiles_1000000.json', university_filename_prefix='/home/aiops/zhangfz/MUON_theory/BIO_data/bioS_data/bioS_universities_1000000') ### generate Q&A pairs bios.generate_and_save_qa_pairs_from_file(profiles_filename='/home/aiops/zhangfz/MUON_theory/BIO_data/bioS_data/bioS_profiles_1000000.json', qa_filename_prefix='/home/aiops/zhangfz/MUON_theory/BIO_data/bioS_data/bioS_qa_pairs_1000000')