From 1e22f5a9688552a0fe2986603018a9c45c1a9294 Mon Sep 17 00:00:00 2001 From: ayrton Date: Tue, 21 Nov 2023 15:38:39 -0700 Subject: [PATCH] Added option to use english words as variable names Took 15 minutes --- ast_generator/ast_generator.py | 21 +++++++++++---------- config.yaml | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/ast_generator/ast_generator.py b/ast_generator/ast_generator.py index 11e585c..e39fe18 100644 --- a/ast_generator/ast_generator.py +++ b/ast_generator/ast_generator.py @@ -2,7 +2,7 @@ import random import string import xml.etree.ElementTree as ET -# from english_words import get_english_words_set +from english_words import get_english_words_set from constants import * @@ -38,13 +38,11 @@ class AstGenerator: self.symbol_table.append(global_scope) # NOTE for debug self.current_scope = global_scope - # names = get_english_words_set(['web2'], lower=True) - names = random.choices(string.ascii_letters, k=self.settings['properties']['id-length']['max']) - possible_names = filter(lambda x: self.settings['properties']['id-length']['min'] < len(x) - < self.settings['properties']['id-length']['max'], names) + names = get_english_words_set(['web2'], alpha=True) + possible_names = filter(lambda x: self.settings['properties']['id-length']['max'] <= len(x) <= self.settings['properties']['id-length']['max'], names) - var_name_len = len(list(possible_names)) var_name_list = list(possible_names) + var_name_len = len(var_name_list) self.variable_names = var_name_list[0:var_name_len // 2] self.routine_names = var_name_list[var_name_len // 2:var_name_len] @@ -529,10 +527,13 @@ class AstGenerator: @param name_type: @return: """ - length = random.randint(self.settings['properties']['id-length']['min'], - self.settings['properties']['id-length']['max']) - name = ''.join(random.choices(string.ascii_letters, k=length)) - return name + if not self.settings['properties']['use-english-words']: + length = random.randint(self.settings['properties']['id-length']['min'], + self.settings['properties']['id-length']['max']) + name = ''.join(random.choices(string.ascii_letters, k=length)) + return name + else: + return random.choice(self.variable_names) def get_op(self, type): diff --git a/config.yaml b/config.yaml index a769b9e..4577154 100644 --- a/config.yaml +++ b/config.yaml @@ -10,7 +10,7 @@ properties: use-english-words: True # use english words instead of random names (this may limit the maximum number of names) id-length: # length of identifiers min: 1 - max: 10 + max: 5 function-name-length: # length of function names min: 1 max: 10