从java中的给定文本中提取阿拉伯语短语 [英] Extract Arabic phrases from a given text in java

查看:228
本文介绍了从java中的给定文本中提取阿拉伯语短语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我找一个带有短语列表的正则表达式并检查给定文本中是否存在其中一个短语吗?

Can you help me in finding a regex that take list of phrases and check if one of these phrases exist in the given text, please?

示例:

如果我在 hashSet 中有以下字样:

If I have in the hashSet the following words:

كيف الحال  
إلى أين  
أين يوجد  
هل من أحد هنا  

给定的文字是:كيفالحالأتمنىأنتكونبخير

我想在执行正则表达式后得到:كيفالحال

I want to get after performing regex: كيف الحال

我的初始代码:

HashSet<String> QWWords = new HashSet<String>();

QWWords.add("كيف الحال");
QWWords.add("إلى أين");
QWWords.add("أين يوجد");
QWWords.add("هل من أحد هنا");

String s1 = "كيف الحال أتمنى أن تكون بخير";

for (String qp : QWWords) {

    Pattern p = Pattern.compile("[\\s" + qp + "\\s]");

    Matcher m = p.matcher(s1);

    String found = "";

    while (m.find()) {
        found = m.group();
        System.out.println(found);

    }

}


推荐答案

[...] 字符类和字符类只能匹配它指定的一个字符。例如,像 [abc] 这样的字符类只能匹配 a OR b c 。因此,如果您只想找到单词 abc ,请不要用 [...] 包围它。

[...] is character class and character class can match only one character it specifies. For instance character class like [abc] can match only a OR b OR c. So if you want to find only word abc don't surround it with [...].

另一个问题是你使用 \\\\ 作为单词分隔符,所以在下面的字符串中

Another problem is that you are using \\s as word separator, so in following String

String data = "foo foo foo foo";

regex \\sfoo \\\\ 将无法匹配第一个 foo 因为 之前没有空格。

所以首先匹配它将find将是

regex \\sfoo\\s will not be able to match first foo because there is no space before.
So first match it will find will be

String data = "foo foo foo foo";
//      this one--^^^^^

现在,因为正则表达式消耗了空间在第二个 foo 之后,它无法在下一场比赛中重复使用,因此第三个 foo 也将被跳过,因为没有空格可以在它之前匹配。

你也不会匹配 foo 因为这次之后没有空格

Now, since regex consumed space after second foo it can't reuse it in next match so third foo will also be skipped because there is no space available to match before it.
You will also not match forth foo because this time there is no space after it.

要解决此问题,您可以使用 \\ b - 字边界,用于检查它所代表的地点是否在字母数字字符和非字母数字字符之间(或字符串的开头/结尾)。

To solve this problem you can use \\b - word boundary which checks if place it represents is between alphanumeric and non-alphanumeric characters (or start/end of string).

所以而不是

Pattern p = Pattern.compile("[\\s" + qp + "\\s]");

使用

Pattern p = Pattern.compile("\\b" + qp + "\\b");

或者更好 Tim提到

Pattern p = Pattern.compile("\\b" + qp + "\\b",Pattern.UNICODE_CHARACTER_CLASS);

确保 \\\\ 将在预定义的字母数字类中包含阿拉伯字符。

to make sure that \\b will include Arabic characters in predefined alphanumeric class.

更新:

我不确定你的话是否可以包含正则表达式元字符,如 { [ + * 等等,以防你还可以添加转义机制来将这些字符更改为文字。

I am not sure if your words can contain regex metacharacters like { [ + * and so on, so just in case you can also add escaping mechanism to change such characters into literals.

所以

"\\b" + qp + "\\b"

可以成为

"\\b" + Pattern.quote(qp) + "\\b"

这篇关于从java中的给定文本中提取阿拉伯语短语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆