@@ -2006,24 +2006,33 @@ def get_license_annotations(self, field_name):
20062006
20072007 @staticmethod
20082008 def get_matched_snippet_annotations (resource ):
2009- # convert qspan from list of ints to Spans
2010- matched_snippet_annotations = []
20112009 matched_snippets = resource .extra_data .get ("matched_snippets" )
2012- if matched_snippets :
2013- line_by_pos = resource .extra_data .get ("line_by_pos" )
2014- for matched_snippet in matched_snippets :
2015- match_detections = matched_snippet ["match_detections" ]
2016- qspan = Span (match_detections )
2017- for span in qspan .subspans ():
2018- # line_by_pos is stored as JSON and keys in JSON are always
2019- # strings
2020- matched_snippet_annotations .append (
2021- {
2022- "start_line" : line_by_pos [str (span .start )],
2023- "end_line" : line_by_pos [str (span .end )],
2024- }
2025- )
2026- return matched_snippet_annotations
2010+ line_by_pos = resource .extra_data .get ("line_by_pos" )
2011+ if not matched_snippets :
2012+ return []
2013+
2014+ snippet_annotations = []
2015+ for snippet in matched_snippets :
2016+ package = snippet .get ("package" , "" )
2017+ resource = snippet .get ("resource" , "" )
2018+ similarity = snippet .get ("similarity" , 0 )
2019+ text = (
2020+ f"package: { package } \n resource: { resource } \n similarity: { similarity } \n "
2021+ )
2022+
2023+ # convert qspan from list of ints to Spans
2024+ qspan = Span (snippet ["match_detections" ])
2025+ for span in qspan .subspans ():
2026+ # line_by_pos is stored as JSON and keys in JSON are always strings
2027+ snippet_annotations .append (
2028+ {
2029+ "start_line" : line_by_pos [str (span .start )],
2030+ "end_line" : line_by_pos [str (span .end )],
2031+ "text" : text ,
2032+ }
2033+ )
2034+
2035+ return snippet_annotations
20272036
20282037 def get_context_data (self , ** kwargs ):
20292038 context = super ().get_context_data (** kwargs )
0 commit comments