【发布时间】:2018-07-16 18:46:54
【问题描述】:
我正在使用 LocationTextExtractionStrategy 从 PDF 呈现文本。 文本在称为 RenderText 的函数中呈现。 所以我的问题是:一个块可以包含两个以上的单词吗? 例如我们有文本: 'MKL 是一个乐于助人的人' 它可以写成块(最重要的块是粗体): MK
L
是一个人
elpfull
个人
?
以下是我用于分词的代码。 我在将文本(来自 renderText 函数的块)添加到当前行期间进行单词分离。
public class TextLineLocation
{
public float X { get; set; }
public float Y { get; set; }
public float Height { get; set; }
public float Width { get; set; }
private string Text;
private List<char> bannedSings = new List<char>() {' ',',', '.', '/', '|', Convert.ToChar(@"\"), ';', '(', ')', '*', '&', '^', '!','?' };
public void AddText(TextInfo text)
{
Text += text;
foreach (char sign in bannedSings)
{
//creating new word
if (text.textChunk.Text.Contains(sign))
{
string[] splittedText = text.textChunk.Text.Split(sign);
foreach (string val in splittedText)
{
//if its first element, add it to current word
if (splittedText[0] == val)
{
// if its space, just ignore...
if (splittedText[0] == " ")
{
continue;
}
wordList[wordList.Count - 1].Text += val;
wordList[wordList.Count - 1].Width += text.getFontWidth();
wordList[wordList.Count - 1].Height += text.getFontHeight();
}
else
{
//if it isnt a first element, create another word
wordList.Add(new WordLocation(text.textChunk.StartLocation[1], text.textChunk.StartLocation[0], text.getFontWidth(), text.getFontHeight(), val));
//TODO: what if chunk has more than 2 words separated ?
}
}
}
}
else
{
//update last word
wordList[wordList.Count-1].Text += text.textChunk.Text;
wordList[wordList.Count - 1].Width += text.getFontWidth();
wordList[wordList.Count - 1].Height += text.getFontHeight();
}
}
public List<WordLocation> wordList = new List<WordLocation>();
}
【问题讨论】:
-
我试图从here 扩展算法以返回单词位置(X,Y,Width,Height)而不是所有行 - 我已经在返回行中添加了宽度和高度,但我想知道块...它们可以包含什么?
-
感谢您的赞美;)。正如@dirkt 回答的那样,你不能依赖任何东西。一个块可以包含从单个字母到整行(甚至跨多列)的任何内容。甚至可以少于可见字符,例如一个“a”可能由两个块“a”和“^”构建而成。但是,您的示例中的一件事不太可能发生:如果“人”一词作为单个块出现,则该块不太可能包含空格“人”。
-
好的,所以我必须明智地解析它,并希望我的方法适用于大多数 pdf。我现在不太关心国家标志,我的意思是从 pdf 中删除敏感数据,例如:文件编号、价格、姓名和姓氏。当然有些名字可以包含特殊符号,但我认为不是这个时候解决这样的问题。感谢您的回复。