[英]Extract words surrounding a UNICODE word
This code works perfectly for this English sentence. 该代码非常适合该英语句子。
But when I try to do this with a sentence in Hindi language, it gives me error that word is not in the list. 但是,当我尝试使用印地文语言的句子来执行此操作时,出现错误信息:单词不在列表中。
This is my Hindi sentence: 这是我的北印度语句子:
प्रखर बुद्धि तेजस्वी बालक राजेन्द्र बाल्यावस्था में ही फारसी में शिक्षा ग्रहण करने लगा और उसके पश्चात प्राथमिक शिक्षा के लिए छपरा के जिला स्कूल |
I want to extract words adjacent to the word बालक
. 我想提取与单词
बालक
相邻的单词。
Python code: Python代码:
import re
sentence = 'The world is a small place, we should try to take care of it.'
words = re.findall(r'\w+', sentence)
index = words.index('place')
left = words[index - 3:index]
right = words[index + 1:index + 4]
You may do like this on python 3. 您可以在python 3上这样做。
>>> import re
>>> s = 'प्रखर बुद्धि तेजस्वी बालक राजेन्द्र बाल्यावस्था में ही फारसी में शिक्षा ग्रहण करने लगा और उसके पश्चात प्राथमिक शिक्षा के लिए छपरा के जिला स्कूल |'
>>> re.findall(r'(\S+)\s+बालक\s+(\S+)', s)
[('तेजस्वी', 'राजेन्द्र')]
ie,. 即。
>>> left, right = re.findall(r'(\S+)\s+बालक\s+(\S+)', s)[0]
>>> left
'तेजस्वी'
>>> right
'राजेन्द्र'
>>>
Update: 更新:
To get two adjacent words. 得到两个相邻的单词。
>>> left, right = re.findall(r'(\S+\s+\S+)\s+बालक\s+(\S+\s+\S+)', s)[0]
>>> left
'बुद्धि तेजस्वी'
>>> right
'राजेन्द्र बाल्यावस्था'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.