Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/utility/word_count.cpp
blob: dcc60c598bb66e381dcc8f109e7a76d4690ac207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "StarFile.hpp"
#include "StarLexicalCast.hpp"
#include "StarImage.hpp"
#include "StarRootLoader.hpp"
#include "StarAssets.hpp"
#include "StarItemDatabase.hpp"
#include "StarJson.hpp"

using namespace Star;

int main(int argc, char** argv) {
  try {
    RootLoader rootLoader({{}, {}, {}, LogLevel::Error, false, {}});

    rootLoader.setSummary("Calculate a (very approximate) word count of user-facing text in assets");

    RootUPtr root;
    OptionParser::Options options;
    tie(root, options) = rootLoader.commandInitOrDie(argc, argv);

    StringMap<int> wordCounts;
    auto assets = Root::singleton().assets();

    auto countWordsInType = [&](String const& type, function<int(Json const&)> countFunction, Maybe<function<bool(String const&)>> filterFunction = {}, Maybe<String> wordCountKey = {}) {
      auto files = assets->scanExtension(type).values();
      if (filterFunction)
        files.filter(*filterFunction);
      assets->queueJsons(files);
      for (auto& path : files) {
        auto json = assets->json(path);
        if (json.isNull())
          continue;

        String countKey = wordCountKey ? *wordCountKey : strf(".{} files", type);
        wordCounts[countKey] += countFunction(json);
      }
    };

    StringList itemFileTypes = {
        "tech",
        "item",
        "liqitem",
        "matitem",
        "miningtool",
        "flashlight",
        "wiretool",
        "beamaxe",
        "tillingtool",
        "painttool",
        "harvestingtool",
        "head",
        "chest",
        "legs",
        "back",
        "currencyitem",
        "consumable",
        "blueprint",
        "inspectiontool",
        "instrument",
        "thrownitem",
        "unlock",
        "activeitem",
        "augment" };

    for (auto& itemFileType : itemFileTypes) {
      countWordsInType(itemFileType, [](Json const& json) {
          int wordCount = 0;
          wordCount += json.getString("shortdescription", "").split(" ").count();
          wordCount += json.getString("description", "").split(" ").count();
          return wordCount;
        });
    }

    countWordsInType("object", [](Json const& json) {
        int wordCount = 0;
        wordCount += json.getString("shortdescription", "").split(" ").count();
        wordCount += json.getString("description", "").split(" ").count();
        wordCount += json.getString("apexDescription", "").split(" ").count();
        wordCount += json.getString("avianDescription", "").split(" ").count();
        wordCount += json.getString("glitchDescription", "").split(" ").count();
        wordCount += json.getString("floranDescription", "").split(" ").count();
        wordCount += json.getString("humanDescription", "").split(" ").count();
        wordCount += json.getString("hylotlDescription", "").split(" ").count();
        wordCount += json.getString("novakidDescription", "").split(" ").count();
        return wordCount;
      });

    countWordsInType("codex", [](Json const& json) {
        int wordCount = 0;
        wordCount += json.getString("title", "").split(" ").count();
        wordCount += json.getString("description", "").split(" ").count();
        for (auto contentPage : json.getArray("contentPages", JsonArray()))
          wordCount += contentPage.toString().split(" ").count();
        return wordCount;
      });

    countWordsInType("monstertype", [](Json const& json) {
        return json.getString("description", "").split(" ").count();
      });

    countWordsInType("radiomessages", [](Json const& json) {
        auto wordCount = 0;
        for (auto messageConfigPair : json.iterateObject())
          wordCount += messageConfigPair.second.getString("text", "").split(" ").count();
        return wordCount;
      });

    function<int(Json const& json)> countOnlyStrings;
    countOnlyStrings = [&](Json const& json) {
      int wordCount = 0;
      if (json.isType(Json::Type::Object)) {
        for (auto entry : json.iterateObject())
          wordCount += countOnlyStrings(entry.second);
      } else if (json.isType(Json::Type::Array)) {
        for (auto entry : json.iterateArray())
          wordCount += countOnlyStrings(entry);
      } else if (json.isType(Json::Type::String)) {
        if (!json.toString().beginsWith("/")) {
          wordCount += json.toString().split(" ").count();
        }
      }
      return wordCount;
    };

    function<bool(String const&)> dialogFilter = [](String const& filePath) { return filePath.beginsWith("/dialog/"); };
    countWordsInType("config", countOnlyStrings, dialogFilter, String("NPC dialog (.config files)"));

    countWordsInType("npctype", [&](Json const& json) {
        if (auto scriptConfig = json.get("scriptConfig", Json()))
          return countOnlyStrings(scriptConfig.get("dialog", Json()));
        return 0;
      }, {}, String("NPC dialog (.npctype files)"));

    countWordsInType("questtemplate", [&](Json const& json) {
        int wordCount = 0;
        wordCount += json.getString("title", "").split(" ").count();
        wordCount += json.getString("text", "").split(" ").count();
        wordCount += json.getString("completionText", "").split(" ").count();
        if (auto scriptConfig = json.get("scriptConfig", Json()))
          wordCount += countOnlyStrings(scriptConfig.get("generatedText", Json()));
        return wordCount;
      });

    countWordsInType("collection", [&](Json const& json) {
        int wordCount = 0;
        for (auto entry : json.get("collectables", Json()).iterateObject())
          wordCount += entry.second.getString("description", "").split(" ").count();
        return wordCount;
      });

    countWordsInType("cinematic", [&](Json const& json) {
        int wordCount = 0;
        for (auto panel : json.get("panels", Json()).iterateArray()) {
          auto panelText = panel.optString("text");
          // filter on pipes to ignore those long lists of backer names in the credits
          if (panelText && !panelText->contains("|"))
            wordCount += panelText->split(" ").count();
        }
        return wordCount;
      });

    countWordsInType("aimission", [&](Json const& json) {
        int wordCount = 0;
        for (auto entry : json.get("speciesText", Json()).iterateObject()) {
          wordCount += entry.second.getString("buttonText", "").split(" ").count();
          wordCount += entry.second.getString("repeatButtonText", "").split(" ").count();
          if (auto selectSpeech = entry.second.get("selectSpeech"))
            wordCount += selectSpeech.getString("text", "").split(" ").count();
        }
        return wordCount;
      });

    auto cockpitConfig = assets->json("/interface/cockpit/cockpit.config");
    int cockpitWordCount = 0;
    cockpitWordCount += countOnlyStrings(cockpitConfig.get("visitableTypeDescription"));
    cockpitWordCount += countOnlyStrings(cockpitConfig.get("worldTypeDescription"));
    wordCounts["planet descriptions (cockpit.config)"] = cockpitWordCount;

    int totalWordCount = 0;
    for (auto countPair : wordCounts) {
      coutf("{} words in {}\n", countPair.second, countPair.first);
      totalWordCount += countPair.second;
    }
    coutf("approximately {} words total\n", totalWordCount);

    return 0;
  } catch (std::exception const& e) {
    cerrf("exception caught: {}\n", outputException(e, true));
    return 1;
  }
}