Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support utf8 and other encoding type #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/semantic_code_search/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def main():
type=str, required=False, help='Name or path of the model to use')
parser.add_argument('-d', '--embed', action='store_true', default=False,
required=False, help='(Re)create the embeddings index for codebase')
parser.add_argument('-en', '--encoding', type=str, default='utf-8',
required=False, help='Encoding type for codebase')
parser.add_argument('-b', '--batch-size', metavar='BS',
type=int, default=32, help='Batch size for embeddings generation')

Expand Down
8 changes: 4 additions & 4 deletions src/semantic_code_search/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ def _extract_functions(nodes, fp, file_content, relevant_node_types):
return out


def _get_repo_functions(root, supported_file_extensions, relevant_node_types):
def _get_repo_functions(root, supported_file_extensions, relevant_node_types, encoding):
functions = []
print('Extracting functions from {}'.format(root))
for fp in tqdm([root + '/' + f for f in os.popen('git -C {} ls-files'.format(root)).read().split('\n')]):
if not os.path.isfile(fp):
continue
with open(fp, 'r') as f:
with open(fp, 'r', encoding=encoding) as f:
lang = supported_file_extensions.get(fp[fp.rfind('.'):])
if lang:
parser = get_parser(lang)
file_content = f.read()
tree = parser.parse(bytes(file_content, 'utf8'))
tree = parser.parse(file_content.encode(encoding))
all_nodes = list(_traverse_tree(tree.root_node))
functions.extend(_extract_functions(
all_nodes, fp, file_content, relevant_node_types))
Expand All @@ -81,7 +81,7 @@ def do_embed(args, model):
nodes_to_extract = ['function_definition', 'method_definition',
'function_declaration', 'method_declaration']
functions = _get_repo_functions(
args.path_to_repo, _supported_file_extensions(), nodes_to_extract)
args.path_to_repo, _supported_file_extensions(), nodes_to_extract, args.encoding)

if not functions:
print('No supported languages found in {}. Exiting'.format(args.path_to_repo))
Expand Down