0

I am working on NLP in python and I have converted audio files to text and then found time offsets against each word spoken in speech, and then stored words in wordlist plus the time in timelist.

I have three lists, 1st list named strlist, 2nd named wordlist, 3rd named timelist strlist contains phrase lets say

strlist = ["in", "the", "family"]

wordlist contains paragraph or lets say sentences

wordlist = ["there", "are", "few", "things", "to", " be", "in", "the", "family", "means"]

timelist contains some time values against each word stores in wordlist lets suppose

timelist=[2,3,4,5,7,4,8,9,5,3]

I want to know if strlist's phrase (consists of few words) is present in wordlist or not. If it is present then I want to check the time values stores in timelist against those words.

 from pathlib import Path
  import io
 from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file('proven- 
mystery-310205-f04fb2ab3d69.json')
str='in my family'
strlist = list(str.split(" "))
timelist=[]
wordlist=[]
strlist.append("")
for i in strlist:
  print(i)
speech_file = Path("C:/Users/Tani/PycharmProjects/pythonProject/t.wav")
print("Start")

from google.cloud import speech_v1 as speech

print("checking credentials")

client = speech.SpeechClient(credentials=credentials)

print("Checked")
with io.open(speech_file, 'rb') as audio_file:
    content = audio_file.read()

print("audio file read")

audio = speech.RecognitionAudio(content=content)

print("config start")
config = speech.RecognitionConfig(
     encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
     language_code='en-US',
     audio_channel_count=2,
     enable_separate_recognition_per_channel=True,
     enable_word_time_offsets=True)
 print("Recognizing:")
 response = client.recognize(config=config,audio=audio)

 print("Recognized")

 for result in response.results:
     alternative = result.alternatives[0]
     #print('Transcript: {}'.format(alternative.transcript))

  for word_info in alternative.words:
       word = word_info.word
       start_time = word_info.start_time
       end_time = word_info.end_time
       wordlist.append(word)
       timelist.append(start_time.seconds)
 print(str)
 for a, b in zip(wordlist,timelist):
      print('Word: {}, time: {}'.format(
      a,
      b))
 print("findout time")


 for s in strlist:
    if s in wordlist:
       position = wordlist.index(s)
       time_s = timelist[position]
       print(f"Word: '{s}', Time: {time_s}")

1 Answer 1

0

I have a code that does the job. It can be improved, of course:

strlist = ["in", "the", "family"]
wordlist = ["there", "are", "few", "things", "to", " be", "in", "the", "family", "means"]
timelist=[2,3,4,5,7,4,8,9,5,3]

for s in strlist:
    if s in wordlist:
        position = wordlist.index(s)
        time_s = timelist[position]
        print(f"Word: '{s}', Time: {time_s}")

And the output is:

Word: 'in', Time: 8
Word: 'the', Time: 9
Word: 'family', Time: 5

There's another code that produces the same results, but it works only if you don’t have repeated words:

strlist = ["in", "the", "family"]
wordlist = ["there", "are", "few", "things", "to", " be", "in", "the", "family", "means"]
timelist=[2,3,4,5,7,4,8,9,5,3]

map = {word: time for word, time in zip(wordlist, timelist)}
for s in strlist:
    print(f"Word: '{s}', Time: {map[s]}")

Feel free to test both.

11
  • the answer you have provided is not working for me properly as it doesn't provides correct time Commented Apr 21, 2021 at 23:42
  • This code is returning 8, 9 and 5 for the words "in", "the", and "family". I'm not seeing an error. The code is working right, unless your lists are wrong. I edited the print line to show the information more clearly.
    – wensiso
    Commented Apr 22, 2021 at 1:20
  • as i told in description that i am supposing these lists they are not real, in real words can be repeated in the lists.For example on 2 index if there is word "in" on it will give time of that "in" rather that of on index 6 which is wrong Commented Apr 22, 2021 at 6:24
  • can you please explain 2nd code which you have provide i am not understanding it that how its working Commented Apr 22, 2021 at 6:53
  • You have to provide a coherent mapping rule between the wordlist and timelist. Your description implicitly says that if "in" is at index 6 in wordlist, then we have to take index 6 in timelist. That's make sense. If it's not true, you have to provide a different mapping rule in description, otherwise it'll be impossible to solve the problem. What should be the real time for the word "in" in your example?
    – wensiso
    Commented Apr 22, 2021 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.