Skip to content

Commit

Permalink
feat: add chinese convert #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony15246 committed Dec 7, 2024
1 parent 5736294 commit 0f3e102
Show file tree
Hide file tree
Showing 525 changed files with 63,716 additions and 7 deletions.
14 changes: 7 additions & 7 deletions api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local utils = require("mp.utils")
local md5 = require("md5")

local danmaku_path = os.getenv("TEMP") or "/tmp/"
local exec_path = mp.command_native({ "expand-path", options.DanmakuFactory_Path })
local danmaku_factory_path = mp.command_native({ "expand-path", options.DanmakuFactory_Path })
local history_path = mp.command_native({"expand-path", options.history_path})
local blacklist_file = mp.command_native({ "expand-path", options.blacklist_path })

Expand Down Expand Up @@ -143,7 +143,7 @@ function itable_index_of(itable, value)
end
end

local platform = (function()
platform = (function()
local platform = mp.get_property_native("platform")
if platform then
if itable_index_of({ "windows", "darwin" }, platform) then
Expand Down Expand Up @@ -791,15 +791,15 @@ end
--将json文件又转换为ass文件。
-- Function to convert JSON file using DanmakuFactory
function convert_with_danmaku_factory(danmaku_input, danmaku_out)
if exec_path == "" then
exec_path = utils.join_path(mp.get_script_directory(), "bin")
if danmaku_factory_path == "" then
danmaku_factory_path = utils.join_path(mp.get_script_directory(), "bin/DanmakuFactory")
if platform == "windows" then
exec_path = utils.join_path(exec_path, "DanmakuFactory.exe")
danmaku_factory_path = utils.join_path(danmaku_factory_path, "DanmakuFactory.exe")
else
exec_path = utils.join_path(exec_path, "DanmakuFactory")
danmaku_factory_path = utils.join_path(danmaku_factory_path, "DanmakuFactory")
end
end
local danmaku_factory_path = os.getenv("DANMAKU_FACTORY") or exec_path
local danmaku_factory_path = os.getenv("DANMAKU_FACTORY") or danmaku_factory_path

local arg = {
danmaku_factory_path,
Expand Down
File renamed without changes.
File renamed without changes.
Binary file added bin/OpenCC_Linux/bin/opencc
Binary file not shown.
Binary file added bin/OpenCC_Linux/bin/opencc_dict
Binary file not shown.
Binary file added bin/OpenCC_Linux/bin/opencc_phrase_extract
Binary file not shown.
53 changes: 53 additions & 0 deletions bin/OpenCC_Linux/include/opencc/BinaryDict.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Open Chinese Convert
*
* Copyright 2010-2014 Carbo Kuo <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Common.hpp"
#include "SerializableDict.hpp"

namespace opencc {
/**
* Binary dictionary for faster deserialization
* @ingroup opencc_cpp_api
*/
class OPENCC_EXPORT BinaryDict : public SerializableDict {
public:
BinaryDict(const LexiconPtr& _lexicon) : lexicon(_lexicon) {}

virtual ~BinaryDict() {}

virtual void SerializeToFile(FILE* fp) const;

static BinaryDictPtr NewFromFile(FILE* fp);

const LexiconPtr& GetLexicon() const { return lexicon; }

size_t KeyMaxLength() const;

private:
LexiconPtr lexicon;
std::string keyBuffer;
std::string valueBuffer;

void ConstructBuffer(std::string& keyBuffer, std::vector<size_t>& keyOffset,
size_t& keyTotalLength, std::string& valueBuffer,
std::vector<size_t>& valueOffset,
size_t& valueTotalLength) const;
};
} // namespace opencc
82 changes: 82 additions & 0 deletions bin/OpenCC_Linux/include/opencc/Common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Open Chinese Convert
*
* Copyright 2010-2014 Carbo Kuo <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

// Microsoft Visual C++ specific
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
#pragma warning(disable : 4251 4266 4350 4503 4512 4514 4710 4820)
#endif

#include <cstddef>
#include <memory>
#include <string>
#include <vector>

#include "Export.hpp"
#include "Optional.hpp"
#include "opencc_config.h"

// Forward decalarations and alias
namespace opencc {
class Config;
class Conversion;
class ConversionChain;
class Converter;
class Dict;
class DictEntry;
class DictGroup;
class Lexicon;
class MarisaDict;
class MultiValueDictEntry;
class NoValueDictEntry;
class Segmentation;
class Segments;
class SerializableDict;
class SingleValueDictEntry;
class TextDict;
typedef std::shared_ptr<Conversion> ConversionPtr;
typedef std::shared_ptr<ConversionChain> ConversionChainPtr;
typedef std::shared_ptr<Converter> ConverterPtr;
typedef std::shared_ptr<Dict> DictPtr;
typedef std::shared_ptr<DictGroup> DictGroupPtr;
typedef std::shared_ptr<Lexicon> LexiconPtr;
typedef std::shared_ptr<MarisaDict> MarisaDictPtr;
typedef std::shared_ptr<Segmentation> SegmentationPtr;
typedef std::shared_ptr<Segments> SegmentsPtr;
typedef std::shared_ptr<SerializableDict> SerializableDictPtr;
typedef std::shared_ptr<TextDict> TextDictPtr;

#ifdef OPENCC_ENABLE_DARTS
class BinaryDict;
class DartsDict;
typedef std::shared_ptr<BinaryDict> BinaryDictPtr;
typedef std::shared_ptr<DartsDict> DartsDictPtr;
#endif

} // namespace opencc

#ifndef PKGDATADIR
const std::string PACKAGE_DATA_DIRECTORY = "";
#else // ifndef PKGDATADIR
const std::string PACKAGE_DATA_DIRECTORY = PKGDATADIR "/";
#endif // ifndef PKGDATADIR

#ifndef VERSION
#define VERSION "1.0.*"
#endif // ifndef VERSION
49 changes: 49 additions & 0 deletions bin/OpenCC_Linux/include/opencc/Config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Open Chinese Convert
*
* Copyright 2010-2014 Carbo Kuo <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Common.hpp"

namespace opencc {
/**
* Configuration loader
* @ingroup opencc_cpp_api
*/
class OPENCC_EXPORT Config {
public:
Config();

virtual ~Config();

ConverterPtr NewFromString(const std::string& json,
const std::string& configDirectory);

ConverterPtr NewFromString(const std::string& json,
const std::vector<std::string>& paths);

ConverterPtr NewFromFile(const std::string& fileName);

ConverterPtr NewFromFile(const std::string& fileName,
const std::vector<std::string>& paths,
const char* argv0);

private:
void* internal;
};
} // namespace opencc
47 changes: 47 additions & 0 deletions bin/OpenCC_Linux/include/opencc/Conversion.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Open Chinese Convert
*
* Copyright 2010-2014 Carbo Kuo <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Common.hpp"
#include "Segmentation.hpp"

namespace opencc {
/**
* Conversion interface
* @ingroup opencc_cpp_api
*/
class OPENCC_EXPORT Conversion {
public:
Conversion(DictPtr _dict) : dict(_dict) {}

// Convert single phrase
std::string Convert(const std::string& phrase) const;

// Convert single phrase
std::string Convert(const char* phrase) const;

// Convert segmented text
SegmentsPtr Convert(const SegmentsPtr& input) const;

const DictPtr GetDict() const { return dict; }

private:
const DictPtr dict;
};
} // namespace opencc
43 changes: 43 additions & 0 deletions bin/OpenCC_Linux/include/opencc/ConversionChain.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Open Chinese Convert
*
* Copyright 2010-2014 Carbo Kuo <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <list>

#include "Common.hpp"
#include "Conversion.hpp"

namespace opencc {
/**
* Chain of conversions
* Consists of a list of conversions. Converts input in sequence.
* @ingroup opencc_cpp_api
*/
class OPENCC_EXPORT ConversionChain {
public:
ConversionChain(const std::list<ConversionPtr> _conversions);

SegmentsPtr Convert(const SegmentsPtr& input) const;

const std::list<ConversionPtr> GetConversions() const { return conversions; }

private:
const std::list<ConversionPtr> conversions;
};
} // namespace opencc
51 changes: 51 additions & 0 deletions bin/OpenCC_Linux/include/opencc/Converter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Open Chinese Convert
*
* Copyright 2010-2014 Carbo Kuo <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Common.hpp"
#include "Segmentation.hpp"

namespace opencc {
/**
* Controller of segmentation and conversion
* @ingroup opencc_cpp_api
*/
class OPENCC_EXPORT Converter {
public:
Converter(const std::string& _name, SegmentationPtr _segmentation,
ConversionChainPtr _conversionChain)
: name(_name), segmentation(_segmentation),
conversionChain(_conversionChain) {}

std::string Convert(const std::string& text) const;

size_t Convert(const char* input, char* output) const;

const SegmentationPtr GetSegmentation() const { return segmentation; }

const ConversionChainPtr GetConversionChain() const {
return conversionChain;
}

private:
const std::string name;
const SegmentationPtr segmentation;
const ConversionChainPtr conversionChain;
};
} // namespace opencc
Loading

0 comments on commit 0f3e102

Please sign in to comment.