# Copyright 2021-2024 Cambridge Quantum Computing Ltd.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.from__future__importannotationsimportloggingfrommathimportfloorimportpicklefromtypingimportAny,List,TYPE_CHECKING,UnionimportspacyifTYPE_CHECKING:importspacy.cliTokenisedSentenceType=List[str]SentenceType=Union[str,TokenisedSentenceType]TokenisedSentenceBatchType=List[TokenisedSentenceType]SentenceBatchType=Union[List[str],TokenisedSentenceBatchType]
[docs]defnormalise_duration(duration_secs:float|None)->str:"""Normalise a duration value in seconds into a more human-readable form. >>> normalise_duration(4890.0) '1h21m30s' >>> normalise_duration(65.0) '1m5s' >>> normalise_duration(0.29182375) '0.29s' >>> normalise_duration(0.29682375) '0.30s' >>> normalise_duration(None) 'None' Parameters ---------- duration_secs : float The duration value in seconds. """ifduration_secsisNone:return'None'seconds_in_day=24*60*60seconds_in_hour=60*60seconds_in_min=60days=floor(duration_secs/seconds_in_day)duration_secs-=days*seconds_in_dayhours=floor(duration_secs/seconds_in_hour)duration_secs-=hours*seconds_in_hourminutes=floor(duration_secs/seconds_in_min)secs=duration_secs-minutes*seconds_in_minout=[]ifdays:out.append(f'{days}d')ifhours:out.append(f'{hours}h')ifminutes:out.append(f'{minutes}m')iflen(out):out.append(f'{round(secs):.0f}s')else:out.append(f'{secs:.2f}s')return''.join(out)
[docs]deffast_deepcopy(obj:Any)->Any:"""Fast deepcopy (faster than `copy.deepcopy`)."""returnpickle.loads(pickle.dumps(obj))
[docs]defget_spacy_tokeniser(model:str='en_core_web_sm')->spacy.language.Language:try:returnspacy.load(model)exceptOSError:logger=logging.getLogger(__name__)logger.warning('Downloading SpaCy tokeniser. ''This action only has to happen once.')spacy.cli.download(model)returnspacy.load(model)