44import pathlib
55
66from collections import defaultdict
7- from typing import DefaultDict , Dict , Optional , Union
7+ from typing import DefaultDict , Dict , Optional , Sequence , Union
88
99from .utils import astype
1010
@@ -51,22 +51,37 @@ def make_entry(
5151 expressions = astype (expressions , list )
5252 entry : DefaultDict [str , Dict [str , str ]] = defaultdict (dict )
5353 entry [nickname ][attr ] = "|" .join (expressions )
54- self .__add__ (entry )
54+ self .__iadd__ (entry )
5555
56- def __add__ (self , other_vocab : Union [DefaultDict [str , Dict [str , str ]], "Vocab" ]):
56+ def add (
57+ self , other_vocab : Union [DefaultDict [str , Dict [str , str ]], "Vocab" ], method : str
58+ ) -> "Vocab" :
5759 """Add two Vocab objects together...
5860
5961 by adding their `.vocab`s together. Expressions are piped together but otherwise not changed.
62+ This is used for both `__add__` and `__iadd__`.
6063
6164 Parameters
6265 ----------
6366 other_vocab: Vocab
6467 Other Vocab object to combine with.
68+ method : str
69+ Whether to run as "add" which returns a new Vocab object or "iadd" which adds to the original object.
70+
71+ Returns
72+ -------
73+ Vocab
74+ vocab + other_vocab either as a new object or in place.
6575 """
6676
6777 if isinstance (other_vocab , Vocab ):
6878 other_vocab = other_vocab .vocab
6979
80+ if method == "add" :
81+ output = Vocab ()
82+ elif method == "iadd" :
83+ output = self
84+
7085 nicknames = set (list (self .vocab .keys ()) + list (other_vocab .keys ()))
7186 for nickname in nicknames :
7287
@@ -82,8 +97,22 @@ def __add__(self, other_vocab: Union[DefaultDict[str, Dict[str, str]], "Vocab"])
8297 + "|"
8398 + other_vocab [nickname ].get (attribute , "" )
8499 ).strip ("|" )
85- self .vocab [nickname ][attribute ] = new_expressions
86- return self
100+ output .vocab [nickname ][attribute ] = new_expressions
101+ return output
102+
103+ def __add__ (self , other_vocab : Union [DefaultDict [str , Dict [str , str ]], "Vocab" ]):
104+ """vocab1 + vocab2"""
105+ return self .add (other_vocab , "add" )
106+
107+ def __iadd__ (self , other_vocab : Union [DefaultDict [str , Dict [str , str ]], "Vocab" ]):
108+ """vocab1 += vocab2"""
109+ return self .add (other_vocab , "iadd" )
110+
111+ def __radd__ (
112+ self , other_vocab : Union [DefaultDict [str , Dict [str , str ]], "Vocab" ]
113+ ) -> "Vocab" :
114+ """right add?"""
115+ return self .__add__ (other_vocab )
87116
88117 def save (self , savename : Union [str , pathlib .PurePath ]):
89118 """Save to file.
@@ -108,3 +137,23 @@ def open_file(self, openname: Union[str, pathlib.PurePath]):
108137 return json .loads (
109138 open (pathlib .PurePath (openname ).with_suffix (".json" ), "r" ).read ()
110139 )
140+
141+
142+ def merge (vocabs : Sequence [Vocab ]) -> Vocab :
143+ """Add together multiple Vocab objects.
144+
145+ Parameters
146+ ----------
147+ vocabs : Sequence[Vocab]
148+ Sequence of Vocab objects to merge.
149+
150+ Returns
151+ -------
152+ Vocab
153+ Single Vocab object made up of input vocabs.
154+ """
155+
156+ final_vocab = Vocab ()
157+ for vocab in vocabs :
158+ final_vocab += vocab
159+ return final_vocab
0 commit comments