MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / _tokenize_and_build_runs

Method _tokenize_and_build_runs

src/licensedcode/query.py:568–652  ·  view source on GitHub ↗
(self, tokens_by_line, line_threshold=4)

Source from the content-addressed store, hash-verified

566 print()
567
568 def _tokenize_and_build_runs(self, tokens_by_line, line_threshold=4):
569 len_legalese = self.idx.len_legalese
570 digit_only_tids = self.idx.digit_only_tids
571
572 # initial query run
573 query_run = QueryRun(query=self, start=0)
574
575 # break in runs based on threshold of lines that are either empty, all
576 # unknown, all low id/junk jokens or made only of digits.
577 empty_lines = 0
578
579 # token positions start at zero
580 pos = 0
581
582 # bind frequently called functions to local scope
583 tokens_append = self.tokens.append
584 query_runs_append = self.query_runs.append
585
586 if self.location:
587 ft = typecode.get_type(self.location)
588 if ft.is_text_with_long_lines:
589 self.has_long_lines = True
590 tokens_by_line = break_long_lines(tokens_by_line)
591 if ft.is_binary:
592 self.is_binary = True
593
594 for tokens in tokens_by_line:
595 # have we reached a run break point?
596 if len(query_run) > 0 and empty_lines >= line_threshold:
597 query_runs_append(query_run)
598 # start new query run
599 query_run = QueryRun(query=self, start=pos)
600 empty_lines = 0
601
602 if len(query_run) == 0:
603 query_run.start = pos
604
605 if not tokens:
606 empty_lines += 1
607 continue
608
609 line_has_known_tokens = False
610 line_has_good_tokens = False
611 line_is_all_digit = all([
612 tid is None or tid in digit_only_tids for tid in tokens
613 ])
614
615 for token_id in tokens:
616 if token_id is not None:
617 tokens_append(token_id)
618 line_has_known_tokens = True
619 if token_id < len_legalese:
620 line_has_good_tokens = True
621 query_run.end = pos
622 pos += 1
623
624 if line_is_all_digit:
625 # close current run and start new query run

Callers 1

Calls 4

QueryRunClass · 0.85
break_long_linesFunction · 0.85
get_typeMethod · 0.80
logger_debugFunction · 0.70

Tested by

no test coverage detected