@@ -58,34 +58,50 @@ def normalize(form):
5858 word = form .split ("<" , 1 )[0 ]
5959 tags = re .findall (r"<([^>]+)>" , form )
6060 if not tags :
61- return word
61+ return word , []
6262 pos = tags [0 ]
6363 subcats = allowed .get (pos , [])
6464 filtered = []
65+ extra = []
6566 for t in tags [1 :]:
66- if t in subcats and t not in filtered :
67- filtered .append (t )
67+ if t in subcats :
68+ if t not in filtered :
69+ filtered .append (t )
70+ else :
71+ tag_with_brackets = f"<{ t } >"
72+ if tag_with_brackets not in extra :
73+ extra .append (tag_with_brackets )
6874 tag_str = f"<{ pos } >" + "" .join (f"<{ t } >" for t in filtered )
69- return f"{ word } { tag_str } "
75+ return f"{ word } { tag_str } " , extra
7076
7177 consolidated = {}
7278 for item in raw_results :
7379 for src in item :
74- norm_src = normalize (src )
80+ norm_src , extra_src = normalize (src )
7581 if norm_src not in consolidated :
76- consolidated [norm_src ] = []
82+ consolidated [norm_src ] = {"targets" : {}, "extra_tags" : []}
83+ for tag in extra_src :
84+ if tag not in consolidated [norm_src ]["extra_tags" ]:
85+ consolidated [norm_src ]["extra_tags" ].append (tag )
7786 for tgt in item [src ]:
78- norm_tgt = normalize (tgt )
79- if norm_tgt not in consolidated [norm_src ]:
80- consolidated [norm_src ]. append ( norm_tgt )
87+ norm_tgt , _ = normalize (tgt )
88+ if norm_tgt not in consolidated [norm_src ][ "targets" ] :
89+ consolidated [norm_src ][ "targets" ][ norm_tgt ] = True
8190
8291 results = []
83- for src , tgts in consolidated .items ():
84- tgt_list = []
85- for t in tgts :
86- tgt_list .append (t )
87- entry = {}
88- entry [src ] = tgt_list
92+ for src , data in consolidated .items ():
93+ tgt_list = list (data ["targets" ].keys ())
94+ if data ["extra_tags" ]:
95+ extra_combined = "" .join (data ["extra_tags" ])
96+ entry = {
97+ src : tgt_list ,
98+ "extra-tags" : [extra_combined ]
99+ }
100+ else :
101+ entry = {
102+ src : tgt_list ,
103+ "extra-tags" : []
104+ }
89105 results .append (entry )
90106
91107 self .send_response ({
@@ -102,6 +118,6 @@ def normalize(form):
102118 @gen .coroutine
103119 def get (self ):
104120 pair = self .get_pair_or_error (self .get_argument ('langpair' ))
105-
121+
106122 if pair is not None :
107123 yield self .lookup_and_respond (pair , self .get_argument ('q' ))
0 commit comments