| 1712 | ] |
| 1713 | |
| 1714 | def get_file_mentions(self, content, ignore_current=False): |
| 1715 | words = set(word for word in content.split()) |
| 1716 | |
| 1717 | # drop sentence punctuation from the end |
| 1718 | words = set(word.rstrip(",.!;:?") for word in words) |
| 1719 | |
| 1720 | # strip away all kinds of quotes |
| 1721 | quotes = "\"'`*_" |
| 1722 | words = set(word.strip(quotes) for word in words) |
| 1723 | |
| 1724 | if ignore_current: |
| 1725 | addable_rel_fnames = self.get_all_relative_files() |
| 1726 | existing_basenames = {} |
| 1727 | else: |
| 1728 | addable_rel_fnames = self.get_addable_relative_files() |
| 1729 | |
| 1730 | # Get basenames of files already in chat or read-only |
| 1731 | existing_basenames = {os.path.basename(f) for f in self.get_inchat_relative_files()} | { |
| 1732 | os.path.basename(self.get_rel_fname(f)) for f in self.abs_read_only_fnames |
| 1733 | } |
| 1734 | |
| 1735 | mentioned_rel_fnames = set() |
| 1736 | fname_to_rel_fnames = {} |
| 1737 | for rel_fname in addable_rel_fnames: |
| 1738 | normalized_rel_fname = rel_fname.replace("\\", "/") |
| 1739 | normalized_words = set(word.replace("\\", "/") for word in words) |
| 1740 | if normalized_rel_fname in normalized_words: |
| 1741 | mentioned_rel_fnames.add(rel_fname) |
| 1742 | |
| 1743 | fname = os.path.basename(rel_fname) |
| 1744 | |
| 1745 | # Don't add basenames that could be plain words like "run" or "make" |
| 1746 | if "/" in fname or "\\" in fname or "." in fname or "_" in fname or "-" in fname: |
| 1747 | if fname not in fname_to_rel_fnames: |
| 1748 | fname_to_rel_fnames[fname] = [] |
| 1749 | fname_to_rel_fnames[fname].append(rel_fname) |
| 1750 | |
| 1751 | for fname, rel_fnames in fname_to_rel_fnames.items(): |
| 1752 | # If the basename is already in chat, don't add based on a basename mention |
| 1753 | if fname in existing_basenames: |
| 1754 | continue |
| 1755 | # If the basename mention is unique among addable files and present in the text |
| 1756 | if len(rel_fnames) == 1 and fname in words: |
| 1757 | mentioned_rel_fnames.add(rel_fnames[0]) |
| 1758 | |
| 1759 | return mentioned_rel_fnames |
| 1760 | |
| 1761 | def check_for_file_mentions(self, content): |
| 1762 | mentioned_rel_fnames = self.get_file_mentions(content) |