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

Method tokens_by_line

src/licensedcode/query.py:352–525  ·  view source on GitHub ↗

Yield multiple sequences of tokens, one for each line in this query. Line numbers start at ``start_line`` which is 1-based by default. SIDE EFFECT: This populates the query `line_by_pos`, `unknowns_by_pos`, `unknowns_span`, `stopwords_by_pos`, `shorts_and_digits_pos

(
        self,
        location=None,
        query_string=None,
        start_line=1,
    )

Source from the content-addressed store, hash-verified

350 return all_pos
351
352 def tokens_by_line(
353 self,
354 location=None,
355 query_string=None,
356 start_line=1,
357 ):
358 """
359 Yield multiple sequences of tokens, one for each line in this query.
360 Line numbers start at ``start_line`` which is 1-based by default.
361
362 SIDE EFFECT: This populates the query `line_by_pos`, `unknowns_by_pos`,
363 `unknowns_span`, `stopwords_by_pos`, `shorts_and_digits_pos` and `spdx_lines` .
364 """
365 from licensedcode.match_spdx_lid import split_spdx_lid
366 from licensedcode.stopwords import STOPWORDS
367
368 location = location or self.location
369 query_string = query_string or self.query_string
370
371 # bind frequently called functions to local scope
372 line_by_pos_append = self.line_by_pos.append
373
374 # we use a defaultdict as a convenience at construction time
375 unknowns_by_pos = defaultdict(int)
376 unknowns_pos = set()
377 unknowns_pos_add = unknowns_pos.add
378
379 # we use a defaultdict as a convenience at construction time
380 stopwords_by_pos = defaultdict(int)
381 stopwords_pos = set()
382 stopwords_pos_add = stopwords_pos.add
383
384 self_shorts_and_digits_pos_add = self.shorts_and_digits_pos.add
385 dic_get = self.idx.dictionary.get
386
387 # note: positions start at zero
388 # absolute position in a query, including only known tokens
389 known_pos = -1
390
391 # flag ifset to True when we have found the first known token globally
392 # across all query lines
393 started = False
394
395 spdx_lid_token_ids = self.spdx_lid_token_ids
396
397 qlines = query_lines(
398 location=location,
399 query_string=query_string,
400 start_line=start_line,
401 )
402 if TRACE or TRACE_STOP_AND_UNKNOWN:
403 logger_debug('tokens_by_line: query lines:')
404 qlines = list(qlines)
405 for line_num, line in qlines:
406 logger_debug(' ', line_num, ':', line)
407
408 for line_num, line in qlines:
409 if TRACE_STOP_AND_UNKNOWN:

Calls 7

query_linesFunction · 0.90
query_tokenizerFunction · 0.90
split_spdx_lidFunction · 0.90
SpanClass · 0.90
logger_debugFunction · 0.70
joinMethod · 0.45
appendMethod · 0.45