从 php 调用 wordnet(PHP 的 Wordnet 类或 API)
我正在尝试编写一个程序来查找两个文档之间的相似性,由于我只使用英语,所以我决定使用wordnet,但是我找不到将wordnet与php链接的方法,我无法从php找到任何wordnet api。
我在论坛上看到有人说(Spudley)他从 php 调用了 wordnet(使用 shell_exec() 函数), PHP 的同义词库类或 API [已编辑]
我真的很想知道使用的方法或一些示例代码,可能是开始使用 php 的 wordnet 的教程。
非常感谢
I am trying to write a program to find similarity between two documents, and since im using only english, I decided to use wordnet, but I cannot find a way to link the wordnet with php, I cannot find any wordnet api from php.
I saw in the forum some one said (Spudley) he called wordnet from php (using shell_exec() function),
Thesaurus class or API for PHP [edited]
I would really like to know a method used or some example code, a tutorial perhaps to start using the wordnet with php.
many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 WordNet 站点链接到的 PHP 扩展非常古老且过时——它声称可以与 PHP4 一起使用,所以我认为它已经很多年没有被人关注了。
没有任何其他 API 可用于 WordNet->PHP,因此我推出了自己的解决方案。
WordNet 可以从命令行运行,因此 PHP 的 shell_exec() 函数可以读取输出。
如果您从不带任何参数的命令行(cd 到 Wordnet 的目录,然后只需
wn
)运行 WordNet,它将显示 Wordnet 支持的可能功能的列表。仍然在命令行中,如果您随后尝试其中一个/某些功能,您将看到 Wordnet 如何输出其结果。例如,如果您想要单词“star”的同义词,您可以尝试
-synsn
函数:这将产生类似于以下的输出:
在 PHP 中,您可以使用 shell_exec() 函数读取相同的输出。
现在
$result
应该包含上面引用的文本块。此时,您必须进行一些适当的编码。您需要获取该文本块并解析它以获取所需的数据。
这就是棘手的地方。由于数据以供人类而非程序读取的格式呈现,因此准确解析很困难。
值得注意的是,不同的搜索选项的输出略有不同。而且,返回的一些结果可能有些深奥。我最终编写了一个加权系统来对结果进行评分,但它非常适合我的需求,因此您需要对其进行试验才能提出自己的系统。
我希望这对您有足够的帮助。 :)
The PHP extension which is linked to from the WordNet site is very old and out of date -- it claims to work with PHP4, so I don't think it's been looked at in years.
There aren't any other APIs available for WordNet->PHP, so I rolled my own solution.
WordNet can be run from the command-line, so PHP's
shell_exec()
function can read the output.If you run WordNet from the command-line (cd to Wordnet's directory, then just
wn
) without any parameters, it will show you a list of possible functions that Wordnet supports.Still in the command-line, if you then try one/some of those functions, you'll see how Wordnet outputs its results. For example, if you want synonyms for the word 'star', you could try the
-synsn
function:This will produce output that looks a bit like this:
In PHP, you can read this same output using the
shell_exec()
function.Now
$result
should contain the block of text quoted above.At this point, you have to do some proper coding. You'll need to take that block of text and parse it for the data you want.
This is where it gets tricky. Because the data is presented in a format designed to be read by a human rather than by a program, it is tricky to parse accurately.
It is important to note that different search options present their output slightly differently. And, some of the results that are returned can be somewhat esoteric. I ended up writing a weighting system to score the results, but it was fairly specific to my needs, so you'll need to experiment with it to come up with your own system.
I hope that's enough help for you. :)
我知道有点太晚了,但最近我创建了一个库来解决自己的问题
Wordnet php 包装器
I know it's kinda too late but recently I made a library to scratch my own itch
Wordnet php wrapper