我正在尝试将文本解析为我创建的数据模型,特别是来自RPG书籍的怪物。我从PDF中提取了文本,看起来是这样的:
Dire RatFrom age to age, dire rats vary in size. In this age, they're only halfas big as they sometimes get, but they're also twice as vicious.1st level mook [beast ]Initiative: +2Infected bite +5 vs. AC-4 ongoing damageAC 15PD 15 HP 6 (mook)MD 10Mook: Kill one dire rat mook for every 6 damage you deal to the mob.
这是我期望的数据模型,碰巧用C#编写的:
public interface IMonsterData { string Name { get; } int Level { get; set; } string Role { get; set; } string Type { get; set; } int Initiative { get; set; } int ArmorClass { get; set; } int PhysicalDefense { get; set; } int MentalDefense { get; set; } int HealthPoints { get; set; } }
期望的结果看起来像这样:
Name = Dire RatLevel = 1Role = mookType = beastInitiative = 2ArmourClass = 15PhysicalDefense = 15MentalDefense = 10HealthPoints = 6
源文本中的这些怪物分布在书籍的一节中,所以大多数页面没有怪物,然后会有一节包含怪物。有些怪物在一页上,有些怪物被分开,但我认为这并不重要,因为我已经使用我找到的工具将书中的所有文本解析为字符串。
我使用C#和.Net手动解析来解决这个问题,通过查找只包含“st, nd, rd, th”的行,并在这些行之间查找AC、PD,但我感觉这是一个使用文本挖掘或其他形式的机器学习解决的好案例。
问题是我对机器学习一无所知,也不知道文本挖掘中应使用的正确算法,而且我只能通过谷歌搜索到一定程度,因为我不知道任何关键词。
我应该使用什么样的库、算法或类似的东西来实现我的目标?
回答:
这看起来是一个可以用老式的正则表达式解决的案例。我会这样处理:
- 从你已经抓取的内容中过滤掉无用的记录(那些不包含相关字段的记录)。
- 将每个相关的记录展平成一行,以便于使用正则表达式查询进行解析。
- 尝试使用这样的正则表达式查询构建器来提取字段并将它们放入你的模型中。