我正在开发一个应用程序,用于将阿拉伯语文本从PDF中提取到字符串变量中,每个单词以相反的顺序出现(而不是专用于解决),有时以正确的顺序出现,但分开的字符(专用于解决)类似于英语字符,但在阿拉伯语中,字符是连接在一起的。任何解决方案:我正在使用visual studio 2017 C#MVC应用程序,在windows 10本地,使用iTextSharp从PDF中读取文本。
private string GetTextFromPDF(string Path)
{
StringBuilder text = new StringBuilder();
using (PdfReader reader = new PdfReader(Path))
{
for (int i = 1; i <= reader.NumberOfPages; i++)
{
text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
}
}
return text.ToString();
}
注意:问题不仅仅是颠倒顺序,如果我手动颠倒字符的顺序(颠倒数组顺序),它将显示分离的文本
您需要mirrorglyphs
,这就是大多数PDF编写器创建阿拉伯语PDF的方式。我已经将Bidi.js移植到C#来解决这个问题:
public class BidiResult
{
public string Text { set; get; }
public bool IsRtl { set; get; }
public BidiResult(string text, bool isRtl)
{
this.Text = text;
this.IsRtl = isRtl;
}
}
/// <summary>
/// Ported from https://github.com/mozilla/pdf.js/blob/master/src/core/bidi.js
/// </summary>
public static class Bidi
{
/// <summary>
/// Character types for symbols from 0000 to 00FF.
/// </summary>
public static string[] BaseTypes = new[] {
"BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "S", "B", "S", "WS",
"B", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN",
"BN", "BN", "B", "B", "B", "S", "WS", "ON", "ON", "ET", "ET", "ET", "ON",
"ON", "ON", "ON", "ON", "ON", "CS", "ON", "CS", "ON", "EN", "EN", "EN",
"EN", "EN", "EN", "EN", "EN", "EN", "EN", "ON", "ON", "ON", "ON", "ON",
"ON", "ON", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "ON", "ON",
"ON", "ON", "ON", "ON", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "ON", "ON", "ON", "ON", "BN", "BN", "BN", "BN", "BN", "BN", "B", "BN",
"BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN",
"BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN", "BN",
"BN", "CS", "ON", "ET", "ET", "ET", "ET", "ON", "ON", "ON", "ON", "L", "ON",
"ON", "ON", "ON", "ON", "ET", "ET", "EN", "EN", "ON", "L", "ON", "ON", "ON",
"EN", "L", "ON", "ON", "ON", "ON", "ON", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "ON", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L", "L",
"L", "L", "L", "ON", "L", "L", "L", "L", "L", "L", "L", "L"
};
/// <summary>
/// Character types for symbols from 0600 to 06FF
/// </summary>
public static string[] ArabicTypes = new[] {
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"CS", "AL", "ON", "ON", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM",
"NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AN", "AN", "AN", "AN", "AN", "AN", "AN", "AN", "AN",
"AN", "ET", "AN", "AN", "AL", "AL", "AL", "NSM", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM",
"NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "NSM", "ON", "NSM",
"NSM", "NSM", "NSM", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL"
};
public static bool IsOdd(int i)
{
return (i & 1) != 0;
}
public static bool IsEven(int i)
{
return (i & 1) == 0;
}
public static int FindUnequal(string[] arr, int start, string value)
{
int j;
var jj = arr.Length;
for (j = start; j < jj; ++j)
{
if (arr[j] != value)
return j;
}
return j;
}
public static void SetValues(string[] arr, int start, int end, string value)
{
for (var j = start; j < end; ++j)
{
arr[j] = value;
}
}
public static char[] ReverseValues(char[] arr, int start, int end)
{
var j = end - 1;
for (var i = start; i < j; ++i, --j)
{
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
public static char MirrorGlyphs(char c)
{
/*
# BidiMirroring-1.txt
0028; 0029 # LEFT PARENTHESIS
0029; 0028 # RIGHT PARENTHESIS
003C; 003E # LESS-THAN SIGN
003E; 003C # GREATER-THAN SIGN
005B; 005D # LEFT SQUARE BRACKET
005D; 005B # RIGHT SQUARE BRACKET
007B; 007D # LEFT CURLY BRACKET
007D; 007B # RIGHT CURLY BRACKET
00AB; 00BB # LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
00BB; 00AB # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
*/
switch (c)
{
case '(':
return ')';
case ')':
return '(';
case '<':
return '>';
case '>':
return '<';
case ']':
return '[';
case '[':
return ']';
case '}':
return '{';
case '{':
return '}';
case '\u00AB':
return '\u00BB';
case '\u00BB':
return '\u00AB';
default:
return c;
}
}
public static BidiResult BidiText(string str, int startLevel)
{
var isLtr = true;
var strLength = str.Length;
if (strLength == 0)
return new BidiResult(str, false);
// get types, fill arrays
var chars = new char[strLength];
var types = new string[strLength];
var oldtypes = new string[strLength];
var numBidi = 0;
for (var i = 0; i < strLength; ++i)
{
chars[i] = str[i];
var charCode = str[i];
string charType = "L";
if (charCode <= 0x00ff)
charType = BaseTypes[charCode];
else if (0x0590 <= charCode && charCode <= 0x05f4)
charType = "R";
else if (0x0600 <= charCode && charCode <= 0x06ff)
charType = ArabicTypes[charCode & 0xff];
else if (0x0700 <= charCode && charCode <= 0x08AC)
charType = "AL";
if (charType == "R" || charType == "AL" || charType == "AN")
numBidi++;
oldtypes[i] = types[i] = charType;
}
// detect the bidi method
// if there are no rtl characters then no bidi needed
// if less than 30% chars are rtl then string is primarily ltr
// if more than 30% chars are rtl then string is primarily rtl
if (numBidi == 0)
{
return new BidiResult(str, true);
}
if (startLevel == -1)
{
if ((strLength / numBidi) < 0.3)
{
startLevel = 0;
}
else
{
isLtr = false;
startLevel = 1;
}
}
var levels = new int[strLength];
for (var i = 0; i < strLength; ++i)
{
levels[i] = startLevel;
}
/*
X1-X10: skip most of this, since we are NOT doing the embeddings.
*/
var e = IsOdd(startLevel) ? "R" : "L";
var sor = e;
var eor = sor;
/*
W1. Examine each non-spacing mark (NSM) in the level run, and change the
type of the NSM to the type of the previous character. If the NSM is at the
start of the level run, it will get the type of sor.
*/
var lastType = sor;
for (var i = 0; i < strLength; ++i)
{
if (types[i] == "NSM")
types[i] = lastType;
else
lastType = types[i];
}
/*
W2. Search backwards from each instance of a European number until the
first strong type (R, L, AL, or sor) is found. If an AL is found, change
the type of the European number to Arabic number.
*/
lastType = sor;
for (var i = 0; i < strLength; ++i)
{
var t = types[i];
if (t == "EN")
types[i] = (lastType == "AL") ? "AN" : "EN";
else if (t == "R" || t == "L" || t == "AL")
lastType = t;
}
/*
W3. Change all ALs to R.
*/
for (var i = 0; i < strLength; ++i)
{
var t = types[i];
if (t == "AL")
types[i] = "R";
}
/*
W4. A single European separator between two European numbers changes to a
European number. A single common separator between two numbers of the same
type changes to that type:
*/
for (var i = 1; i < strLength - 1; ++i)
{
if (types[i] == "ES" && types[i - 1] == "EN" && types[i + 1] == "EN")
types[i] = "EN";
if (types[i] == "CS" && (types[i - 1] == "EN" || types[i - 1] == "AN") &&
types[i + 1] == types[i - 1])
types[i] = types[i - 1];
}
/*
W5. A sequence of European terminators adjacent to European numbers changes
to all European numbers:
*/
for (var i = 0; i < strLength; ++i)
{
if (types[i] == "EN")
{
// do before
for (var j = i - 1; j >= 0; --j)
{
if (types[j] != "ET")
break;
types[j] = "EN";
}
// do after
for (var j = i + 1; j < strLength; --j)
{
if (types[j] != "ET")
break;
types[j] = "EN";
}
}
}
/*
W6. Otherwise, separators and terminators change to Other Neutral:
*/
for (var i = 0; i < strLength; ++i)
{
var t = types[i];
if (t == "WS" || t == "ES" || t == "ET" || t == "CS")
types[i] = "ON";
}
/*
W7. Search backwards from each instance of a European number until the
first strong type (R, L, or sor) is found. If an L is found, then change
the type of the European number to L.
*/
lastType = sor;
for (var i = 0; i < strLength; ++i)
{
var t = types[i];
if (t == "EN")
types[i] = (lastType == "L") ? "L" : "EN";
else if (t == "R" || t == "L")
lastType = t;
}
/*
N1. A sequence of neutrals takes the direction of the surrounding strong
text if the text on both sides has the same direction. European and Arabic
numbers are treated as though they were R. Start-of-level-run (sor) and
end-of-level-run (eor) are used at level run boundaries.
*/
for (var i = 0; i < strLength; ++i)
{
if (types[i] == "ON")
{
var end = FindUnequal(types, i + 1, "ON");
var before = sor;
if (i > 0)
before = types[i - 1];
var after = eor;
if (end + 1 < strLength)
after = types[end + 1];
if (before != "L")
before = "R";
if (after != "L")
after = "R";
if (before == after)
SetValues(types, i, end, before);
i = end - 1; // reset to end (-1 so next iteration is ok)
}
}
/*
N2. Any remaining neutrals take the embedding direction.
*/
for (var i = 0; i < strLength; ++i)
{
if (types[i] == "ON")
types[i] = e;
}
/*
I1. For all characters with an even (left-to-right) embedding direction,
those of type R go up one level and those of type AN or EN go up two
levels.
I2. For all characters with an odd (right-to-left) embedding direction,
those of type L, EN or AN go up one level.
*/
for (var i = 0; i < strLength; ++i)
{
var t = types[i];
if (IsEven(levels[i]))
{
if (t == "R")
{
levels[i] += 1;
}
else if (t == "AN" || t == "EN")
{
levels[i] += 2;
}
}
else
{ // isOdd, so
if (t == "L" || t == "AN" || t == "EN")
{
levels[i] += 1;
}
}
}
/*
L1. On each line, reset the embedding level of the following characters to
the paragraph embedding level:
segment separators,
paragraph separators,
any sequence of whitespace characters preceding a segment separator or
paragraph separator, and any sequence of white space characters at the end
of the line.
*/
// don't bother as text is only single line
/*
L2. From the highest level found in the text to the lowest odd level on
each line, reverse any contiguous sequence of characters that are at that
level or higher.
*/
// find highest level & lowest odd level
var highestLevel = -1;
var lowestOddLevel = 99;
var ii = levels.Length;
for (var i = 0; i < ii; ++i)
{
var level = levels[i];
if (highestLevel < level)
highestLevel = level;
if (lowestOddLevel > level && IsOdd(level))
lowestOddLevel = level;
}
// now reverse between those limits
for (var level = highestLevel; level >= lowestOddLevel; --level)
{
// find segments to reverse
var start = -1;
ii = levels.Length;
for (var i = 0; i < ii; ++i)
{
if (levels[i] < level)
{
if (start >= 0)
{
chars = ReverseValues(chars, start, i);
start = -1;
}
}
else if (start < 0)
{
start = i;
}
}
if (start >= 0)
{
chars = ReverseValues(chars, start, levels.Length);
}
}
/*
L3. Combining marks applied to a right-to-left base character will at this
point precede their base character. If the rendering engine expects them to
follow the base characters in the final display process, then the ordering
of the marks and the base character must be reversed.
*/
// don't bother for now
/*
L4. A character that possesses the mirrored property as specified by
Section 4.7, Mirrored, must be depicted by a mirrored glyph if the resolved
directionality of that character is R.
*/
// don't mirror as characters are already mirrored in the pdf
// Finally, return string
var result = string.Empty;
ii = chars.Length;
for (var i = 0; i < ii; ++i)
{
var ch = chars[i];
if (ch != '<' && ch != '>')
result += ch;
}
return new BidiResult(result, isLtr);
}
}
使用方法:
public static string GetTextFromPDF(string path)
{
var reader = new PdfReader(path);
var text = new StringBuilder();
for (var i = 1; i <= reader.NumberOfPages; i++)
{
var data = PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy());
text.Append(Bidi.BidiText(data, 1).Text);
}
return text.ToString();
}
任何帮助都很感激..谢谢..!!!
问题内容: 您能帮我找到一个包含短语列表的正则表达式,请检查给定文本中是否存在这些短语之一? 例: 如果我有以下话: 给定的文本是: 我想在执行正则表达式后得到: 我的初始代码: 问题答案: 是字符类,字符类只能匹配它指定的一个字符。例如,像字符类只能匹配OR OR 。因此,如果您只想查找单词,请不要将其括起来。 另一个问题是您用作单词分隔符,因此在下面的字符串中 正则表达式将无法首先匹配,因为
问题内容: 我正在运行Windows 7,并使用python脚本将一些阿拉伯语字符串输出到Windows cmd。这是我得到的: 如何配置cmd正确显示阿拉伯字符串? 问题答案: 将您的代码页更改为要输出的编码: 这是Windows的阿拉伯语代码页如果您要输出其他编码,则只需在代码页的整个列表中找到它们,然后更改为命令中所需的标识符即可。 提示:UTF-8是65001 注意:要显示以外的其他字符,
Adobe 亚洲语言书写器 借助 Adobe 亚洲语言书写器,您可以使用中东和南亚语言创建内容。您可以键入或混合键入阿拉伯语、希伯来语、英语和其他语言。 您可以在“段落”面板菜单(窗口 > 段落 > 面板菜单)中的可用书写器之间进行选择。例如,您可以使用中东和南亚语言单行书写器或中东和南亚语言逐行书写器。 有关 Illustrator 中支持的其他亚洲语言的更多信息,请参阅适用于亚洲语言文字的书写
启用中东语言功能 若要在 Photoshop 界面中显示中东文字选项,请执行以下操作: 选取“编辑”>“首选项”>“文字”(Windows) 或“Photoshop”>“首选项”>“文字”(Mac OS)。 在“选取文本引擎选项”部分中,选取“中东”。 单击“确定”,并重新启动 Photoshop。 选取“文字”>“语言选项”>“中东语言功能”。 文本方向 要创建阿拉伯语和希伯来语内容,您可以将默
我正在使用扫描仪读取一个阿拉伯语文件,并将文本文件存储在ArrayList中 我有一本字典,里面有一些单词,肯定的和否定的,有thier比率的单词。