Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
magicseteditor
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
magicseteditor
Commits
c70f5032
Commit
c70f5032
authored
Feb 22, 2011
by
twanvl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(svn newline style)
parent
5171481f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
131 additions
and
131 deletions
+131
-131
src/util/spell_checker.cpp
src/util/spell_checker.cpp
+131
-131
No files found.
src/util/spell_checker.cpp
View file @
c70f5032
//+----------------------------------------------------------------------------+
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2010 Twan van Laarhoven and Sean Hunt |
//| Copyright: (C) 2001 - 2010 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/prec.hpp>
#include <util/spell_checker.hpp>
#include <util/spell_checker.hpp>
#include <util/string.hpp>
#include <util/string.hpp>
#include <util/io/package_manager.hpp>
#include <util/io/package_manager.hpp>
// ----------------------------------------------------------------------------- : Spell checker : construction
// ----------------------------------------------------------------------------- : Spell checker : construction
map
<
String
,
SpellCheckerP
>
SpellChecker
::
spellers
;
map
<
String
,
SpellCheckerP
>
SpellChecker
::
spellers
;
SpellChecker
&
SpellChecker
::
get
(
const
String
&
language
)
{
SpellChecker
&
SpellChecker
::
get
(
const
String
&
language
)
{
SpellCheckerP
&
speller
=
spellers
[
language
];
SpellCheckerP
&
speller
=
spellers
[
language
];
if
(
!
speller
)
{
if
(
!
speller
)
{
String
local_dir
=
package_manager
.
getDictionaryDir
(
true
);
String
local_dir
=
package_manager
.
getDictionaryDir
(
true
);
String
global_dir
=
package_manager
.
getDictionaryDir
(
false
);
String
global_dir
=
package_manager
.
getDictionaryDir
(
false
);
String
aff_path
=
language
+
_
(
".aff"
);
String
aff_path
=
language
+
_
(
".aff"
);
String
dic_path
=
language
+
_
(
".dic"
);
String
dic_path
=
language
+
_
(
".dic"
);
if
(
wxFileExists
(
local_dir
+
aff_path
)
&&
wxFileExists
(
local_dir
+
dic_path
))
{
if
(
wxFileExists
(
local_dir
+
aff_path
)
&&
wxFileExists
(
local_dir
+
dic_path
))
{
speller
=
SpellCheckerP
(
new
SpellChecker
((
local_dir
+
aff_path
).
mb_str
(),
speller
=
SpellCheckerP
(
new
SpellChecker
((
local_dir
+
aff_path
).
mb_str
(),
(
local_dir
+
dic_path
).
mb_str
()));
(
local_dir
+
dic_path
).
mb_str
()));
}
else
if
(
wxFileExists
(
global_dir
+
aff_path
)
&&
wxFileExists
(
global_dir
+
dic_path
))
{
}
else
if
(
wxFileExists
(
global_dir
+
aff_path
)
&&
wxFileExists
(
global_dir
+
dic_path
))
{
speller
=
SpellCheckerP
(
new
SpellChecker
((
global_dir
+
aff_path
).
mb_str
(),
speller
=
SpellCheckerP
(
new
SpellChecker
((
global_dir
+
aff_path
).
mb_str
(),
(
global_dir
+
dic_path
).
mb_str
()));
(
global_dir
+
dic_path
).
mb_str
()));
}
else
{
}
else
{
throw
Error
(
_
(
"Dictionary not found for language: "
)
+
language
);
throw
Error
(
_
(
"Dictionary not found for language: "
)
+
language
);
}
}
}
}
return
*
speller
;
return
*
speller
;
}
}
SpellChecker
&
SpellChecker
::
get
(
const
String
&
filename
,
const
String
&
language
)
{
SpellChecker
&
SpellChecker
::
get
(
const
String
&
filename
,
const
String
&
language
)
{
SpellCheckerP
&
speller
=
spellers
[
filename
+
_
(
"."
)
+
language
];
SpellCheckerP
&
speller
=
spellers
[
filename
+
_
(
"."
)
+
language
];
if
(
!
speller
)
{
if
(
!
speller
)
{
Packaged
*
package
=
nullptr
;
Packaged
*
package
=
nullptr
;
String
prefix
=
package_manager
.
openFilenameFromPackage
(
package
,
filename
)
+
_
(
"."
);
String
prefix
=
package_manager
.
openFilenameFromPackage
(
package
,
filename
)
+
_
(
"."
);
String
local_dir
=
package_manager
.
getDictionaryDir
(
true
);
String
local_dir
=
package_manager
.
getDictionaryDir
(
true
);
String
global_dir
=
package_manager
.
getDictionaryDir
(
false
);
String
global_dir
=
package_manager
.
getDictionaryDir
(
false
);
String
aff_path
=
language
+
_
(
".aff"
);
String
aff_path
=
language
+
_
(
".aff"
);
String
dic_path
=
language
+
_
(
".dic"
);
String
dic_path
=
language
+
_
(
".dic"
);
if
(
wxFileExists
(
prefix
+
aff_path
)
&&
wxFileExists
(
prefix
+
dic_path
))
{
if
(
wxFileExists
(
prefix
+
aff_path
)
&&
wxFileExists
(
prefix
+
dic_path
))
{
speller
=
SpellCheckerP
(
new
SpellChecker
((
prefix
+
aff_path
).
mb_str
(),
speller
=
SpellCheckerP
(
new
SpellChecker
((
prefix
+
aff_path
).
mb_str
(),
(
prefix
+
dic_path
).
mb_str
()));
(
prefix
+
dic_path
).
mb_str
()));
}
else
if
(
wxFileExists
(
local_dir
+
aff_path
)
&&
wxFileExists
(
prefix
+
dic_path
))
{
}
else
if
(
wxFileExists
(
local_dir
+
aff_path
)
&&
wxFileExists
(
prefix
+
dic_path
))
{
speller
=
SpellCheckerP
(
new
SpellChecker
((
local_dir
+
aff_path
).
mb_str
(),
speller
=
SpellCheckerP
(
new
SpellChecker
((
local_dir
+
aff_path
).
mb_str
(),
(
prefix
+
dic_path
).
mb_str
()));
(
prefix
+
dic_path
).
mb_str
()));
}
else
if
(
wxFileExists
(
global_dir
+
aff_path
)
&&
wxFileExists
(
prefix
+
dic_path
))
{
}
else
if
(
wxFileExists
(
global_dir
+
aff_path
)
&&
wxFileExists
(
prefix
+
dic_path
))
{
speller
=
SpellCheckerP
(
new
SpellChecker
((
global_dir
+
aff_path
).
mb_str
(),
speller
=
SpellCheckerP
(
new
SpellChecker
((
global_dir
+
aff_path
).
mb_str
(),
(
prefix
+
dic_path
).
mb_str
()));
(
prefix
+
dic_path
).
mb_str
()));
}
else
{
}
else
{
throw
Error
(
_
(
"Dictionary '"
)
+
filename
+
_
(
"' not found for language: "
)
+
language
);
throw
Error
(
_
(
"Dictionary '"
)
+
filename
+
_
(
"' not found for language: "
)
+
language
);
}
}
}
}
return
*
speller
;
return
*
speller
;
}
}
SpellChecker
::
SpellChecker
(
const
char
*
aff_path
,
const
char
*
dic_path
)
SpellChecker
::
SpellChecker
(
const
char
*
aff_path
,
const
char
*
dic_path
)
:
Hunspell
(
aff_path
,
dic_path
)
:
Hunspell
(
aff_path
,
dic_path
)
,
encoding
(
String
(
get_dic_encoding
(),
IF_UNICODE
(
wxConvLibc
,
wxSTRING_MAXLEN
)))
,
encoding
(
String
(
get_dic_encoding
(),
IF_UNICODE
(
wxConvLibc
,
wxSTRING_MAXLEN
)))
{}
{}
void
SpellChecker
::
destroyAll
()
{
void
SpellChecker
::
destroyAll
()
{
spellers
.
clear
();
spellers
.
clear
();
}
}
// ----------------------------------------------------------------------------- : Spell checker : use
// ----------------------------------------------------------------------------- : Spell checker : use
bool
SpellChecker
::
convert_encoding
(
const
String
&
word
,
CharBuffer
&
out
)
{
bool
SpellChecker
::
convert_encoding
(
const
String
&
word
,
CharBuffer
&
out
)
{
// fix curly quotes, especially apstrophes
// fix curly quotes, especially apstrophes
String
fixed
;
String
fixed
;
FOR_EACH_CONST
(
c
,
word
)
{
FOR_EACH_CONST
(
c
,
word
)
{
if
(
c
==
LEFT_SINGLE_QUOTE
||
c
==
RIGHT_SINGLE_QUOTE
)
{
if
(
c
==
LEFT_SINGLE_QUOTE
||
c
==
RIGHT_SINGLE_QUOTE
)
{
fixed
+=
_
(
'\''
);
fixed
+=
_
(
'\''
);
}
else
if
(
c
==
LEFT_DOUBLE_QUOTE
||
c
==
RIGHT_DOUBLE_QUOTE
)
{
}
else
if
(
c
==
LEFT_DOUBLE_QUOTE
||
c
==
RIGHT_DOUBLE_QUOTE
)
{
fixed
+=
_
(
'\"'
);
fixed
+=
_
(
'\"'
);
}
else
if
(
c
==
0x00C6
)
{
}
else
if
(
c
==
0x00C6
)
{
// expand ligatures, TODO: put this in a better place
// expand ligatures, TODO: put this in a better place
fixed
+=
_
(
"Ae"
);
fixed
+=
_
(
"Ae"
);
}
else
if
(
c
==
0x0132
)
{
}
else
if
(
c
==
0x0132
)
{
fixed
+=
_
(
"IJ"
);
fixed
+=
_
(
"IJ"
);
}
else
if
(
c
==
0x0152
)
{
}
else
if
(
c
==
0x0152
)
{
fixed
+=
_
(
"Oe"
);
fixed
+=
_
(
"Oe"
);
}
else
if
(
c
==
0xFB01
)
{
}
else
if
(
c
==
0xFB01
)
{
fixed
+=
_
(
"fi"
);
fixed
+=
_
(
"fi"
);
}
else
if
(
c
==
0xFB02
)
{
}
else
if
(
c
==
0xFB02
)
{
fixed
+=
_
(
"fl"
);
fixed
+=
_
(
"fl"
);
}
else
{
}
else
{
fixed
+=
c
;
fixed
+=
c
;
}
}
}
}
// convert encoding
// convert encoding
out
=
fixed
.
mb_str
(
encoding
);
out
=
fixed
.
mb_str
(
encoding
);
if
(
*
out
==
'\0'
)
{
if
(
*
out
==
'\0'
)
{
// If encoding fails we get an empty string, since the word was not empty this can never happen
// If encoding fails we get an empty string, since the word was not empty this can never happen
// words that can't be encoded are not in the dictionary, so they are wrong.
// words that can't be encoded are not in the dictionary, so they are wrong.
return
false
;
return
false
;
}
else
{
}
else
{
return
true
;
return
true
;
}
}
}
}
bool
SpellChecker
::
spell
(
const
String
&
word
)
{
bool
SpellChecker
::
spell
(
const
String
&
word
)
{
if
(
word
.
empty
())
return
true
;
// empty word is okay
if
(
word
.
empty
())
return
true
;
// empty word is okay
CharBuffer
str
;
CharBuffer
str
;
if
(
!
convert_encoding
(
word
,
str
))
return
false
;
if
(
!
convert_encoding
(
word
,
str
))
return
false
;
return
Hunspell
::
spell
(
str
);
return
Hunspell
::
spell
(
str
);
}
}
bool
SpellChecker
::
spell_with_punctuation
(
const
String
&
word
)
{
bool
SpellChecker
::
spell_with_punctuation
(
const
String
&
word
)
{
size_t
start
=
0
,
end
=
String
::
npos
;
size_t
start
=
0
,
end
=
String
::
npos
;
trim_punctuation
(
word
,
start
,
end
);
trim_punctuation
(
word
,
start
,
end
);
if
(
start
>=
end
)
return
true
;
// just punctuation is wrong
if
(
start
>=
end
)
return
true
;
// just punctuation is wrong
return
spell
(
word
.
substr
(
start
,
end
-
start
));
return
spell
(
word
.
substr
(
start
,
end
-
start
));
}
}
void
SpellChecker
::
suggest
(
const
String
&
word
,
vector
<
String
>&
suggestions_out
)
{
void
SpellChecker
::
suggest
(
const
String
&
word
,
vector
<
String
>&
suggestions_out
)
{
CharBuffer
str
;
CharBuffer
str
;
if
(
!
convert_encoding
(
word
,
str
))
return
;
if
(
!
convert_encoding
(
word
,
str
))
return
;
// call Hunspell
// call Hunspell
char
**
suggestions
;
char
**
suggestions
;
int
num_suggestions
=
Hunspell
::
suggest
(
&
suggestions
,
str
);
int
num_suggestions
=
Hunspell
::
suggest
(
&
suggestions
,
str
);
// copy sugestions
// copy sugestions
for
(
int
i
=
0
;
i
<
num_suggestions
;
++
i
)
{
for
(
int
i
=
0
;
i
<
num_suggestions
;
++
i
)
{
suggestions_out
.
push_back
(
String
(
suggestions
[
i
],
encoding
));
suggestions_out
.
push_back
(
String
(
suggestions
[
i
],
encoding
));
free
(
suggestions
[
i
]);
free
(
suggestions
[
i
]);
}
}
free
(
suggestions
);
free
(
suggestions
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment