Я новичок в Python и работаю над проектом с Google Speech to text.Наконец-то разобрался, как импортировать результаты из Google STT (JSON) и форматировать данные в csv.НО ....
Google дает вам альтернативные слова, которые хороши и плохи.Прикрепленный код будет читать только первый вариант и останавливаться, поэтому я могу выбрать только один вариант.
Я хотел бы импортировать другие альтернативы и показать в их собственной колонке это Main, alt1, alt2.Иногда метки времени такие же, как у Main, иногда отличаются.
Советы приветствуются.- Чувствую, что я поняла это медленно.
{
"@type": "type.googleapis.com/google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse",
"timestamp": "2018-12-28 14:13:18",
"results": [
{
"alternatives": [
{
"confidence": 0.9319887,
"words": [
{
"confidence": 0.9572171,
"endTime": "2s",
"startTime": "1s",
"word": "Bla1a"
},
{
"confidence": 0.9572171,
"endTime": "3s",
"startTime": "2s",
"word": "Bla1b"
}
]
}
],
"languageCode": "th-th"
},
{
"alternatives": [
{
"confidence": 0.95174015,
"words": [
{
"confidence": 0.9572171,
"endTime": "2s",
"startTime": "1s",
"word": "Bla2a"
},
{
"confidence": 0.9572171,
"endTime": "3s",
"startTime": "2s",
"word": "Bla2b"
}
]
}
],
"languageCode": "th-th"
},
{
"alternatives": [
{
"confidence": 0.95298487,
"words": [
{
"confidence": 0.9572171,
"endTime": "2s",
"startTime": "1s",
"word": "bla3b"
},
{
"confidence": 0.9572171,
"endTime": "3s",
"startTime": "2s",
"word": "Bla3b"
}
]
}
],
"languageCode": "th-th"
},
{
"alternatives": [
{
"confidence": 0.8774771,
"words": [
{
"confidence": 0.7337543,
"endTime": "3s",
"startTime": "2s",
"word": "Bla4a"
},
{
"confidence": 0.9363319,
"endTime": "4s",
"startTime": "3s",
"word": "bla4b"
}
]
}
],
"languageCode": "th-th"
},
{
"alternatives": [
{
"confidence": 0.9491383,
"words": [
{
"confidence": 0.8349256,
"endTime": "4s",
"startTime": "3s",
"word": "Bla5a"
},
{
"confidence": 0.9572171,
"endTime": "5s",
"speakerTag": 1,
"startTime": "4s",
"word": "Bla5b"
}
]
}
],
"languageCode": "th-th"
}
]
}
#!/usr/bin/python
# note = can only show one alternatives list
import json
import pandas as pd
from pandas import ExcelWriter
import numpy as np
with open('Thai_Unicode(bk).json') as f: # this ensures opening and closing file
a = json.loads(f.read())
data = a["results"][0]["alternatives"][0]["words"]
df = pd.DataFrame(data)
#print(df)
df.to_excel('pandas4.xls')