nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
stories.cc
Go to the documentation of this file.
1#include "server/stories.h"
2
3#include <algorithm>
4#include <cstddef>
5#include <optional>
6#include <string>
7#include <string_view>
8#include <vector>
9
10#include "absl/strings/ascii.h"
11#include "absl/strings/str_cat.h"
12#include "absl/strings/str_join.h"
13#include "absl/strings/str_split.h"
14#include "protocol/chat.h"
15
16namespace nanommo {
17
18namespace {
19
20constexpr std::string_view kSentenceEnds = ".!?";
21
22// Whether the word ends a sentence, with one of those, or with one of those
23// and the quote that closes what someone says.
24bool EndsSentence(std::string_view word) {
25 if (word.ends_with('"')) {
26 word.remove_suffix(1);
27 }
28 return !word.empty() && kSentenceEnds.contains(word.back());
29}
30
31// Where to cut a word longer than a line. In UTF-8, each byte after a
32// character's first starts with the bits 10, so the cut goes back past those.
33std::size_t Cut(std::string_view word) {
34 std::size_t cut = kMaxChatLength;
35 while (cut > 0 && (static_cast<unsigned char>(word[cut]) & 0xC0) == 0x80) {
36 --cut;
37 }
38 // Nothing but those isn't UTF-8, so any cut will do.
39 return cut > 0 ? cut : kMaxChatLength;
40}
41
42} // namespace
43
44std::optional<std::string> StoryPrompt(std::string_view words) {
45 std::vector<std::string> kept;
46 for (const std::string_view word :
47 absl::StrSplit(words, absl::ByAnyChar(" ,"), absl::SkipEmpty())) {
48 if (kept.size() == kStoryWords) {
49 break;
50 }
51 // The stories it learned from give their words in lowercase.
52 kept.push_back(absl::AsciiStrToLower(word));
53 }
54 if (kept.empty()) {
55 return std::nullopt;
56 }
57 return absl::StrCat("Words: ", absl::StrJoin(kept, ", "), "\nStory:");
58}
59
60std::vector<std::string> StoryLines(std::string_view story) {
61 std::size_t end = story.find_last_of(kSentenceEnds);
62 if (end != std::string_view::npos) {
63 // A sentence can end inside the quotes of what someone says.
64 if (end + 1 < story.size() && story[end + 1] == '"') {
65 ++end;
66 }
67 story = story.substr(0, end + 1);
68 }
69 std::vector<std::string> lines;
70 std::string line;
71 // How much of the line is whole sentences.
72 std::size_t sentences = 0;
73 for (std::string_view word :
74 absl::StrSplit(story, absl::ByAsciiWhitespace(), absl::SkipEmpty())) {
75 while (word.size() > kMaxChatLength) {
76 if (!line.empty()) {
77 lines.push_back(line);
78 line.clear();
79 sentences = 0;
80 }
81 const std::size_t cut = Cut(word);
82 lines.emplace_back(word.substr(0, cut));
83 word.remove_prefix(cut);
84 }
85 while (!line.empty() && line.size() + 1 + word.size() > kMaxChatLength) {
86 const std::size_t cut = sentences > 0 ? sentences : line.size();
87 lines.push_back(line.substr(0, cut));
88 line.erase(0, std::min(cut + 1, line.size()));
89 sentences = 0;
90 }
91 if (!line.empty()) {
92 line += ' ';
93 }
94 line += word;
95 if (EndsSentence(word)) {
96 sentences = line.size();
97 }
98 }
99 if (!line.empty()) {
100 lines.push_back(line);
101 }
102 return lines;
103}
104
105} // namespace nanommo
Definition chat.cc:9
constexpr std::size_t kMaxChatLength
Definition chat.h:13
constexpr std::size_t kStoryWords
How many words mvGPT learned to write a story from.
Definition stories.h:13
std::vector< std::string > StoryLines(std::string_view story)
Definition stories.cc:60
std::optional< std::string > StoryPrompt(std::string_view words)
Definition stories.cc:44