summaryrefslogtreecommitdiff
path: root/chimere/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'chimere/utils.py')
-rw-r--r--chimere/utils.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/chimere/utils.py b/chimere/utils.py
index ffa5789..2ed2fd3 100644
--- a/chimere/utils.py
+++ b/chimere/utils.py
@@ -1455,3 +1455,42 @@ class IcalManager(ImportManager):
if created:
new_item += 1
return (new_item, updated_item, msg)
+
+
+def merge_tsvectors(vectors):
+ """
+ Parse tsvector to merge them in one string
+ :param vectors: list of tsvector string
+ :return: merged tsvector
+ """
+ result_dict = {}
+ for vector in vectors:
+ if not vector:
+ continue
+
+ current_position = 0
+ if result_dict:
+ for key in result_dict:
+ max_position = max(result_dict[key])
+ if max_position > current_position:
+ current_position = max_position
+
+ for dct_member in vector.split(" "):
+ key, positions = dct_member.split(':')
+ key = key[1:-1] # remove quotes
+ positions = [int(pos) + current_position
+ for pos in positions.split(',')]
+ if key in result_dict:
+ result_dict[key] += positions
+ else:
+ result_dict[key] = positions
+
+ # {'lamelie': [1, 42, 5]} => {'lamelie': "1,42,5"}
+ result_dict = {k: ",".join([str(val) for val in result_dict[k]])
+ for k in result_dict}
+ # {'lamelie': "1,5", "hagarde": "2", "regarde": "4"} =>
+ # "lamelie':1,5 hagarde:2 regarde:4"
+ result = " ".join(["{}:{}".format(k, result_dict[k]) for k in result_dict])
+
+ return result
+