簡體   English   中英

如何檢查單詞是否跨句子?

[英]how to check if words lie across sentences?

我有一個從文本文件中讀取某些單詞並成對顯示它們的代碼(取決於它們在段落中的出現-例如:

Hi I am <PER>Rita</PER>.I live in <LOC>Canada</LOC>
Hi I am <PER>Jane</PER> and I do not live in <LOC>Canada<LOC/> 

輸出量

麗塔加拿大
簡加拿大

(注意:這不是xml文件。)
我希望輸出該對(Rita Canada)= 1(因為它們之間出現句號)和(Jane Canada)= 0(因為它們之間沒有句號)
這是我的代碼,用於按段輸出名稱。 您能幫助我確定句點嗎?

private static final Pattern personPattern = Pattern.compile("<PER>(.+?)</PER>");
private static final Pattern locationPattern = Pattern.compile("<LOC>(.+?)</LOC>");
for(File file : listOfFiles)
    {
        BufferedReader input = new BufferedReader(new FileReader(file));

        String line = "";
        while((line = input.readLine()) != null)
        {

            ArrayList<String> persons = new ArrayList<String>();
            ArrayList<String> locations = new ArrayList<String>();
            Matcher m_person = personPattern.matcher(line);
            while(m_person.find())
            {
                persons.add(m_person.group(1));

            }

            Matcher m_location = locationPattern.matcher(line);
            while(m_location.find())
            {
                locations.add(m_location.group(1));

            }


            for(int i = 0;i<persons.size();i++)
            {
                for(int j =0 ;j<locations.size();j++)
                {

                System.out.println(persons.get(i) + "\t" + locations.get(j));
                }

            }

PER標簽是否總是在LOC標簽之前? 他們有時在不同的地方嗎?

在下面的正則表達式中,我指定了一個正向查找(?=)其中包含一個與\\.匹配的原子團(?>\\.) \\. 如果沒有,則失敗。

然后是第二個捕獲組的交替,因此在沒有\\.的情況下,模式可以繼續匹配\\.

<PER>(.+?)</PER>(?=(?>\\.))|<PER>(.+?)</PER>

捕獲組1:麗塔

捕獲組2:簡

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM