def _convert(self, audio): #import speech_recognition as sr with audio.get_filename() as filename: with sr.AudioFile(filename) as source: clip = self.recognizer.record(source) _json = self._query_api(clip) if 'results' in _json: results = _json['results'] else: raise Exception( 'received invalid results from API: {0}'.format(str(_json))) elements = [] for result in results: if result['final'] is True: timestamps = result['alternatives'][0]['timestamps'] if self.resolution is 'words': for entry in timestamps: text = entry[0] start = entry[1] end = entry[2] elements.append(TextStim(text=text, onset=start, duration=end-start)) elif self.resolution is 'phrases': text = result['alternatives'][0]['transcript'] start = timestamps[0][1] end = timestamps[-1][2] elements.append(TextStim(text=text, onset=start, duration=end-start)) return ComplexTextStim(elements=elements)
def _convert(self, stim): request = self._build_request([stim]) responses = self._query_api(request) response = responses[0] if response: annotations = response[self.response_object] # Combine the annotations if self.handle_annotations == 'first': text = annotations[0]['description'] return TextStim(text=text, onset=stim.onset, duration=stim.duration) elif self.handle_annotations == 'concatenate': text = '' for annotation in annotations: text += annotation['description'] return TextStim(text=text, onset=stim.onset, duration=stim.duration) elif self.handle_annotations == 'list': texts = [] for annotation in annotations: texts.append( TextStim(text=annotation['description'], onset=stim.onset, duration=stim.duration)) return texts else: return TextStim(text='', onset=stim.onset, duration=stim.duration)
def _convert(self, stims): request = self._build_request(stims) responses = self._query_api(request) texts = [] for response in responses: if response and self.response_object in response: annotations = response[self.response_object] # Combine the annotations if self.handle_annotations == 'first': text = annotations[0]['description'] texts.append(TextStim(text=text)) elif self.handle_annotations == 'concatenate': text = '' for annotation in annotations: text = ' '.join([text, annotation['description']]) texts.append(TextStim(text=text)) elif self.handle_annotations == 'list': for annotation in annotations: texts.append(TextStim(text=annotation['description'])) elif 'error' in response: raise Exception(response['error']['message']) else: texts.append(TextStim(text='')) return texts
def _filter(self, stim): if self.tokenize: tokens = stim.text.split() stemmed = ' '.join([self.stemmer.stem(tok) for tok in tokens]) else: stemmed = self.stemmer.stem(stim.text) return TextStim(stim.filename, stemmed)
def _filter(self, stim): pos_map = { 'ADJ': 'a', 'ADJ_SAT': 's', 'ADV': 'r', 'NOUN': 'n', 'VERB': 'v' } def pos_wordnet(txt): pos_tagged = dict(nltk.pos_tag(txt, tagset='universal')) pos_tagged = {t: pos_map[tag] if tag in pos_map else 'n' for t, tag in pos_tagged.items()} return pos_tagged tokens = [stim.text] if self.tokenize: tokens = nltk.word_tokenize(tokens[0]) tokens = [t if self.case_sensitive else t.lower() for t in tokens] if not isinstance(self.stemmer, stem.WordNetLemmatizer): stemmed = ' '.join([self.stemmer.stem(t) for t in tokens]) else: pos_tagged = pos_wordnet(tokens) stemmed = ' '.join([self.stemmer.lemmatize(t, pos=pos_tagged[t]) for t in tokens]) return TextStim(stim.filename, stemmed, stim.onset, stim.duration, stim.order, stim.url)
def _convert(self, stim): verify_dependencies(['pytesseract']) text = pytesseract.image_to_string(Image.fromarray(stim.data)) return TextStim(text=text, onset=stim.onset, duration=stim.duration, order=stim.order)
def _filter(self, stim): if self.tokenizer: tokens = self.tokenizer.tokenize(stim.text) else: tokens = word_tokenize(stim.text) stims = [TextStim(stim.filename, token, order=i) for i, token in enumerate(tokens)] return stims
def _filter(self, stim): if self.tokenizer: tokens = self.tokenizer.tokenize(stim.text) else: tokens = word_tokenize(stim.text) stims = [TextStim(stim.filename, token, stim.onset, stim.duration) for token in tokens] return stims
def _filter(self, stim): tokens = word_tokenize(stim.text) tokens = [tok for tok in tokens if tok not in self.tokens] text = ' '.join(tokens) return TextStim(stim.filename, text, onset=stim.onset, duration=stim.duration, order=stim.order)
def _filter(self, stim): if self.tokenize: tokens = stim.text.split() stemmed = ' '.join([self.stemmer.stem(tok) for tok in tokens]) else: stemmed = self.stemmer.stem(stim.text) return TextStim(stim.filename, stemmed, onset=stim.onset, duration=stim.duration, order=stim.order)
def _convert(self, stim): params = {'language': self.language, 'detectOrientation': False} response = self._query_api(stim, params) lines = [] for r in response['regions']: for l in r['lines']: lines.append(' '.join([w['text'] for w in l['words']])) text = '\n'.join(lines) return TextStim(text=text)
def _convert(self, audio): verify_dependencies(['sr']) offset = 0.0 if audio.onset is None else audio.onset with audio.get_filename() as filename: with sr.AudioFile(filename) as source: clip = self.recognizer.record(source) _json = self._query_api(clip) if 'results' in _json: results = _json['results'] else: raise Exception('received invalid results from API: {0}'.format( str(_json))) elements = [] order = 0 for result in results: if result['final'] is True: timestamps = result['alternatives'][0]['timestamps'] if self.resolution is 'words': for entry in timestamps: text = entry[0] start = entry[1] end = entry[2] elements.append( TextStim(text=text, onset=offset + start, duration=end - start, order=order)) order += 1 elif self.resolution is 'phrases': text = result['alternatives'][0]['transcript'] start = timestamps[0][1] end = timestamps[-1][2] elements.append( TextStim(text=text, onset=offset + start, duration=end - start, order=order)) order += 1 return ComplexTextStim(elements=elements, onset=audio.onset)
def _convert(self, audio): import speech_recognition as sr with sr.AudioFile(audio.filename) as source: clip = self.recognizer.record(source) result = self._query_api(clip) timestamps = result['results'][0]['alternatives'][0]['timestamps'] elements = [] for i, entry in enumerate(timestamps): elements.append( TextStim(text=entry[0], onset=entry[1], duration=entry[2] - entry[1])) return ComplexTextStim(elements=elements)
def _convert(self, audio): verify_dependencies(['rev_ai']) msg = "Beginning audio transcription with a timeout of %fs. Even for "\ "small audios, full transcription may take awhile." % self.timeout logging.warning(msg) if audio.url: job = self.client.submit_job_url(audio.url) else: with audio.get_filename() as filename: job = self.client.submit_job_local_file(filename) operation_start = time.time() response = self.client.get_job_details(job.id) while (response.status == rev_ai.JobStatus.IN_PROGRESS) and \ (time.time() - operation_start) < self.timeout: response = self.client.get_job_details(job.id) time.sleep(self.request_rate) if (time.time() - operation_start) >= self.timeout: msg = "Conversion reached the timeout limit of %fs." % self.timeout logging.warning(msg) if response.status == rev_ai.JobStatus.FAILED: raise Exception('API failed: %s' % response.failure_detail) result = self.client.get_transcript_object(job.id) elements = [] order = 0 for m in result.monologues: for e in m.elements: if e.type_ == 'text': start = e.timestamp end = e.end_timestamp elements.append( TextStim(text=e.value, onset=start, duration=end - start, order=order)) order += 1 return ComplexTextStim(elements=elements, onset=audio.onset)
def _convert(self, stim): request = self._build_request(stim) response = self._query_api(request) if 'error' in response: raise Exception(response['error']['message']) words = [] if 'results' in response: for result in response['results']: transcription = result['alternatives'][0] for w in transcription['words']: onset = float(w['startTime'][:-1]) duration = float(w['endTime'][:-1]) - onset words.append(TextStim(text=w['word'], onset=onset, duration=duration)) return ComplexTextStim(elements=words)
def _filter(self, stim): return TextStim(stim.filename, stim.text.lower())
def _filter(self, stim): stemmed = self.stemmer.stem(stim.text) return TextStim(stim.filename, stemmed, stim.onset, stim.duration)
def _convert(self, stim): import pytesseract text = pytesseract.image_to_string(Image.fromarray(stim.data)) return TextStim(text=text, onset=stim.onset, duration=stim.duration)
def _convert(self, stim): verify_dependencies(['pytesseract']) text = pytesseract.image_to_string(Image.fromarray(stim.data)) return TextStim(text=text)
def _filter(self, stim): tokens = word_tokenize(stim.text) tokens = [tok for tok in tokens if tok not in self.tokens] text = ' '.join(tokens) return TextStim(stim.filename, text)
def _filter(self, stim): pattern = '[%s]' % re.escape(string.punctuation) text = re.sub(pattern, '', stim.text) return TextStim(stim.filename, text)