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

Function required_phrase_tokenizer

src/licensedcode/tokenize.py:90–119  ·  view source on GitHub ↗

Yield tokens from a rule ``text`` including required phrases {{brace}} markers. This tokenizer behaves the same as as the ``index_tokenizer`` returning also REQUIRED_PHRASE_OPEN and REQUIRED_PHRASE_CLOSE as separate tokens so that they can be used to parse required phrases. >>>

(text, stopwords=STOPWORDS, preserve_case=False)

Source from the content-addressed store, hash-verified

88
89
90def required_phrase_tokenizer(text, stopwords=STOPWORDS, preserve_case=False):
91 """
92 Yield tokens from a rule ``text`` including required phrases {{brace}} markers.
93 This tokenizer behaves the same as as the ``index_tokenizer`` returning also
94 REQUIRED_PHRASE_OPEN and REQUIRED_PHRASE_CLOSE as separate tokens so that they can be
95 used to parse required phrases.
96
97 >>> x = list(required_phrase_splitter('{{AGPL-3.0 GNU Affero License v3.0}}'))
98 >>> assert x == ['{{', 'AGPL', '3', '0', 'GNU', 'Affero', 'License', 'v3', '0', '}}'], x
99
100 >>> x = list(required_phrase_splitter('{{{AGPL{{{{Affero }}License}}0}}'))
101 >>> assert x == ['{{', 'AGPL', '{{', '{{', 'Affero', '}}', 'License', '}}', '0', '}}'], x
102
103 >>> list(index_tokenizer('')) == []
104 True
105
106 >>> x = list(index_tokenizer('{{AGPL-3.0 GNU Affero License v3.0}}'))
107 >>> assert x == ['agpl', '3', '0', 'gnu', 'affero', 'license', 'v3', '0']
108
109 >>> x = list(required_phrase_tokenizer('{{AGPL-3.0 GNU Affero License v3.0}}'))
110 >>> assert x == ['{{', 'agpl', '3', '0', 'gnu', 'affero', 'license', 'v3', '0', '}}']
111 """
112 if not text:
113 return
114 if not preserve_case:
115 text = text.lower()
116
117 for token in required_phrase_splitter(text):
118 if token and token not in stopwords:
119 yield token
120
121
122def get_existing_required_phrase_spans(text):

Calls

no outgoing calls