gpt4 book ai didi

python - 用于解析体育比赛数据的自然语言解析器

转载 作者:太空狗 更新时间:2023-10-29 20:28:41 24 4
gpt4 key购买 nike

我正在尝试为足球比赛设计一个解析器。我在这里非常宽松地使用术语“自然语言”,所以请耐心等待,因为我对这个领域知之甚少。

以下是我正在使用的一些示例(格式:TIME|DOWN&DIST|OFF_TEAM|DESCRIPTION):

04:39|4th and 20@NYJ46|Dal|Mat McBriar punts for 32 yards to NYJ14. Jeremy Kerley - no return. FUMBLE, recovered by NYJ.|
04:31|1st and 10@NYJ16|NYJ|Shonn Greene rush up the middle for 5 yards to the NYJ21. Tackled by Keith Brooking.|
03:53|2nd and 5@NYJ21|NYJ|Mark Sanchez rush to the right for 3 yards to the NYJ24. Tackled by Anthony Spencer. FUMBLE, recovered by NYJ (Matthew Mulligan).|
03:20|1st and 10@NYJ33|NYJ|Shonn Greene rush to the left for 4 yards to the NYJ37. Tackled by Jason Hatcher.|
02:43|2nd and 6@NYJ37|NYJ|Mark Sanchez pass to the left to Shonn Greene for 7 yards to the NYJ44. Tackled by Mike Jenkins.|
02:02|1st and 10@NYJ44|NYJ|Shonn Greene rush to the right for 1 yard to the NYJ45. Tackled by Anthony Spencer.|
01:23|2nd and 9@NYJ45|NYJ|Mark Sanchez pass to the left to LaDainian Tomlinson for 5 yards to the 50. Tackled by Sean Lee.|

到目前为止,我已经编写了一个愚蠢的解析器来处理所有简单的东西(playID、季度、时间、下降和距离、进攻球队)以及一些脚本来获取这些数据并将其清理成上面看到的格式.单行变成“播放”对象以存储到数据库中。

这里最困难的部分(至少对我而言)是解析剧本的描述。这是我想从该字符串中提取的一些信息:

示例字符串:

"Mark Sanchez pass to the left to Shonn Greene for 7 yards to the NYJ44. Tackled by Mike Jenkins."

结果:

turnover = False
interception = False
fumble = False
to_on_downs = False
passing = True
rushing = False
direction = 'left'
loss = False
penalty = False
scored = False
TD = False
PA = False
FG = False
TPC = False
SFTY = False
punt = False
kickoff = False
ret_yardage = 0
yardage_diff = 7
playmakers = ['Mark Sanchez', 'Shonn Greene', 'Mike Jenkins']

我的初始解析器逻辑是这样的:

# pass, rush or kick
# gain or loss of yards
# scoring play
# Who scored? off or def?
# TD, PA, FG, TPC, SFTY?
# first down gained
# punt?
# kick?
# return yards?
# penalty?
# def or off?
# turnover?
# INT, fumble, to on downs?
# off play makers
# def play makers

描述可能会变得非常复杂(多次失误和恢复并受到惩罚等),我想知道我是否可以利用那里的一些 NLP 模块。我可能会花几天时间研究像解析器这样的哑/静态状态机,但如果有人对如何使用 NLP 技术处理它有建议,我想听听他们的意见。

最佳答案

我认为 pyparsing 在这里非常有用。

您的输入文本看起来非常规则(不像真正的自然语言),而 pyparsing 在这方面做得很好。你应该看看它。

例如解析下面的句子:

Mat McBriar punts for 32 yards to NYJ14.
Mark Sanchez rush to the right for 3 yards to the NYJ24.

你可以用类似的东西定义一个解析语句(在文档中寻找准确的语法):

name = Group(Word(alphas) + Word(alphas)).setResultsName('name')

action = Or(Exact("punts"),Exact("rush")).setResultsName('action') + Optional(Exact("to the")) + Or(Exact("left"), Exact("right")) )

distance = Word(number).setResultsName("distance") + Exact("yards")

pattern = name + action + Exact("for") + distance + Or(Exact("to"), Exact("to the")) + Word()

并且 pyparsing 会使用这种模式来破坏字符串。它还将返回一个字典,其中包含从句子中提取的项目名称、 Action 和距离。

关于python - 用于解析体育比赛数据的自然语言解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8198923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com