Skip to content

Commit

Permalink
added fromdict method to utterance
Browse files Browse the repository at this point in the history
  • Loading branch information
Morrizzzzz committed Nov 14, 2023
1 parent 64442ca commit d5b32ee
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sktalk/corpus/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def from_json(cls, path):
Conversation: A Conversation object representing the conversation in the file.
"""
with open(path, encoding='utf-8') as f:
json_in = json.load(f)
utterances = [Utterance(**u) for u in json_in["Utterances"]]
json_in = json.load(f)
utterances = [Utterance.fromdict(u) for u in json_in["Utterances"]]
del json_in["Utterances"]
return Conversation(utterances, metadata=json_in)

Expand Down
6 changes: 3 additions & 3 deletions sktalk/corpus/corpus.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
from .conversation import Conversation
from .parsing.xml import XmlFile
from .write.writer import Writer
from .utterance import Utterance
from .write.writer import Writer


class Corpus(Writer):
Expand Down Expand Up @@ -70,13 +70,13 @@ def from_json(cls, path):
"""
with open(path, encoding='utf-8') as f:
json_in = json.load(f)
conversations_lst = []
conversations_lst = []
for con in json_in["Conversations"]:
utterances = [Utterance(**u) for u in con["Utterances"]]
del con["Utterances"]
conversations_lst.append(Conversation(utterances, metadata=con))
return Corpus(conversations=conversations_lst)

@classmethod
def from_xml(cls, path):
return XmlFile(path).parse()
4 changes: 4 additions & 0 deletions sktalk/corpus/utterance.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ def get_audio(self):
def asdict(self):
return asdict(self)

@classmethod
def fromdict(cls, fields):
return Utterance(**fields)

# TODO function: that prints summary of data, shows it to user
# TODO function: create a pandas data frame with the utterances
2 changes: 1 addition & 1 deletion tests/corpus/test_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ def test_from_jsonfile(self):
json_in = Corpus.from_json("tests/testdata/dummy_corpus.json")
assert isinstance(json_in, Corpus)
assert len(json_in.conversations) == 2
#assert json_in.utterances[0].utterance == "Hello world"
# assert json_in.utterances[0].utterance == "Hello world"

0 comments on commit d5b32ee

Please sign in to comment.